MaPi
MaPi

Reputation: 1601

Retrieve Entity attributes from the Entity homepage (Grid view)

I have built a process that will be called by a button from the Ribbon. I have added it to the form view, without problems and I want it to work from the grid view too.

The problem is that I am validating some data before the actual process starts. In the Visual Ribbon i pass SelectedControlSelectedItemIds as a parameter and I only enable the button when there is only one record selected.

The JS called looks like this:

function AutoQualify(dialogId, typeName, recordId, width, heigth) {


    retrieveLeadRecord(recordId);

    var stateCode = getLeadStateCode(recordId)
    if ((stateCode === null) || (stateCode === undefined) || (stateCode === "")) {
        alert("status Code fält kan inte vara tömt");
        return;
    }
    if ((stateCode != 0)) {
        alert("Man kan inte köra Quicksale från ett kvalificerat Lead");
        return;
    }

    var firstName = GetAttributeValue('firstname');
    if ((firstName === null) || (firstName === undefined) || (firstName === "")) {
        alert("Förstanamn fält kan inte vara tömt");
        return;
    }

    var lastName = GetAttributeValue('lastname');
    if ((lastName === null) || (lastName === undefined) || (lastName === "")){
        alert("Efternamn fält kan inte vara tömt");
        return; 
    }

    var companyName = GetAttributeValue('companyname');
    if ((companyName === null) || (companyName === undefined) || (companyName === "")) {
        alert("Företagsnamn fält kan inte vara tömt");
        return;
    }

    var orgNummer = GetAttributeValue('custom_corporateidentificationnumber');
    if ((orgNummer === null) || (orgNummer === undefined) || (orgNummer === "")) {
        alert("Organisationsnummer fält kan inte vara tömt");
        return;
    }

    var leadSourceCode = GetAttributeValue('leadsourcecode');
    if ((leadSourceCode === null) || (leadSourceCode === undefined) || (leadSourceCode === "")) {
        alert("Leadskälla fält kan inte vara tömt");
        return;
    }

    var mailAddress = GetAttributeValue('emailaddress1');
    var telephone = GetAttributeValue('telephone1');

    if (((mailAddress === null) || (mailAddress === undefined) || (mailAddress === "")) && ((telephone === null) || (telephone === undefined) || (telephone === ""))) {
        alert("Man måste ange e-post adress eller telefonnummer");
        return;
    }

    if (confirm("Vill du kvalificera leadet?")) {
        LaunchModalDialogChangeSize(dialogId, typeName, recordId, width, heigth, Xrm.Page.context.getServerUrl() );
    }
}

The retrieveUserRecord function:

function retrieveLeadRecord(Id) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var GlobalODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    var select = "/LeadSet?$select=StateCode,StatusCode&$filter=LeadId eq guid'" + Id + "'";
    showMessage("retrieveLead function START");
    var retrieveLeadReq = new XMLHttpRequest();
    retrieveLeadReq.open("GET", GlobalODataPath + select, true);
    retrieveLeadReq.setRequestHeader("Accept", "application/json");
    retrieveLeadReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    alert("3");
    retrieveLeadReq.onreadystatechange = function () {
        retrieveLeadReqCallBack(this);
};
    retrieveLeadReq.send();
    showMessage("retrieveLead function END.");
}

but in never gets past the XMLHttpRequest declaration. Is there a library missing?

Upvotes: 2

Views: 1573

Answers (2)

MaPi
MaPi

Reputation: 1601

I managed to fix it. In the Visual Ribbon editor I have an enabling rules that enable the button only when there is only one record selected. Another activation rule if the call of the activateQuicksaleForLead, with the SelectedControlSelectedItemIds a parameter. The button requires the function to return true to be enabled.

function activateQuicksaleForLead(recordId) {
    function myBool() {
        this.value = true;
    }
    var active = new myBool();
    retrieveLeadStatus(recordId, active);
    return active.value;
}

The retrieveLeadStatus retrieves the value by making a sync call to the oData CRM webservice

function retrieveLeadStatus(Id, active) {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var GlobalODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    var select = "/LeadSet?$filter=LeadId eq guid'" + Id + "'";
    var retrieveLeadReq = new XMLHttpRequest();
    retrieveLeadReq.open("GET", GlobalODataPath + select, false);
    retrieveLeadReq.setRequestHeader("Accept", "application/json");
    retrieveLeadReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    retrieveLeadReq.send(null);
    var records = JSON.parse(retrieveLeadReq.responseText).d;
    var stateCode = records.results[0].StateCode.Value;
    if ((stateCode === null) || (stateCode === undefined) || (stateCode === "")) {
        alert("Det går inte att hitta posten. Kontakta din system admininistrator.");
        activateQuickSaleLead = false;
        return;
    }
    if ((stateCode != 0)) {
        active.value = false;
        return;
    }
    else {
        active.value = true;
        return;
    }
}

Upvotes: 1

Conor Gallagher
Conor Gallagher

Reputation: 1499

Although I commented, now that I reread it your code looks incorrect. Even though you're only selecting 1 row in the grid the IDs will still come in as an Array (via recordId I imagine if you're properties are set up correctly). But the problem you'll hit is you need to handle both a Form and a Grid. Because the Form will just give you an ID whereas a grid view will pass in an array (or possibly comma delimited string, I can't remember which)/

So, effectively you will need to do 1 of 2 things:

  1. Option 1 is within the JavaScript check the variable "recordId" to see if it's just 1 id or if it is multiple. Depending on if it's an array or a delimited string the code could vary so I don't have an example off the top of my head

  2. Option 2 is create a second function that accepts a recordId and passes through the first element to the other function. Then you can set your grid button to call this function instead. Function would look something like this:

    function AutoQualifyMultiple(dialogId, typeName, recordIds, width, heigth)
    {
      AutoQualify(dialogId, typeName, recordIds[0], width, heigth);
    }

Upvotes: 1

Related Questions