web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

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

Help with Javascript

(0) ShareShare
ReportReport
Posted on by 2,171

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 

pastedimage1575526254212v1.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?

I have the same question (0)
  • Suggested answer
    Abhishek Dhoriya Profile Picture
    1,013 on at
    RE: Help with Passing a field name parameter and using it to update another entity field

    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

  • Suggested answer
    Abhishek Dhoriya Profile Picture
    1,013 on at
    RE: Help with Passing a field name parameter and using it to update another entity field

    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
    1,013 on at
    RE: Help with Passing a field name parameter and using it to update another entity field

    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

  • Suggested answer
    Irena Benja Profile Picture
    414 on at
    RE: Help with Passing a field name parameter and using it to update another entity field

    Hallo Mike,

    Instead of just writing the name of your field, try on the parameters input on the form:

    myVar="new_field"

    Hope it helps!

  • Suggested answer
    Irena Benja Profile Picture
    414 on at
    RE: Help with Passing a field name parameter and using it to update another entity field

    Hallo Mike,

    Instead of just writing the name of your field, try on the parameters input on the form:

    myVar="new_field"

    Hope it helps!

  • MikeC282 Profile Picture
    2,171 on at
    Help with Passing a field name parameter and using it to update another entity field

    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 

    Pass-field-as-parameter.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?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 179

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 129 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans