Search This Blog

Sunday, September 27, 2015

Associate child record with parent using xrmservicetolkit in crm 2013 javascript

In this example I associate contact with an account

function associateRec()
{
var stdId = Xrm.Page.data.entity.getId();//get Account record's GUID
XrmServiceToolkit.Rest.Associate(
    stdId,
    "AccountSet",
    "0F383E15-1047-E511-80D7-3863BB3C5778",//contact record's GUID which u want to associate
    "ContactSet",
    "contact_customer_accounts",//account and contact relationship name
    function () {
         alert("Associated"); //Success - No Return Data - Do Something
    },
    function (error) {
         alert(error.message);
    },
    true
);
}

Thursday, September 24, 2015

Retrieve multiple records using javascript xrmservicetoolit in crm 2013 and crm 2015

function retrieve_multiple()
{

var stdId = Xrm.Page.data.entity.getId();
XrmServiceToolkit.Rest.RetrieveMultiple(
    "new_departmentSet",
    "?$select=new_Description&$filter=new_School/Id eq guid'"+stdId+"'",
    function (results) {
    var ab="";
    for (var i = 0; i < results.length; i++) {
         var Description = results[i].new_Description;
         //var new_School = results[i].new_School;
         //alert(Description);
          var des = Description == null ? 0 :  Description;
         ab=des+"-"+ab;
         if (ab!=null)
            {
            Xrm.Page.getAttribute("new_description").setValue(ab);
            Xrm.Page.getAttribute("new_description").setSubmitMode("always");
            }
            else
            {
            Xrm.Page.getAttribute("new_description").setValue("");
            Xrm.Page.getAttribute("new_description").setSubmitMode("always");   
            }
       
    }
    },
    function (error) {
         alert(error.message);
    },
    function () {
         //On Complete - Do Something
    },
    true
);
}

Create Record in related entity using javascript xrmservicetoolkit in crm 2013 and crm 2015

In this function i want to create record of department against School (School (1): Depattment(N))

function CreateRecord()
{
 var stdId = Xrm.Page.data.entity.getId();
var entity = {};
 entity.new_School = {
            Id: stdId,
            LogicalName: "new_department"//logical name of entity if which you want to create record
        };
entity.new_name = "test";

XrmServiceToolkit.Rest.Create(
    entity,
    "new_departmentSet",//
    function (result) {
         var newEntityId = result.new_departmentId;
    },
    function (error) {
         alert(error.message);
    },
    true
);
}

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

Hide and Show button using ribbon editor Display rule on status change in crm 2013

Create Command and add Display rule


Inside Display rule. By default hide this button by. IsCore: False


Add steps in Display rule By default hide button default: False. Now when status reason(statuscode)=175650000 invert result to true. 

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

Wednesday, September 9, 2015

Resolve Error the given key was not present in the dictionary plugin error

this error is due to no fields are not getting then create a Post image inside Update step




Now add this Entity Alias StudentCourse in your plugin where your write "Target"
For example:

 if (context.PostEntityImages.Contains("StudentCourse") && context.PostEntityImages["StudentCourse"] is Entity)
            {
Entity entity = (Entity)context.PostEntityImages["StudentCourse"];
}

Updating other related entity using xrmsvctoolkit in crm 2013

 XrmSvcToolkit.updateRecord({

    //Entity we are updating
    entityName: "wse_timesheet",

    //Entity ID, taken from above
    id: ID,
    
    //Attribute, but Schema name not name of entity
    entity: {new_OpportunityRevenue:  { value: 2, type: "Money" }},
    
    async: false,

    successCallback: function (result) {

    },

    errorCallback: function (error) {

        alert("There was an error when updating the contact record");

    }

});