Sunday 19 January 2014

Get entity form name in CRM 2013 using Javascript.

function getFormName() {
    var formLabel = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
    alert(formLabel.toString());
    if (formLabel == 'YourFormName') {
       //Write your logic here.
    }
}

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


Monday 6 January 2014

Prevent to create opportunity when qualifying Lead in CRM 2013

Use the following code in your plugin and register in the PostValidation on the QualifyLead message for the Lead entity.

context.InputParameters["CreateOpportunity"] = false; // set to true by default
//context.InputParameters["CreateAccount"] = false; // set to true by default
//context.InputParameters["CreateContact"] = false; // set to true by default