Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Triggering a plugin from a JS onSave

(0) ShareShare
ReportReport
Posted on by 8

I have two fields on the Contact entity, let's call them new_b_main and new_b_sub, both of type Boolean.

My JS is simple - when new_b_main changes from false to true , new_b_sub should also switch to true (even if new_b_main switched back to false, new_b_sub should remain true)

My JS code:

function onSave(ExecutionContext)
{
  if(Xrm.Page.getAttribute("new_b_main").getValue() && Xrm.Page.getAttribute("new_b_main").getIsDirty())
    Xrm.Page.getAttribute("new_b_sub").setValue(true);
}

I also have a plugin step that runs of an Update of Contact (Pre-Operation) with a filtering attributes of new_b_sub.

The plugin should check if new_b_sub is true and if so, get the governmentid of the Contact (via Pre-Image), encode it to Base64 and store it in some other field.

The code for the Plugin is as follows:

// Runs of Pre-Operation

private void EncodeToBase64(Entity target, string governmentId)
{
  if(target.Contains("new_b_sub") && target["new_b_sub"] != null && (bool)target["new_b_sub"])
  {
    string base64 = string.Empty; // encode governmentid to Base64 here ....
    
    target["new_stored_base64_field"] = base64;
  }
}

Now, when I change new_b_main for the first time, new_b_sub is changed to true , as expected. I save the record , but the Base64 field remains empty. However, if I then change new_b_sub to false, save the record, and then change it to true again and save the record, I do see the Base64 field being populated.

What could be the cause for the Base64 field being populated only from second update?

  • Kuku17 Profile Picture
    8 on at
    RE: Triggering a plugin from a JS onSave

    Apparently the issue was that on Create of Contact record, new_b_sub was changed to true. Which is why new_b_main had no effect the first time it changed to true. 

    The getIsDirty has been evaluated to true on Create.

    I wonder why Dynamics would think new_b_main had been changed even though it hadn't.

  • Suggested answer
    a33ik Profile Picture
    84,329 Most Valuable Professional on at
    RE: Triggering a plugin from a JS onSave

    Hello,

    Try to use the following addition to your code:

    function onSave(ExecutionContext)

    {

     if(Xrm.Page.getAttribute("new_b_main").getValue() && Xrm.Page.getAttribute("new_b_main").getIsDirty())

       Xrm.Page.getAttribute("new_b_sub").setValue(true);

       Xrm.Page.getAttribute("new_b_sub").setSubmitMode("always");

    }

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... 293,097 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,872 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans