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 :
Microsoft Dynamics CRM (Archived)

Javascript, run code after save

(0) ShareShare
ReportReport
Posted on by 600

Hi!

We have the following code that runs when we click a button: 

function followUpTask(duration) {
debugger;
//check for not saved form

if (Xrm.Page.ui.getFormType() == 1 && followup == false) {
Xrm.Page.data.entity.save(true);
followupduration = duration;
followup = true;
return;
}
else if (Xrm.Page.data.entity.getIsDirty()) {

Xrm.Page.data.entity.save(true);
followupduration = duration;
followup = true;
return;
}

If the else if (Xrm.Page.data.entity.getIsDirty()) statement is true the form saves but following code

followupduration = duration;
followup = true;

will not run... We have to click the button once more for the code to run. Is there a way we can change this so the code runs after the form is saved?

Thanks for help! 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Javascript, run code after save

    Hi ,

    When you write code inside (form type ==1 ) that means piece of code will be executed at the time of load of create form. Once you saved that means the create form will convert as a update form . So you need to put (form type ==2 ) in the condition statement .In that case the code will every-time on onload of any created record / modify record.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Javascript, run code after save

    Hi Martin ,

    You can create two function one is for Button Click and another is for onload when record being saved and load update form. Here is sample -

            // Fire this function in the onload , Make sure you define/assign  the variable value inside the function
            function followUpTask(duration) {
                debugger;
                //check for not saved form
                if (Xrm.Page.ui.getFormType() == 1 && followup == false) { // Button click it will check if form tyoe is Create
                    Xrm.Page.data.save().then(successSubmitCallback(), errorSubmitCallback()); // Fire success call back once save
                    return;
                }
            }
    
            function successSubmitCallback() {
                followupduration = duration;
                followup = true;
            }
    
            function errorSubmitCallback() {
                // Handle error for any type of issue if onsave fails
            }
    
    
            //Trigger this function in the form onlaod , Make sure you define the variable value inside the function
            function OnloadForm() {
                if (Xrm.Page.ui.getFormType() == 2) { // Check when form is update form
                    followupduration = duration;
                    followup = true;
                }
            }


  • Suggested answer
    Iron Arm Profile Picture
    131 on at
    Javascript, run code after save
    Use addMessageToOnPostSave
     
    formContext.data.entity.addOnPostSave(myFunction);
     

    The flaw with the other answers talking about formType =1 on create, and then it's formType = 2 on the subsequent onload. Yeah, that can work in some scenarios.  But what if they hit Save & Close?  Hmmm!  Anyway, now we have addMessageToOnPostSave, so it doesn't matter. 

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans