Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Javascript help

(0) ShareShare
ReportReport
Posted on by 2,115

Hi all,

So I've got a Boolean field on case that has the same logical name as a field on lead (that field is called new_field). I've connected the case to lead using a lead lookup field called new_lead.

Now whenever I toggle the Boolean field on case I want it to update the related Lead in the Lead lookup field. In the form I've passed the 

pastedimage1575526413964v1.png

I've then written the following Javascript:

function testFunction (executionContext,paramOne){ // paramOne will contain the field logical name (new_field)
    var formContext = executionContext.getFormContext();
    var lead = formContext.getAttribute("new_lead").getValue(); // Get the value of the lead lookup field
    if (lead !== null) { // Check if the lead lookup field contains data
        var leadId = lead[0].id.slice(1, -1); //Slice the lead ID into a usable format
        var paramOneValue = formContext.getAttribute(paramOne).getValue(); // Get the field value of the passed field name
        var entity = {};
        entity.paramOne = paramOneValue; // Trying to update a field in leads that has the same logical name. I'm trying to pass paramOne as a variable
        // Normally paramOne would be the logical name of the field on lead (e.g. new_field). But it doesn't seem to want to use the paramOne pass field name

        var req = new XMLHttpRequest();
        req.open("PATCH", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/leads(" leadId ")", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function() {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    //Success
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(entity));
    }
}

This seems to give me a error but when I modify the JS to the below it works

function testFunction (executionContext,paramOne){ // paramOne will contain the field logical name (new_field)
    var formContext = executionContext.getFormContext();
    var lead = formContext.getAttribute("new_lead").getValue(); // Get the value of the lead lookup field
    if (lead !== null) { // Check if the lead lookup field contains data
        var leadId = lead[0].id.slice(1, -1); //Slice the lead ID into a usable format
        var paramOneValue = formContext.getAttribute(paramOne).getValue(); // Get the field value of the passed field name
        var entity = {};
        entity.new_field = paramOneValue; // This seems to work if I replace paramOne with the actual lead field logical name

        var req = new XMLHttpRequest();
        req.open("PATCH", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/leads(" leadId ")", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function() {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    //Success
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(entity));
    }
}

It seems it won't pass the parameter field logical name into my PATCH request.

Is there anyway to dynamically sync this with the passed paramOne?

  • Suggested answer
    Abhishek Dhoriya Profile Picture
    Abhishek Dhoriya 1,011 on at
    RE: Javascript help

    Hi Mike,

    I this is related to JavaScript Objects creation, in this case you are trying to set the JavaScript Property name dynamically which I believe is possible too.

    Try changing below line from

    entity.new_field = paramOneValue;

    TO

        entity[paramOne] = paramOneValue;

    OR

          eval("entity." + paramOne + " = '" + paramOneValue + "'");

    And still if problem persists which I think should not, then use the below change to your code which is if... else blocks

    function testFunction (executionContext,paramOne){ // paramOne will contain the field logical name (new_field)

       var formContext = executionContext.getFormContext();

       var lead = formContext.getAttribute("new_lead").getValue(); // Get the value of the lead lookup field

       if (lead !== null) { // Check if the lead lookup field contains data

           var leadId = lead[0].id.slice(1, -1); //Slice the lead ID into a usable format

           var paramOneValue = formContext.getAttribute(paramOne).getValue(); // Get the field value of the passed field name

           var entity = {};

            if (paramOne ==="new_field"){

           entity.new_field = paramOneValue; // This seems to work if I replace paramOne with the actual lead field logical name

    }

    else if (paramOne ==="new_fieldTwo"){

     entity.new_fieldTwo = paramOneValue; // And go on for different possible fields and places you are triggering this function from....

    }

           var req = new XMLHttpRequest();

           req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/leads("+leadId+")", true);

           req.setRequestHeader("OData-MaxVersion", "4.0");

           req.setRequestHeader("OData-Version", "4.0");

           req.setRequestHeader("Accept", "application/json");

           req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

           req.onreadystatechange = function() {

               if (this.readyState === 4) {

                   req.onreadystatechange = null;

                   if (this.status === 204) {

                       //Success

                   } else {

                       Xrm.Utility.alertDialog(this.statusText);

                   }

               }

           };

           req.send(JSON.stringify(entity));

       }

    }

    Hope this will solve your query. Kindly mark this as verified answer if it does.

    As always feel free to reach out to me if more assistance is needed.

    Have a great day ahead.....

    Best Regards

    Abhishek Dhoriya

    Consultant @ Microsoft India

  • Verified answer
    Abhishek Dhoriya Profile Picture
    Abhishek Dhoriya 1,011 on at
    RE: Javascript help

    Hi Mike,

    I this is related to JavaScript Objects creation, in this case you are trying to set the JavaScript Property name dynamically which I believe is possible too.

    Try changing below line from

    entity.new_field = paramOneValue;

    TO

        entity[paramOne = paramOneValue;

    OR

          eval("entity." paramOne " = '" paramOneValue "'");

    And still if problem persists which I think should not, then use the below change to your code which is if... else blocks

    function testFunction (executionContext,paramOne){ // paramOne will contain the field logical name (new_field)
    
    var formContext = executionContext.getFormContext();
    
    var lead = formContext.getAttribute("new_lead").getValue(); // Get the value of the lead lookup field
    
    if (lead !== null) { // Check if the lead lookup field contains data
    
    var leadId = lead[0].id.slice(1, -1); //Slice the lead ID into a usable format
    var paramOneValue = formContext.getAttribute(paramOne).getValue(); // Get the field value of the passed field name
    
    var entity = {};
    
    if (paramOne ==="new_field"){
    
    entity.new_field = paramOneValue; // This seems to work if I replace paramOne with the actual lead field logical name
    
    }
    
    else if (paramOne ==="new_fieldTwo"){
    
    entity.new_fieldTwo = paramOneValue; // And go on for different possible fields and places you are triggering this function from....
    
    }
    
    var req = new XMLHttpRequest();
    
    req.open("PATCH", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/leads(" leadId ")", true);
    
    req.setRequestHeader("OData-MaxVersion", "4.0");
    
    req.setRequestHeader("OData-Version", "4.0");
    
    req.setRequestHeader("Accept", "application/json");
    
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    
    req.onreadystatechange = function() {
    
    if (this.readyState === 4) {
    
    req.onreadystatechange = null;
    
    if (this.status === 204) {
    
    //Success
    
    } else {
    
    Xrm.Utility.alertDialog(this.statusText);
    
    }
    
    }
    
    };
    
    req.send(JSON.stringify(entity));
    
    }
    
    }

    Hope this will solve your query. Kindly mark this as verified answer if it does.

    As always feel free to reach out to me if more assistance is needed.

    Have a great day ahead.....

    Best Regards

    Abhishek Dhoriya

    Consultant @ Microsoft India

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans