Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Form Switch - Opportunity Record

(0) ShareShare
ReportReport
Posted on by 123

Hi,

I am struggling to find a straight forward JavaScript to do a form switch when using an option set.

I have two opportunity forms - Direct Sale (70BFE66D-3235-4A2C-BDF7-154F3F93ED35) and Indirect Sale (FAB5966D-5748-EC11-8C62-00224800AEB0)

I have a option set field with Direct (519790000) and Indirect (519790001) values.

Can someone share a script that I can use onload and onchange.

Thanks

Damesh

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,077 Super User 2024 Season 1 on at
    RE: Form Switch - Opportunity Record

    Hi Damesh Peri,

    Here you are: 

    // A namespace defined for the sample code
    // As a best practice, you should always define
    // a unique namespace for your libraries
    var Test = window.Test || {};
    (function () {
    // Define some global variables
    let controls = {
    aw_choice: "aw_choice"
    };
    let aw_choiceValue = {
    ONE: 809790000,
    TWO: 809790001,
    THREE: 809790002
    };

    // Code to run in the form OnLoad event
    this.formOnLoad = function (executionContext) {
    try {
    let formContext = "";
    let choiceValue = "";
    let formItem = "";
    let entityFormOptions = {
    entityName: "maa_test",
    formId: "",
    entityId: ""
    };

    formContext = executionContext.getFormContext();
    formItem = formContext.ui.formSelector.getCurrentItem();
    choiceValue = this.getValue(formContext, controls.maa_choice);
    entityFormOptions.entityId = this.getId(formContext);

    if (choiceValue === maa_choiceValue.ONE) {
    entityFormOptions.formId = "00a54fa4-4251-4cec-9419-289fd6673005";
    }
    else if (choiceValue === maa_choiceValue.TWO) {
    entityFormOptions.formId = "61433d49-9091-ed11-aad0-00224814a850";
    }
    else if (choiceValue === maa_choiceValue.THREE) {
    entityFormOptions.formId = "3bfcfab1-9091-ed11-aad1-00224818a74c";
    }
    else {
    entityFormOptions.formId = "00a54fa4-4251-4cec-9419-289fd6673005";
    }
    if (formItem.getId() !== entityFormOptions.formId) {
    Xrm.Navigation.openForm(entityFormOptions).then();
    }

    } catch (e) {
    this.openAlertDialog("Error from formOnLoad: " + "e: " + e + ". e.mesage: " + e.message);
    }
    }

    this.getId = function (formContext) {
    let iD = "";
    try {
    iD = formContext.data.entity.getId();
    } catch (e) {
    this.openAlertDialog("Error from getId: " + "e: " + e + ". e.mesage: " + e.message);
    }
    return iD;
    }

    this.getValue = function (formContext, attribute) {
    let value = "";
    try {
    if (this.getControl(formContext, attribute) != "") {
    value = formContext.getAttribute(attribute).getValue();
    }
    } catch (e) {
    this.openAlertDialog("Error from getValue: " + "e: " + e + ". e.mesage: " + e.message);
    }
    return value;
    }

    this.getControl = function (formContext, control) {
    let controlValue = ""
    try {
    if (formContext.getControl(control)) {
    controlValue = formContext.getControl(control);
    }
    } catch (e) {
    this.openAlertDialog("Error from getControl: " + "e: " + e + ". e.mesage: " + e.message);
    }
    return controlValue
    }

    this.openAlertDialog = function (alertStrings) {
    try {
    Xrm.Navigation.openAlertDialog(alertStrings).then();
    } catch (e) {
    Xrm.Navigation.openAlertDialog("Error from openAlertDialog: " + "e: " + e + ". e.mesage: " + e.message).then();
    }
    }
    }).call(Test);

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Form Switch - Opportunity Record

    Hello Damesh,

    Check this - gist.github.com/.../204d031d95090b469465d305828f1c8c it should address your scenario.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,605 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans