Question Status

Unanswered
SweetDh2 asked a question on 27 Jan 2015 1:55 PM

Hello,

I have a requirement where I need to add a custom button on Contact entity View(HomePage) Grid. Not on a form but on the Home. All the records that are selected, the custom button is going to check if the owner is a team and not themselves or another person then assign the record to themselves and update additional fields.

The code I have written throws error "Error e [object Document]" when I click on the button, regardless of any records being selected or not and the actual code doesn't get executed.

CODE:

function assignSelectedRecord(selectedIds)
{
    if (selectedIds != null && selectedIds != "")
    {
    var strIds = selectedIds.toString();
        var arrIds = strIds.split(",");
        for (var indxIds = 0; indxIds < arrIds.length; indxIds++)
    {
            assignApplicantRecords(arrIds[indxIds]);
        grid.refresh();
    }
    }
    else
    {
        alert("No records selected!");
    }
} //End of function assignSelectedRecord

//Function for setting values to send SOAP request for assigning record
function assignApplicantRecords(applicantId)
{
    var owner= Xrm.Page.getAttribute("ownerid").getValue();
    var ownerName = owner[0].name;
 
    if (ownerName == "ABUKL")
    {
        var applicantStatus = Xrm.Page.getAttribute("wb_status").getValue();
    
        if (applicantStatus == '108590001')
        {
            Xrm.Page.getAttribute("wb_status").setValue('108590000');
        }

        if(Xrm.Page.getAttribute("wb_recordassigneddate").getValue() == null)
        {
        var currentDateTime = new Date();
            Xrm.Page.getAttribute("wb_recordassigneddate").setValue(currentDateTime);
        }

        Xrm.Page.data.entity.save();

        var userId = Xrm.Page.context.getUserId();  // GUID of user to assign the record
        userId = userId.substr(1, 36);
        var entityName = Xrm.Page.data.entity.getEntityName();  // Entity name the record

        assignSelectedRecord(userId, applicantId, entityName);
        Xrm.Page.getAttribute("ownerid").setSubmitMode("always");
    } //End of if (ownerName == "ABUKL")
    else
    {
    alert ("Selected Applicant(s) is added on your list or another hiring manager's list. Only Applicant records owned by ABUKL can be added on your list.");
    }
} //End of function assignApplicantRecords

// Function for forming and sending SOAP request
function assignSelectedRecord(Assignee, Target, entityName)
{
    var request = ["<request i:type='b:AssignRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>",
                       "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",
                         "<a:KeyValuePairOfstringanyType>",
                           "<c:key>Target</c:key>",
                           "<c:value i:type='a:EntityReference'>",
                             "<a:Id>", Target, "</a:Id>",
                             "<a:LogicalName>", entityName, "</a:LogicalName>",
                             "<a:Name i:nil='true' />",
                           "</c:value>",
                         "</a:KeyValuePairOfstringanyType>",
                         "<a:KeyValuePairOfstringanyType>",
                           "<c:key>Assignee</c:key>",
                           "<c:value i:type='a:EntityReference'>",
                             "<a:Id>", Assignee, "</a:Id>",
                             "<a:LogicalName>", "systemuser", "</a:LogicalName>",
                             "<a:Name i:nil='true' />",
                           "</c:value>",
                         "</a:KeyValuePairOfstringanyType>",
                       "</a:Parameters>",
                       "<a:RequestId i:nil='true' />",
                       "<a:RequestName>Assign</a:RequestName>",
                     "</request>"].join("");

    // Get server URL

    var serverUrl = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web";

    var req = new XMLHttpRequest();
    req.open("POST", serverUrl, true)
    // Responses will return XML.
    var soapXml =
        ["<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>",
            "<soap:Body>",
                "<Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>", request, "</Execute>",
            "</soap:Body>",
            "</soap:Envelope>"
        ].join("");

    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    req.onreadystatechange = function () { assignSelectedResponse(req); };
    req.send(soapXml);
} // End of function AssignRecord

// This function gets called after the execution

function assignSelectedResponse(req)
{
    if (req.readyState == 4)
    {
        if (req.status == 200)
    {

        }
        else
    {

            alert("Error e " + req.responseXML);
        }
    }
}

On the ribbon button command I have referenced jquery and json files and function name as "isNAN" since the button is on home grid, the helper files do not get loaded dynamically.


Please advise. I am using CRM 2015 Online

Reply
MarkR_B responded on 28 Jan 2015 6:44 AM

SweetDh2

You will need to breakdown your code to check each part works.

From just viewing the code, the assignSelectedRecord() function looks ok, however within the assignApplicantRecords() function you cannot use the Xrm.Page.getAttribute("attribute") command as these Xrm libraries are specific to an entity form and not the home page. What you will need to do is to retrieve the record data using the odata (REST) webservice, as all that is available in your code at that point is the record id.

Hope this helps

Mark

Reply