Search This Blog

Showing posts with label retrieve single record. Show all posts
Showing posts with label retrieve single record. Show all posts

Monday, September 21, 2015

Retrieve record from related entity using XrmServiceToolkit javascript in crm 2013/2015

Download XrmServiceToolkit from here
Extract and upload these files into CRM as javascript webresource and also Add on the form where you need to retrieve
1. Json2
2. jquery
3. XrmServiceToolkit

Code: In this code i retrieve description field from related entity

function get_details()
{
var Courselookup=null;
var coursetitle_name=null;
var CourselookupId=null;

 Courselookup = Xrm.Page.data.entity.attributes.get('new_school');
    if (Courselookup.getValue() != null)
    {   
        CourselookupId = Courselookup.getValue()[0].id;   
        var course_obj = Retrieve(CourselookupId);
        if (course_obj.new_Description != null )
        {
            coursetitle_name=course_obj.new_Description;
            Xrm.Page.getAttribute("new_description").setValue(coursetitle_name);
            Xrm.Page.getAttribute("new_description").setSubmitMode("always");
        }
        }
}
function Retrieve(_id) {
    var re;   
    XrmServiceToolkit.Rest.Retrieve(
              _id,
              "new_schoolSet",
              null, null,
              function (result) {
                  re = result;                
              },
              function (error) {
                  alert("failed");
              },
              false
          );
          return re;
}



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;
}