Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Customize Opportunity Close based on Opportunity field

(1) ShareShare
ReportReport
Posted on by 10
I have a custom field on the Opportunity called Opportunity Type. This is a single select option set field. I would like to customize the Opportunity Close to hide specific fields based on the Opportunity Type selected on the Opportunity. 
 
I tried doing this via a business rule but I can't select an Opportunity field on the condition because the business rule is being created on the Opportunity Close entity. 
 
Any ideas? 
  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,941 Super User 2024 Season 1 on at
    Customize Opportunity Close based on Opportunity field
    If we don't need any input from user and have to perform the steps in background then we can use PA.
     
    But here we need inputs from user, so we can not use it in this case.
  • KA-21111713-0 Profile Picture
    KA-21111713-0 10 on at
    Customize Opportunity Close based on Opportunity field
    Thank you, Amit. How can this be done using Power Automate? I see you mentioned that in your answer but your description only includes Javascript. 
  • Verified answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,941 Super User 2024 Season 1 on at
    Customize Opportunity Close based on Opportunity field
    The challenge here is that the Opportunity Close entity is separate from the Opportunity entity, and business rules created on the Opportunity Close entity cannot directly access fields from the related Opportunity. However, you can achieve your goal using JavaScript or Power Automate. Here's how:

    Use JavaScript

    You can write a JavaScript function to dynamically hide specific fields on the Opportunity Close form based on the Opportunity Type selected in the related Opportunity.

    Steps:

    1. Add JavaScript Library to the Opportunity Close Form
    Go to the Opportunity Close form in Dynamics 365.
    Add a JavaScript web resource to the form.


    2. Write JavaScript Code Use the following script to fetch the Opportunity Type from the related Opportunity and hide fields accordingly:

    function onFormLoad(executionContext) {
        var formContext = executionContext.getFormContext();
        var opportunityId = formContext.getAttribute("opportunityid").getValue();

        if (opportunityId) {
            // Get the Opportunity ID
            var opportunityGuid = opportunityId[0].id.replace("{", "").replace("}", "");

            // Call the Web API to retrieve the Opportunity Type
            Xrm.WebApi.retrieveRecord("opportunity", opportunityGuid, "?$select=opportunitytype").then(
                function (result) {
                    var opportunityType = result.opportunitytype; // Replace with your field's logical name

                    // Example: Hide a field based on Opportunity Type
                    if (opportunityType === 123456) { // Replace 123456 with the option set value
                        formContext.getControl("field_to_hide").setVisible(false);
                    } else {
                        formContext.getControl("field_to_hide").setVisible(true);
                    }
                },
                function (error) {
                    console.error("Error retrieving Opportunity: " + error.message);
                }
            );
        }
    }


    3. Bind the Script to the Form Load Event
    In the Opportunity Close form editor, bind the function onFormLoad to the form’s OnLoad event.


    4. Publish the Customizations
    Save and publish your changes.

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! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans