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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Script error for switching forms based on lookup field value

(1) ShareShare
ReportReport
Posted on by 46
Hi all,
 
We currently have three different forms that we use for the opportunity. The correct form that should show is based on the business unit the opportunity is linked to. For example, if the opportunity is linked to Business Unit X or Y then form 1 should be used and if it is linked to Business Unit Q or Z then form 2 should be used.
 
To help endusers and minimize any confusions I want the correct form to show up automatically based on the related Business Unit, so users don't have to pick the correct form themselves manually.
 
To achieve this I have created the following jscript. However it is giving an error when loading the opportunity form: "targetForm.navigate is not a function"Could anybody help me with this?
 
Here is the script:
 
function setOpportunityFormType(executionContext) {
    var formContext = executionContext.getFormContext();
    
    //Determine Form currently loaded. If null returned, it means only one form is available. Just exit. 
    //SRC: https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/formContext-ui-formSelector
    var selectedFormReference = formContext.ui.formSelector.getCurrentItem();
    if (!selectedFormReference) {
        console.log("setOpportunityFormType: No additional forms are available for switching.");
        return;
    }
    console.log("setOpportunityFormType:SelectedForm:" + selectedFormReference.getLabel());
    //Get Business Unit Value
    var BUvalue = formContext.getAttribute("owningbusinessunit").getValue();
    if (!BUvalue) {
        console.log("setOpportunityFormType: Business Unit field missing or null.");
        return;
    }
    console.log("setOpportunityFormType:BUvalue:" + BUvalue);
    //Define the form dictionary mapping Business Unit values to the appropriate form guid.
    formDict = {
        "8841ae65-bb3d-ee11-bdf4-000d3ab69899": "1a89ad94-4210-ee11-8f6d-0022489fdf9a",
        "72a22850-0eef-ed11-8848-0022489fdf9a": "1a89ad94-4210-ee11-8f6d-0022489fdf9a",
        "87c1ee29-0eef-ed11-8848-0022489fdf9a": "1a89ad94-4210-ee11-8f6d-0022489fdf9a",
        "2137a55f-bb3d-ee11-bdf4-000d3ab69899": "595978a6-704c-4aec-aab8-34f3927c1cda",
        "5b4644d6-0def-ed11-8848-0022489fdf9a": "595978a6-704c-4aec-aab8-34f3927c1cda",
        "02a26dfb-0def-ed11-8848-0022489fdf9a": "595978a6-704c-4aec-aab8-34f3927c1cda",
        "cac2b40f-0eef-ed11-8848-0022489fdf9a": "595978a6-704c-4aec-aab8-34f3927c1cda",
        "dd9ba66b-bb3d-ee11-bdf4-000d3ab69899": "e277e15f-088e-ee11-8179-000d3ab76a71",
        "d389397c-a23d-ee11-bdf4-000d3ab69899": "e277e15f-088e-ee11-8179-000d3ab76a71",
        "5921c6c4-a23d-ee11-bdf4-000d3ab69899": "e277e15f-088e-ee11-8179-000d3ab76a71"
    };
    // Determine if Selected Form is correct, and if not, load the correct form.
    var selectedFormId = selectedFormReference.getId();
    if (selectedFormId === formDict[BUvalue]) {
        console.log("setOpportunityFormType:Correct Form Already Loaded.");
    }
    else if (selectedFormId !== formDict[BUvalue]) {
        var targetForm = formContext.ui.formSelector.items.get(formDict[BUvalue]);
        // if TartForm available in formSet attempt to navigate to it. 
        if (targetForm === null) {
            //alert("setOpportunityFormType: Automatic form selector unable to load correct form for this Business Unit.  Please report this to the IT Support Team. TargetForm for this Business Unit is not available. Some fields and system features may not be available if using the current form.");
            var alertStrings = {
                confirmButtonLabel: "I understand.",
                text: "Unable to load correct form for this Business Unit. Some fields and system features may not be available. Please report this to the IT Support Team."
            };
            var alertOptions = {
                height: 250,
                width: 300
            };
            Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
        }
        else {
            //If form is dirty, save then navigate. 
            if (formContext.data.entity.getIsDirty()) {
                console.log("setOpportunityFormType:Incorrect Form Loaded. Fields dirty. Saving and Switching Forms.");
                formContext.data.save().then(function () { targetForm.navigate(); });
            }
            //If Clean, just navigate.
            else {
                console.log("setOpportunityFormType:Incorrect Form Loaded. Fields clean. Switching Forms.");
                targetForm.navigate();
            }
        }
    }
    else {
        console.log("setOpportunityFormType:Error determining if correct form is loaded.");
    }
}
 
Here "8841ae65-bb3d-ee11-bdf4-000d3ab69899" is the GUID of a specific Business Unit, and "1a89ad94-4210-ee11-8f6d-0022489fdf9a" is the id of the form I want to navigate to for this specific BU.
 
Any help would be much appreciated!
I have the same question (0)

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

News and Announcements

Season of Giving Solutions is Here!

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
iampranjal Profile Picture

iampranjal 66

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 51 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 40 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans