Skip to main content

Notifications

Announcements

No record found.

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

Customize Opportunity Close based on Opportunity field

(1) ShareShare
ReportReport
Posted on by 4
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,699 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans