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

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Js Web Resource executes 2 times

    Bro actually i created one more JS web resource in which i wanted to insert that code in which BPF change on form load but that web resource do not execute when form load so it cut code from that web resource and injected this web resource in which button disable on certain stage

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Js Web Resource executes 2 times

    yes it executed 1 time after commenting all code

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Js Web Resource executes 2 times

    Just for trial to  check whether this is the root cause or not . In addition you can comment out  all the code inside the function and check how many time its executing. 

    // 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
    RE: Js Web Resource executes 2 times

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

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Js Web Resource executes 2 times

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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Js Web Resource executes 2 times

    Try to remove below line  -

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

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Js Web Resource executes 2 times

    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
    RE: Js Web Resource executes 2 times

    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
    RE: Js Web Resource executes 2 times

    // 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);

  • gdas Profile Picture
    50,091 Moderator on at
    RE: Js Web Resource executes 2 times

    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 .

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans