Question Status

Suggested Answer
Daniel Nunes asked a question on 17 Mar 2015 12:02 PM

Hello,

I'm currently trying to compare a value from my opportunities to a value in my custom entity. If the value is different when we open the custom entity or if we change it an alert must pop. 

This is my first project so my js skills are not at 100% yet...

Thank you for any advice

Reply
cwojciuk responded on 17 Mar 2015 12:47 PM

Daniel,

I'm not sure if you use one single entity or use a lookup on the opportunity form.

However, if you are using a lookup field this may help:

First, I would download a oData helper library to make the queries easier.

http://crmjsrest.codeplex.com/ is a good library.

Then you can use the library to get the custom entity field you want. This will work best if you have a field linking to the custom entity because you need the Guid of the record.

Something like this:

function compareFields(customEntityFieldName,entitySchemaName){

    //This gets the guid of the lookup field to the custom entity
    var custEntityGuid = Xrm.Page.getAttribute(customEntityFieldName).getValue();

    //This removes the { and } from the returned guid
    custEntityGuid = custEntityGuid.replace('{','').replace('}','');

    //Now we can call the RetrieveRecord which returns a json object
    //We have to add the "Set" to the entity name
    var record = SDK.CrmData.RetrieveRecord(custEntityGuid,entitySchemaName+"Set");

    //Use the returned json to get the field out of the record.
    //Use Xrm to then compare the field value in the custom entity to the field

}

You can use whatever method you want but take a look at the functions in the library to get a better understanding of oData calls.

Hopefully that helps!

-Chuck

Reply
Suggested Answer
Mithilesh Kumar responded on 18 Mar 2015 3:15 AM

Hi Daniel,

Use oData to Query your Database and bring in your values from Opportunity.

Code snippet below

function getOpportunityData(){  
     var oDataURI = Xrm.Page.context.getClientUrl()
        + "/XRMServices/2011/OrganizationData.svc/"
        + "OpportunitySet(guid'" + opportunityid + "')"
        + "?$select="
        + "Name";
 
    //Asynchronous XMLHttpRequest to retrieve opportunity 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);
            }
            else {
                errorCallback(appointmentid);
            }
        }
    };
    req.send();
}

Hope that helps

Thanks

Reply
Suggested Answer
Mithilesh Kumar responded on 18 Mar 2015 3:15 AM

Hi Daniel,

Use oData to Query your Database and bring in your values from Opportunity.

Code snippet below

function getOpportunityData(){  
     var oDataURI = Xrm.Page.context.getClientUrl()
        + "/XRMServices/2011/OrganizationData.svc/"
        + "OpportunitySet(guid'" + opportunityid + "')"
        + "?$select="
        + "Name";
 
    //Asynchronous XMLHttpRequest to retrieve opportunity 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);
            }
            else {
                errorCallback(appointmentid);
            }
        }
    };
    req.send();
}

Hope that helps

Thanks

Reply