Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / How can I hide a field...
Sales forum
Answered

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

Posted on by 146

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?

Categories:
  • Joseph Nasr Profile Picture
    Joseph Nasr 146 on at
    RE: How can I hide a field in a Quick Create Form based on the number of records in a subgrid?

    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? 

  • Joseph Nasr Profile Picture
    Joseph Nasr 146 on at
    RE: How can I hide a field in a Quick Create Form based on the number of records in a subgrid?

    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
    Joseph Nasr 146 on at
    RE: How can I hide a field in a Quick Create Form based on the number of records in a subgrid?

    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?

  • Verified answer
    Leah Ju Profile Picture
    Leah Ju Microsoft Employee on at
    RE: How can I hide a field in a Quick Create Form based on the number of records in a subgrid?

    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

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,339 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,177 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans