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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Active Discussion

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:

}
}

  • 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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Hamza H Profile Picture

Hamza H 142 Super User 2026 Season 1

#2
Nagaraju_Matta Profile Picture

Nagaraju_Matta 128

#3
ManoVerse Profile Picture

ManoVerse 127 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans