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 :
Customer experience | Sales, Customer Insights,...
Unanswered

Business process flow using javascript?

(0) ShareShare
ReportReport
Posted on by 5

I am tracking the stages of Business process flow and setting the field values,

convert this to Form context format;

function formonload() { 


Xrm.Page.data.process.addOnStageChange(getStage);  // Trigger the function when move to next stage.


getStage();
}
function getStage() {


var activeStage = Xrm.Page.data.process.getActiveStage();


var stagename = activeStage.getName();


switch (stagename) {


case "Stage1": // Stage of business process flow
Xrm.Page.getAttribute("field_1").setValue(1);
break;


case "Stage2": //Stage of business process flow
Xrm.Page.getAttribute("field_1").setValue(2);
break;
default:

}
}

I have the same question (0)
  • Dynamic fresher Profile Picture
    5 on at

    help me convert this program from old XRM form to new Formcontext format

  • Pulkit Srivastava Profile Picture
    170 on at

    Hi,

    Please find the code below,

    function BusinessProcessFlowScript(executionContext) {
        try {
            var formContext = executionContext.getFormContext();
            var activeStage = formContext.data.process.getActiveStage();
            if (activeStage !== null && typeof (activeStage) !== "undefined") {
                var stageName = activeStage._stageStep.description;
                switch (stageName) {
                    case "Stage1":
                        formContext.getAttribute("field_1").setValue(1);
                        break;
                    case "Stage2":
                        formContext.getAttribute("field_2").setValue(2);
                        break;
                    default:
                }
            }
        } catch (ex) {
            Xrm.Navigation.openAlertDialog(ex.message);
        }
    }

  • Dynamic fresher Profile Picture
    5 on at

    hi,

    i need this process to happen when i move onto next stage of business process flow also.

    your code runs only once since i registered onload of form.

    please guide me on how to call this function when i move to next subsequent stages of business process flow.

  • Pulkit Srivastava Profile Picture
    170 on at

    Hi,

    Please use formContext.data.process.addOnStageChange(function name);

    add this in the onload event the above metioned funtion should be added here in the function name.

    If you need more help please let me know i can send you the code.

    Thanks

  • Dynamic fresher Profile Picture
    5 on at

    function formonload(e) {

    var formContext = e.getFormContext;

    formcontext.data.process.addOnStageChange(BusinessProcessFlowScript);

    }

    function BusinessProcessFlowScript() {

       try {

           var formContext = executionContext.getFormContext();

           var activeStage = formContext.data.process.getActiveStage();

           if (activeStage !== null && typeof (activeStage) !== "undefined") {

               var stageName = activeStage._stageStep.description;

               switch (stageName) {

                   case "Stage1":

                       formContext.getAttribute("field_1").setValue(1);

                       break;

                   case "Stage2":

                       formContext.getAttribute("field_2").setValue(2);

                       break;

                   default:

               }

           }

       } catch (ex) {

           Xrm.Navigation.openAlertDialog(ex.message);

       }

    }

    is this how i should do? i am registering formonload function on Onload event of form.

  • Pulkit Srivastava Profile Picture
    170 on at

    yes

  • Dynamic fresher Profile Picture
    5 on at

    i am using the above code and i am getting error as:

    pastedimage1647504644307v1.png

  • Pulkit Srivastava Profile Picture
    170 on at

    Hi,

    this is working code

    function OnloadOfForm(executionContext) {
        try {
            var formContext = executionContext.getFormContext();
            formContext.data.process.addOnStageChange(function () {
                BusinessProcessFlowScript(formContext);
            });
        } catch (ex) {
            Xrm.Navigation.openAlertDialog(ex.message);
        }
    }
    
    function BusinessProcessFlowScript(formContext) {
        try {
            var activeStage = formContext.data.process.getActiveStage();
            if (activeStage !== null && typeof (activeStage) !== "undefined") {
                var stageName = activeStage._stageStep.description;
                switch (stageName) {
                    case "Stage1":
                        formContext.getAttribute("field_1").setValue(1);
                        break;
                    case "Stage2":
                        formContext.getAttribute("field_2").setValue(2);
                        break;
                    default:
                }
            }
        } catch (ex) {
            Xrm.Navigation.openAlertDialog(ex.message);
        }
    }

    If this works for you then please mark as verified 

  • Dynamic fresher Profile Picture
    5 on at

    sorry, but i am getting same error . i am saving Function "OnloadForm" on the onload event of form but still what is wrong i dont know.

  • Pulkit Srivastava Profile Picture
    170 on at

    Hi

    Please share the error log.

    Thanks

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 171 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 83

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans