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,...
Unanswered

Subgrid addOnload Function

(1) ShareShare
ReportReport
Posted on by
Hi Folks,
 
I have the one subgrid (accounts) and lookup (accounts) as below. My requirement is as below
1. if there is only one record in the Subgrid, I should set that record as a main funder as per the screenshot 1
2. if there are more than one records, user should choose the value as per the screenshot 2. So I was able to set the Main funder from the Main funder List. 
Note - Main Funders drop down is filtered with the values in the Funders Grid. 
 
if the user intially add one funder, the Main funder is defaulted to the one and only funder in the list, when they add the second one in the funders list, Im clearing the value with null. 
 
My JS script is as below
 
var onLoad = function (executionContext) {
    var formContext = executionContext.getFormContext();
    getFunders(executionContext);
    let fundersSubgrid = formContext.getControl(/FundersSubGrid/);
    if (fundersSubgrid != null) {
        let rowFundersCount = fundersSubgrid.getGrid().getTotalRecordCount();
        
        fundersSubgrid.addOnLoad(function () {
            
            getFunders(executionContext);
            setControlDisabled(formContext, /svdp_mainfunder/, false);
            var gridColumns = [];
            let currentFundersRowCount = fundersSubgrid.getGrid().getTotalRecordCount();
            let gridRows = fundersSubgrid.getGrid().getRows();
            //loop through each row to get values of each column
            if (gridRows.getLength() > 0) {
                gridRows.forEach(function (row, i) {
                    gridColumns = row.getAttribute();                    
                    if (currentFundersRowCount === 1) {
                        gridColumns.forEach(function (column, j) {
                            if (column.getName() == /name/) {
                                setControlDisabled(formContext, /svdp_mainfunder/, true);
                                setAttributeRequiredLevel(formContext, /svdp_mainfunder/, /required/);
                                setLookupValue(formContext, /svdp_mainfunder/, row.getData().getEntity().getEntityName(), row.getData().getEntity().getId(), column.getValue());
                            }
                        });
                    } else if (currentFundersRowCount === 0) {
                        setAttributeRequiredLevel(formContext, /svdp_mainfunder/, /none/);
                        setAttribute(formContext, /svdp_mainfunder/, null);
                    } else if (currentFundersRowCount > 1) {
                        setAttributeRequiredLevel(formContext, /svdp_mainfunder/, /none/);
                        setAttribute(formContext, /svdp_mainfunder/, null);
                    }
                    else {
                        setAttributeRequiredLevel(formContext, /svdp_mainfunder/, /required/);
                        setAttribute(formContext, /svdp_mainfunder/, null);
                    }
                });
            }
            else {
                setAttributeRequiredLevel(formContext, /svdp_mainfunder/, /none/);
                setAttribute(formContext, /svdp_mainfunder/, null);
            }
            if (currentFundersRowCount != rowFundersCount) {
                refreshGrid(formContext, /FundersSubGrid/);
                console.log(/FundersSubGrid refreshed/);
                rowFundersCount = currentFundersRowCount;
            }
        });
    }

}
 
My Issue here is as below
when I set the Main funder from the drop down for the multiple funders list and save the record or re-load the record, the value is getting cleared because of the null
 
setAttribute(formContext, /svdp_mainfunder/, null);
 
Is there any way to skip to run the addOnload on Save or Reload of the Record ?
 
 
 
 
 
 
I have the same question (0)
  • Leah Ju Profile Picture
    Microsoft Employee on at
    Hi,
    According to your description, the problem is mainly that there is no judgement on whether the lookup field was initially set actively or later via js.
    Perhaps you could consider adding one 'Yes/No' choice column as a flag:
    -- If the lookup value is manually selected, "Yes" is displayed, otherwise "No" is displayed.
     
     
  • Prathibha Lakhotia Profile Picture
    47 on at
    Hi Suganthar,
     
    As per your requirement while adding funders list - add a attribute which says Main funder to 'Yes/No'. 
    Once Main funder is Yes update it in Lookup field.
     
    If Main funder is dynamic then on subgrid ribbon - show a button 'Set as Main Funder' once a row is selected so that you can overwrite it based on selected record.

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
Pallavi Phade Profile Picture

Pallavi Phade 98

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 60 Super User 2025 Season 2

#3
Gerardo Rentería García Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans