web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Js Web Resource executes 2 times

(0) ShareShare
ReportReport
Posted on by 297

Hello Everyone!

Hope all of you are happy. Can anyone tell me why my js web resource runs 2 times when form load? Function will have to trigger when form load.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    May be form get refresh on load. Please check it.

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    I am not using refresh method in script

  • Anas Rafik Profile Picture
    365 on at

    can you please post your code ?

  • gdas Profile Picture
    50,091 Moderator on at

    This is not a expected behavior . where did you register your function?  is it OOB on-load event ?

    Please share your code and screenshot here .

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    // A namespace defined for the sample code

    // As a best practice, you should always define

    // a unique namespace for your libraries

    var Sdk = window.Sdk || {};

    (function () {

    // Code to run in the form OnLoad event

    this.formOnLoad = function (executionContext) {

    console.log("disable button web resource");

    var formContext = executionContext.getFormContext();

                           var selectedStage = formContext.data.process.getSelectedStage();

    var selectedStageName  = selectedStage.getName();

           if (selectedStageName == "RF Review")

    {

    parent.document.getElementById("stageAdvanceActionContainer").style.display = "none";

    parent.document.getElementById("stageBackActionContainer").style.display = "none";

    }  

                          console.log("after getting form context");

                         var processObj = formContext.data.process.getActiveProcess();

                          var processName = processObj.getName();

                          console.log("after getting process name");

                          console.log(processName);

                         console.log("before if");

    if(processName == "Case to Work Order Business Process")

    {

    formContext.data.process.setActiveProcess("f51baaeb-f184-4ffa-bf98-19c2891afa67",callBackFunction);

    }

                          console.log("after assigning id");

                        formContext.data.entity.save();

                        console.log("after save");

    }

    function callBackFunction(result){

    }

    }).call(Sdk);

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    This is because you are calling formContext.data.entity.save(); method inside the onload  function. When you are doing Save in onload which is calling Onload functionits expected form will reload again and thats why its getting fired  two time .

    when you save records that means page will always reload and call all the onload function. For the first time Onload will call and second time when page get saved.

    // A namespace defined for the sample code
    
    // As a best practice, you should always define
    
    // a unique namespace for your libraries
    
    var Sdk = window.Sdk || {};
    
    (function () {
    
        // Code to run in the form OnLoad event
    
        this.formOnLoad = function (executionContext) {
    
            console.log("disable button web resource");
    
            var formContext = executionContext.getFormContext();
    
            var selectedStage = formContext.data.process.getSelectedStage();
    
            var selectedStageName  = selectedStage.getName();
    
            if (selectedStageName == "RF Review")
    
            {
    
                parent.document.getElementById("stageAdvanceActionContainer").style.display = "none";
    
                parent.document.getElementById("stageBackActionContainer").style.display = "none";
    
            }  
    
            console.log("after getting form context");
    
            var processObj = formContext.data.process.getActiveProcess();
    
            var processName = processObj.getName();
    
            console.log("after getting process name");
    
            console.log(processName);
    
            console.log("before if");
    
            if(processName == "Case to Work Order Business Process")
    
            {
    
                formContext.data.process.setActiveProcess("f51baaeb-f184-4ffa-bf98-19c2891afa67",callBackFunction);
    
            }
    
            console.log("after assigning id");
    
            formContext.data.entity.save();
    
            console.log("after save");
    
        }
    
        function callBackFunction(result){
    
        }
    
    }).call(Sdk)
  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    After removing this problem is same issue not resolved. It is executing 2 times again.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Try to remove below line  -

    formContext.data.process.setActiveProcess("f51baaeb-f184-4ffa-bf98-19c2891afa67",callBackFunction);

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    but i cannot do this because i have to switch process on form load

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    and as i remember before including this line issue js was executing 2 times

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans