Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
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

Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee 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

  • meelamri Profile Picture
    meelamri 13,198 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 !

  • meelamri Profile Picture
    meelamri 13,198 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?

  • Community Member Profile Picture
    Community Member Microsoft Employee 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);

       }

    }

  • Verified answer
    meelamri Profile Picture
    meelamri 13,198 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 !

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,645 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans