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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Javascript Function OnSave

(0) ShareShare
ReportReport
Posted on by 5

Hi,

I was hoping someone could help guide me with my first javascript for Dynamics 365. I'm just trying to create an override field from a option set that would go into a text field. The text field does have a business in place to put the values needed. However, I need to add an override function for supervisors in the case they need it. I'm not getting any errors but I'm not getting the field it needs to setValue to to update. Here's my code and how I have the script setup in the form builder:

function supervisorFlagOverrideChanged(executionContext) {
    var formContext = executionContext.getFormContext();

    var supervisorFlagOverrideField = formContext.getAttribute("cr93a_SupervisorFlagOverride");
    var contactFlagsField = formContext.getAttribute("cmcps_ApplicationFlags");
    var contactFlagsControl = formContext.getControl("cmcps_ApplicationFlags");

    if (supervisorFlagOverrideField != null) {
        var supervisorFlagOverrideValue = supervisorFlagOverrideField.getValue();

        // Log supervisorFlagOverrideValue to console
        console.log("supervisorFlagOverrideValue: ", supervisorFlagOverrideValue);

            contactFlagsControl.setDisabled(false);
            contactFlagsField.setValue(supervisorFlagOverrideValue);
            contactFlagsControl.setDisabled(true);

            // Save the form.
            formContext.data.entity.save();
    } else {
        console.log("supervisorFlagOverrideField is null.");
    }
}
Ps. I left my debug in the code hoping that would help.
I have the same question (0)
  • Gardagantxo2007 Profile Picture
    27 on at
    RE: Javascript Function OnSave

    Hi

    If I undertsand, you have an option set with a selected option. Do you need evaluate this option?

    In an option set getValue and getText returns number or text.

    Try var supervisorFlagOverrideValue = supervisorFlagOverrideField.getText();

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at
    RE: Javascript Function OnSave

    Hi,

    Why do you use the "onsave" event handler ?

    Could you try with "on colum change" event of cr93a_SupervisorFlagOverride ?

    Are you sure about case of your columns ? cr93a_supervisorflagoverride instead of cr93a_SupervisorFlagOverride ? You need the logical name, not the schema name.
    You can use Metadata Browser of XrmToolBox to help you.
    Example with accountnumber of Account entity.

    If you use "onsave" event, you don't need to call entity.save();

  • Gardagantxo2007 Profile Picture
    27 on at
    RE: Javascript Function OnSave

    Try to use the fields of the form directly.

    formContext.getControl("cmcps_ApplicationFlags").setDisabled(false);

    formContext.getAttribute("cmcps_ApplicationFlags").setValue(supervisorFlagOverrideValue);

    formContext.getControl("cmcps_ApplicationFlags").setDisabled(true);

    Remember to activate form context !! Obviously ;)

    cmcps_ApplicationFlags is an option set? If true, cmcps_ApplicationFlags options numeric value, have to be the same as cr93a_SupervisorFlagOverride numerics value.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 235

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 177

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 156 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans