Wednesday, May 20, 2015

Full example - Assign Request – Client side

//Assign to user
    AssignRequest(Xrm.Page.data.entity.getId(), 'incident', result.fl_userid.Value, 'systemuser');
//Assign to Team
    AssignRequest(Xrm.Page.data.entity.getId(), 'incident', result.fl_teamid.Value, 'team');

function AssignRequest(targetID, targetLogicalName, assigneeID, assigneeLogicalName) {
    var requestMain = ""
    requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    requestMain += "  <s:Body>";
    requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    requestMain += "      <request i:type=\"b:AssignRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>Target</c:key>";
    requestMain += "            <c:value i:type=\"a:EntityReference\">";
    requestMain += "              <a:Id>" + targetID + "</a:Id>";
    requestMain += "              <a:LogicalName>" + targetLogicalName + "</a:LogicalName>";
    requestMain += "              <a:Name i:nil=\"true\" />";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>Assignee</c:key>";
    requestMain += "            <c:value i:type=\"a:EntityReference\">";
    requestMain += "              <a:Id>" + assigneeID + "</a:Id>";
    requestMain += "              <a:LogicalName>" + assigneeLogicalName + "</a:LogicalName>";
    requestMain += "              <a:Name i:nil=\"true\" />";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "        </a:Parameters>";
    requestMain += "        <a:RequestId i:nil=\"true\" />";
    requestMain += "        <a:RequestName>Assign</a:RequestName>";
    requestMain += "      </request>";
    requestMain += "    </Execute>";
    requestMain += "  </s:Body>";
    requestMain += "</s:Envelope>";
    try {
        var req = new XMLHttpRequest();
    }
    catch (e) {
        var req = new ActiveXObject("Msxml2.XMLHTTP");
    }
    req.open("POST", getServerUrlWithOrgServicePath(), isAsync)

    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");
    try { req.responseType = 'msxml-document'; } catch (e) { }
    req.send(requestMain);

    if (req.readyState == 4) {
        if (req.status == 200) {
            return true;
        }
        else {
            return req.responseXML;
        }
    }
}

function getServerUrlWithOrgServicePath() {
    var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
    var serverUrl = Xrm.Page.context.getClientUrl();
    if (serverUrl.match(/\/$/)) {
        serverUrl = serverUrl.substring(0, serverUrl.length - 1);

    }
    return serverUrl + OrgServicePath;
}

Enjoy,
Rami Heleg

No comments:

Post a Comment