Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Js Web Resource executes 2 times

Posted on by 295

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

  • 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

  • RE: Js Web Resource executes 2 times

    yes it executed 1 time after commenting all code

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 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);


  • RE: Js Web Resource executes 2 times

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

  • 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
    gdas 50,085 on at
    RE: Js Web Resource executes 2 times

    Try to remove below line  -

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

  • 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
    gdas 50,085 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)
  • 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
    gdas 50,085 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans