Search This Blog

Friday, October 23, 2015

Change status using javascript xrmservicetoolkit in CRM 2015

function ChangeStatusReason_toReject() {
XrmServiceToolkit.Soap.SetState("entityname", Xrm.Page.data.entity.getId(), statecode, statuscode,false);
Xrm.Page.data.refresh(true).then(null, null);//for refreshing form
}

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");

    }

});

Wednesday, July 8, 2015

Run report on custom button using javascript in crm 2013

function OpenReport() {
    var rdlName = "Program Wise Intake.rdl"; //Replace
    var reportGuid = "F2F15B97-DD1F-E511-9E86-002590898E30"; //Replace
    var entityType = "10012"; //Replace
    var entityGuid = Xrm.Page.data.entity.getId(); 
    var url = Xrm.Page.context.getClientUrl() + "/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=" + rdlName + "&id={" + reportGuid + "}&records=" + entityGuid + "&recordstype=" + entityType;
    window.open(url, null, 800, 600, true, false, null);
}

Wednesday, June 24, 2015

Run Workflow on Button Click without code in CRM 2013

Button on Home Page:


Click on Button and Add a Command.
Add a JavaScript Command and set the following properties:

Function Name: Mscrm.GridRibbonActions.launchOnDemandWorkflow
Library: dev1_/RWB2013/Silverlight.js (for CRM 2013)
Parameters:
1: Type = Crm Parameter, Value = SelectedControl
2: Type = Crm Parameter, Value = SelectedControlSelectedItemReferences
3: Type = Crm Parameter, Value = SelectedEntityTypeCode
4: Type = String, Value = Workflow GUID

 Button on Form:

Click on Button and Add a Command.
Add a JavaScript Command and set the following properties:

Function Name: Mscrm.FormAction.launchOnDemandWorkflowForm
Liibrary: /_forms/form.js
Parameters:
1: Type = Crm Parameter, Value = PrimaryEntityTypeCode
2: Type = String, Value = Workflow GUID

Saturday, June 20, 2015

Change status reason using javascript and XrmSvcTool CRM 2013

function ChangeStatusReason() {
    //Set the stage to send enquiry letter
    XrmSvcToolkit.setState({
        id: Xrm.Page.data.entity.getId(),//record GUID
        entityName: "cms_applicant",//entity name
        stateCode: 0,//status code
        statusCode: 175650003, //status reason code
        async: false,
    });
    //Save and Reload form
    Xrm.Page.data.refresh(true).then(null, null);
}

Thursday, June 4, 2015

Retrieve Multiple records using XrmSvcTool in CRM 2013 javascript RetrieveMultiple function

function Calculations()// to get marks of all student courses marks related to student
{
var stdId = Xrm.Page.data.entity.getId();
if (stdId != null)
{
        var EntityNameP, ArrayNamesP, ArrayValuesP;
        ArrayValuesP = stdId;
           var _query = "?$filter=cms_Student/Id eq (Guid'"+stdId+"')";
        var obj = RecRetrieveMultiple(_query, "cms_studentcourse");
        var entityNodesP = obj;
               var TotalPoints = 0;
        for (var i = 0; i < entityNodesP.length; i++)
        {
            var entityNode = entityNodesP[i];
            var Node = entityNode.cms_ObtainedMarks;//get simple field
             var level = entityNode.cms_level.Value;
            var courselevel = entityNode.cms_CourseId.Name;//get lookup value from child entity
            var courselevel1 = entityNode.cms_CourseId.Id;//get lookup id from child entity
             var ResultNode = (Node == null) ? 0 :  parseFloat(Node);
            TotalPoints = parseFloat(TotalPoints) + parseFloat(ResultNode);//sum of all student courses marks
        }       
    }
}
function RecRetrieveMultiple(_query, _entityname){

var contacts;
XrmSvcToolkit.retrieveMultiple({
    entityName: _entityname,
    async: false,
    odataQuery: _query,
    successCallback: function (result) {
        contacts = result;
    },
    errorCallback: function (error) {
       alert("Error");
    }
});
var _result = contacts;
return _result;
}

Wednesday, June 3, 2015

Retrieve and set Lookup value from related entity using XrmSvcToolkit in CRM 2013

function GetDetails()
{
    var EntityName, EntityId, LookupFieldObject;
    //LookupFieldObject = Xrm.Page.data.entity.attributes.get('new_studentid');
    LookupFieldObject = Xrm.Page.data.entity.attributes.get('cms_batch');
    // If lookup field has value then the code will only run
    if (LookupFieldObject.getValue() != null)
    {
        //Fetch and place Entity Id (GUID) and Name (String) form lookup field into local variables
        EntityId = LookupFieldObject.getValue()[0].id;
        //EntityName = LookupFieldObject.getValue()[0].entityType;   
            var obj = RecRetrieve(EntityId, "cms_batch");
        // In retrieved XML document check if it has accountnumber attribute
   //below is code to retrieve and set lookup        
         if (obj.cms_MeritCriteria.Id != null )
        {
              var lookupData = new Array();
            //Create an Object add to the array.           
           var lookupItem= new Object();               
           lookupItem.id = obj.cms_MeritCriteria.Id;
            lookupItem.typename = 'cms_meritcriteria';
           lookupItem.name = obj.cms_MeritCriteria.Name;
           lookupData[0] = lookupItem;          
            // Set the value of the lookup field to the value of the array.
            Xrm.Page.getAttribute("cms_meritcriteria").setValue(lookupData);   
            Xrm.Page.getAttribute("cms_meritcriteria").setSubmitMode("always");           
        }
        else
        {
              Xrm.Page.data.entity.attributes.get('cms_meritcriteria').setValue(null);
        }
      
         
    }
}

function RecRetrieve(_id, _entityname)
 {

 var _result;
    XrmSvcToolkit.retrieve({
        entityName: _entityname,//"Contact",
        id: _id,//, 1FED44D1-AE68-4A41-BD2B-F13ACAC4ACFA//
        async: false,
        successCallback: function (result)
        {
            _result = result;
        },
        errorCallback: function (error)
        {
           alert("Error");

        }
    });

    return _result;
}