Thursday, May 26, 2016

How to set the From and To Default values in Email in MS CRM

function RetrieveTOfromCASE() {
    var regardingObject = Xrm.Page.getAttribute("regardingobjectid");
    if (regardingObject.getValue() != null) {
        //SDK.REST.retrieveRecord(guid,entityname,select query,null,successcallback,errorcallback);
        SDK.REST.retrieveRecord(regardingObject.getValue()[0].id, "Incident", "Title,ContactId", null, successRetrieveEmail, errorHandler);
        
    }
}

function DefaultFROMandTO(GUID, NAME, LOGICALNAME) {
    //Set From Value
    if (Xrm.Page.ui.getFormType() == 1) {
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = "GUID";
        lookupValue[0].name = "NAME(Contact/User/Queue)";
        lookupValue[0].entityType = "ENTITYNAME(Contact/User/Queue)";
        Xrm.Page.getAttribute('from').setValue(lookupValue);

        //Set To Value
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = GUID;
        lookupValue[0].name = NAME;
        lookupValue[0].entityType = LOGICALNAME;
        Xrm.Page.getAttribute("to").setValue(lookupValue);
    }
}

var successRetrieveEmail = function (results) {
    DefaultTO(results.ContactId.Id, results.ContactId.Name, results.ContactId.LogicalName);
}

function errorHandler(error) {
}

//You need to add CRMSDK for this to work

Get Parent value in Child Record in MS CRM

function GetParentValue() {
//Fetch the case id.
//calling this function on Email form
    var regardingObject = Xrm.Page.getAttribute("regardingobjectid");
    if (regardingObject.getValue() != null) {
        //OData URI to get address information from parent account record
        var oDataURI = Xrm.Page.context.getClientUrl()
            + "/XRMServices/2011/OrganizationData.svc/"
            + "IncidentSet(guid'" + regardingObject.getValue()[0].id + "')"
            + "?$select=Title,ContactId";

        //Asynchronous XMLHttpRequest to retrieve account record
        var req = new XMLHttpRequest();
        req.open("GET", encodeURI(oDataURI), true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            debugger;
            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null; //avoids memory leaks
                if (this.status == 200) {
                    //parse the response string as a JSON object into the successCallback method.
                    //successCallback(JSON.parse(this.responseText).d);
                    //Directly show the alert and assign accordingly to other values
                    alert(JSON.parse(JSON.parse(this.responseText).d.Title));
                }
                else {
                    errorCallback(CaseID);
                }
            }
        };
        req.send();
    }
}

//Call the GetParentValue method on OnLoad.