Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Dynamically Pass a attribute/field logical name to a JavaScript resource onchange of that attribute/field

Posted on by 2,095

Hi all,

Just a quick one. I've got a JavaScript web resource that updates fields of a related entity record from a lookup field on the primary entity record. There are 2 fields that need updating/syncing across the related entity records.

I've then set the web resource to load on change of each of the 2 fields on the primary entity record. The problem is that how I've got the javascript set is that it will update both fields everytime a single field is changed as the script statically sets the attribute values.

What I thought about is whether there's a way to dynamically pass the attribute name as a variable so that when that attribute changes value I only need to update the one field that's changed as opposed to both of them everytime.

As an example of the below code:

function attributeTest (executionContext){
var formContext = executionContext.getFormContext();
// You can see I'm statically getting the attribute one value by inputting the logical name of that attribute
var attributeOneValue = formContext.getAttribute("new_attributeone").getValue();
// I want to see if it's possible to pass the "new_attributeone" logical name as a parameter or variable so that I can dynamically get values of the field that's being changed.
}

Kind regards,

Mike

  • Suggested answer
    Roma Gupta Profile Picture
    Roma Gupta 725 on at
    RE: Dynamically Pass a attribute/field logical name to a JavaScript resource onchange of that attribute/field

    Hi Mike

    You can also set a flag in case you don't want to provide any extra parameter but that will increase checks & line of code.

    Regards

    Roma

  • Verified answer
    Abhishek Dhoriya Profile Picture
    Abhishek Dhoriya 1,011 on at
    RE: Dynamically Pass a attribute/field logical name to a JavaScript resource onchange of that attribute/field

    Hi Mike,

    This is can be achieved via. simple configuration and few code changes as mentioned below.

    Step 1: Update your Function to revive one more parameter apart from executionContext like below.

    function attributeTest (executionContext, paramOne){
    
    var formContext = executionContext.getFormContext();
    // You notice we have added another parameter to function, we are using below
    
    var attributeOneValue = formContext.getAttribute(paramOne).getValue();
    
    }

    Step 2) Pass that respective dynamics parameter Value Dynamically from Front End

    while calling this method from different fields . In this case it is the attribute name it self like new_attributeone and new_attributetwo

    2019_2D00_12_2D00_04_5F00_16_2D00_17_2D00_20.png

    I hope this answers your query, if you find this useful mark this reply as Verified Answer.

    And always feel free to post back if more assistance is needed.

    Regards,

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans