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

js code on dynamics 365 form works if i use Xrm.Page.getAttribute but since it is deprecated i used formContext.getAttribute and it is not working, it will work only if add in Onsave of the form, which i don't want, because i want to update the comman

(0) ShareShare
ReportReport
Posted on by

i want to update a field when users click on custom button and i have a below js code on dynamics 365 form works if i use Xrm.Page.getAttribute but since it is deprecated i used formContext.getAttribute and it is not working,formContext.getAttribute   it will work only if  add in Onsave or onloadof the form, which i don't want, because i want to update the command when users clicks on particular button and not when form saves

below code works perfectly [ cant use because it is deprecated]

setFieldValue: function () {
Xrm.Page.getAttribute("azmdc_assessmentsubmited").setValue("Assessment Submitted button clicked");

this code will work only if i add to onsave or onload , not on custom ribbon button.

setFieldValue: function (executionContext) {
var formContext = executionContext.getFormContext();
formContext.getAttribute("azmdc_assessmentsubmited").setValue("Assessment Submitted button clicked");
}

pastedimage1638423586119v1.png

I have the same question (0)
  • Verified answer
    meelamri Profile Picture
    13,216 User Group Leader on at
    RE: js code on dynamics 365 form works if i use Xrm.Page.getAttribute but since it is deprecated i used formContext.getAttribute and it is not working, it will work only if add in Onsave of the form, which i don't want, because i want to update the co

    Hi, 

    You need to use primaryControl as formContext 

    setFieldValue: function (primaryControl) {
    var formContext = primaryControl;
    formContext.getAttribute("azmdc_assessmentsubmited").setValue("Assessment Submitted button clicked");
    }

    Good Luck !

  • Community Member Profile Picture
    on at
    RE: js code on dynamics 365 form works if i use Xrm.Page.getAttribute but since it is deprecated i used formContext.getAttribute and it is not working, it will work only if add in Onsave of the form, which i don't want, because i want to update the co

    Hello Mehdi,

    The above code worked perfectly .

    I also had alert function in the onsave of the form and it was working fine, but after adding to the ribbon button, the alert will pop up even if required fields are not filled, if i click on button and if req field is not filled, still the alert will pop up? do i need to modify my below code? 

    [also created new post for this - I had alert function in the onsave of the form and it was working fine, but after adding to the ribbon button, the alert will pop up even if required fields are not filled - Microsoft Dynamics CRM Forum Community Forum]

    setFieldValue: function (primaryControl) {

      var formContext = primaryControl;

      var formContext = primaryControl.getFormContext();    //before i used  executioncontext.getFormContext();

      formContext.getAttribute("assessmentsubmited").setValue("Assessment Submitted button clicked");

      var alertStrings = { confirmButtonLabel: "", text: "You Request has been successfully raised !!", title: "Confirmation" };

      var alertOptions = { height: 120, width: 260 };

       if (formContext !== null) {

          Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);

       }

    }

  • meelamri Profile Picture
    13,216 User Group Leader on at
    RE: js code on dynamics 365 form works if i use Xrm.Page.getAttribute but since it is deprecated i used formContext.getAttribute and it is not working, it will work only if add in Onsave of the form, which i don't want, because i want to update the co

    I'm sorry, I can't understand. Did you register the same function in the onSave event?

  • meelamri Profile Picture
    13,216 User Group Leader on at
    RE: js code on dynamics 365 form works if i use Xrm.Page.getAttribute but since it is deprecated i used formContext.getAttribute and it is not working, it will work only if add in Onsave of the form, which i don't want, because i want to update the co

    I notice that you have created a new thread regarding the new issue. Could you close this thread by checking my answer? thanks !

  • Community Member Profile Picture
    on at
    RE: js code on dynamics 365 form works if i use Xrm.Page.getAttribute but since it is deprecated i used formContext.getAttribute and it is not working, it will work only if add in Onsave of the form, which i don't want, because i want to update the co

    when i used in onsave event it was working but used executioncontext.getFormContext();

    but now i removed from onsave and added directly to the custom ribbon button and alert will pop up but if  I dont fill the req field and Click on button, alert will still pop up which i dont want

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 235

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 177

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 156 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans