Sunday 19 January 2014

Retrieve lookup field data in CRM 2013 usign javascript.

The following sample code is used to retrieve entity information by Entity Id via Javascript.

function getRecordDetails() {
    var EntityName, EntityId, LookupFieldObject;
    var Name = "";
    var resultXml;
    LookupFieldObject = Xrm.Page.data.entity.attributes.get('parentaccountid');
    if (LookupFieldObject.getValue() != null) {
        EntityId = LookupFieldObject.getValue()[0].id;
        EntityName = LookupFieldObject.getValue()[0].entityType;
        resultXml = getDetails(EntityName, EntityId);
// Retrieve optionset value and set the value to current record field.
        if (resultXml != null && resultXml.attributes['new_industry'] != null) {
            var industry = resultXml.attributes['new_industry'].value;          
            Xrm.Page.getAttribute('new_common_industry').setValue(industry);          
        }
        else
            Xrm.Page.getAttribute('new_common_industry').setValue(null);

// Retrieve text field value and set the value to current record field.
        if (resultXml != null && resultXml.attributes['new_industrydetail'] != null) {
            var industrydetail = resultXml.attributes['new_industrydetail'].value;          
            Xrm.Page.data.entity.attributes.get('new_industrydetail').setValue(industrydetail);          
        }
        else
            Xrm.Page.data.entity.attributes.get('new_industrydetail').setValue("");

// Retrieve lookup field value and set the value to current record field.
        if (resultXml != null && resultXml.attributes['primarycontactid'] != null) {          
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = resultXml.attributes['primarycontactid'].id;
            lookupValue[0].name = resultXml.attributes['primarycontactid'].name;
            lookupValue[0].entityType = resultXml.attributes['primarycontactid'].logicalName;
            Xrm.Page.data.entity.attributes.get("parentcontactid").setValue(lookupValue);
        }
        else
            Xrm.Page.data.entity.attributes.get("parentcontactid").setValue(null);
    }
}

function getDetails(EntityName, EntityId) {
    var cols = ["new_industry", "new_industrydetail", "primarycontactid"];
    var retrievedResult = XrmServiceToolkit.Soap.Retrieve(EntityName, EntityId, cols);
    return retrievedResult;
}

After that you need to add the following files to the Form Libraries.

Jquery
Json2
XrmServiceToolkit


1 comment:

  1. Try not to become a man of success, but rather try to become a man of value. See the link below for more info.


    #value
    www.ufgop.org

    ReplyDelete