Javascript to replace customerid field on a CASE form when opened or saved RRS feed

  • Question

  • Hello all,

    I've got a workflow that triggers when a queue item is created, creating a new Case record. 

    The workflow is unable to directly set the "Customer" field in the Case to the "From" field of the original email because of the different in field type.

    As an attempted workaround without having to create a custom workflow activity, I've created a new text field in the Case entity called "new_emailfrom".

    The workflow logs the email "From" field into this new field in the Case entity, so it will contain an email address)

    Is it possible to use javascript upon form save to:

    1.Take the value of "new_emailfrom" field

    2.Search for a contact that has this email address

    3.Sets the "customerid" field in the Case to be the found contact.

    I've drafted up some code but I don't know how to approach step 2, or if it's even possible.

    Any help gratefully received,

    Jscript so far:

    function retrievecontactBycontactemail()

    var contactemail = Xrm.Page.getAttribute("new_emailfrom").getValue();    

    if (contactemail != null)
    //find a contact where "emailaddress1" field = contactemail



    Friday, August 30, 2013 10:48 AM

All replies

  • To retrieve an entity by an attribute value you can using the Odata endpoint (SDK reference : Sample: Create, Retrieve, Update and Delete Using the REST Endpoint with JavaScript)

    Code could look similar to this:

    var oDataSelect = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=ContactId&$filter=EMailAddress1 eq '" + emailaddress + "'";
    var req = new XMLHttpRequest();
    req.open("GET", oDataSelect, false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
            if (req.status == 200) {
                var retrieved = JSON.parse(req.responseText).d;
                //Might have multiple results to review
                var value = new Array();
                value[0] = new Object();
                value[0].id = retrieved.results[0].Id;
                value[0].name = retrieved.results[0].Name;
                value[0].entityType = retrieved.results[0].LogicalName;

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, August 30, 2013 12:03 PM