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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

How can I hide a field in a Quick Create Form based on the number of records in a subgrid?

(0) ShareShare
ReportReport
Posted on by 183

Hello, 

I have 2 Forms (of 2 entities: Backlog & Pipeline). Each of them has a subgrid for a third Entity called Cashflow.

In the cashflow's quick create form, that is being launched from each subgrid, I am trying to hide a field called 'Year' if the subgrid contains 1 or more records already, else if it is the first cashflow being created, the user should be able to see the field called year. 

How can I accomplish that with JS?

I have the same question (0)
  • Verified answer
    Leah Ju Profile Picture
    Microsoft Employee on at

    Hi Joseph,

    You can't pass sub-grid records number to the quick create form, but you can retrieve related Cashflow number based on Backlog lookup value on the quick create form

    When you create new cashflow from backlog, you will see that the backlog lookup field has been populated automatically in quick create form:

    pastedimage1679381903907v1.png

    Then you can try to use the following example code:

    function hideYearChoiceInput(executionContext) {
    
        //Initiated Form Context.
        var formContext = executionContext.getFormContext();
    
        //get backlog Lookup value
        var backlog = formContext.getAttribute("crb1f_backlog").getValue();
    
        if (backlog != null) {
            var backlogId = backlog[0].id;
            Xrm.WebApi.retrieveMultipleRecords("crb1f_cashflow", "?$filter=_crb1f_backlog_value eq "   backlogId).then(
                function success(results) {
                    console.log(results);
                    var count = results.entities.length;
                    for (var i = 0; i < results.entities.length; i  ) {
                        var result = results.entities[i];
                        // Columns
                        var crb1f_cashflowid = result["crb1f_cashflowid"]; // Guid
                    }
                    //hide field
                    if (count == 0) {
                        formContext.getControl("crb1f_yearchoice").setVisible(true);
                    }
                    else {
                        formContext.getControl("crb1f_yearchoice").setVisible(false);
                    }
                },
                function (error) {
                    console.log(error.message);
                }
            );
        }
    }

    pastedimage1679385137268v5.png

    Test:

    1)Not First cashflow 

    pastedimage1679384954702v3.png

    2)First cashflow 

    pastedimage1679384994342v4.png

  • Joseph Nasr Profile Picture
    183 on at

    Hey Leah thank you for your reply! I am getting an error "A binary operator with incompatible types was detected. Found operand types 'Microsoft.Dynamics.CRM.new_backlog' and 'Edm.String' for operator kind 'Equal'."

    I guess it is related to the 'eq' in the filter as maybe it should only be used with numbers?

  • Joseph Nasr Profile Picture
    183 on at

    It was my bad in the code I had to use '_cra1c_backlog_cashflow_value' before the eq instead of 'cra1c_backlog_cashflow'. Now it works. Thanks Leah!

  • Joseph Nasr Profile Picture
    183 on at

    Hello Leah I was trying to achieve something else, that is instead of hiding the year when the Quick Create Form Loads, if it is not the first record being created, it sets the default selected value of the Year field to the next year.

    This means if for a backlog I have 3 cashflow records: 1 for 2020, 1 for 2021, and 1 for 2022, when I click 'Create New Cashflow' from the subgrid, it sets the Year field in the Quick Create Form to 2023 by default.

    function autoSelectNextYear(executionContext) {
    
        //Initiated Form Context.
        var formContext = executionContext.getFormContext();
    
        //get backlog Lookup value & pipeline Lookup value
        var backlog = formContext.getAttribute("cra1c_backlog_cashflow").getValue();
        var pipeline = formContext.getAttribute("cra1c_pipeline_cashflow").getValue();
    
        //Get Year Choice Options
        var optionSetValues = formContext.getAttribute("cra1c_yearchoice").getOptions();
    
        if (backlog != null && pipeline == null) {
            var backlogId = backlog[0].id;
            //Get all cashflows related to this backlog, and sort them by Year in descending order
            Xrm.WebApi.retrieveMultipleRecords("cra1c_mn_cashflow", "?$filter=_cra1c_backlog_cashflow_value eq "   backlogId   "&$orderby=cra1c_yearchoice desc").then(
                function success(results) {
                    var count = results.entities.length;
                    //If it's not the first record being entered
                    if (count != 0) {
                        //print count
                        console.log(count);
                        //Get the Next Year (1 after the latest Year entered) as a label
                        var nextYearAsInt = parseInt(results.entities[0]['cra1c_yearchoice@OData.Community.Display.V1.FormattedValue']) 1;
                        var nextYearAsString = nextYearAsInt.toString();
                        //Get the value of the fetched Next Year's Choice, and set it on the Year field
                        for (i = 0; i < optionSetValues.length; i  ) {
                            if (optionSetValues[i].text == nextYearAsString) {
                             formContext.getAttribute("cra1c_yearchoice").setValue(optionSetValues[i].value);
                            }
                        }
                    }
                    else {
                        formContext.getAttribute("cra1c_yearchoice").setValue(null);
                    }
                },
                function (error) {
                    console.log(error.message);
                }
            )
        }
        }

    I am using the code above but after saving and quitting the quick create form, the years are getting automatically incremented again and set to wrong values. It's obviously a logical error but any ideas on why it could be happening? 

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans