Search This Blog

Wednesday, September 16, 2015

Retrieve Lookup value from related entity using javascript + xrmsvctoolkit in crm 2013

    Courselookup = Xrm.Page.data.entity.attributes.get('cms_courseid');
    if (Courselookup.getValue() != null)
    {
        CourselookupId = Courselookup.getValue()[0].id;   
        var course_obj = RecRetrieve(CourselookupId, "cms_course");

if (course_obj.cms_CourseSubArea.Id != null )
        {
           
               var arr_CourseSubArea = new Array();
            //Create an Object add to the array.           
           var obj_CourseSubArea= new Object();               
           obj_CourseSubArea.id = course_obj.cms_CourseSubArea.Id;
            obj_CourseSubArea.typename = 'cms_coursesubarea';//name of entity from where data retrieve
           obj_CourseSubArea.name = course_obj.cms_CourseSubArea.Name;
           arr_CourseSubArea[0] = obj_CourseSubArea;          
            // Set the value of the lookup field to the value of the array.
            Xrm.Page.getAttribute("cms_coursesubarea").setValue(arr_CourseSubArea);   
            Xrm.Page.getAttribute("cms_coursesubarea").setSubmitMode("always");        
        }
}
function RecRetrieve(_id, _entityname)
 {
 var _result;
    XrmSvcToolkit.retrieve({
        entityName: _entityname,
        id: _id,
        async: false,
        successCallback: function (result) {
            _result = result;
        },
        errorCallback: function (error) {
           alert("Error");

        }
    });
    return _result;
}

No comments:

Post a Comment