Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Reading Data from a subgrid

(1) ShareShare
ReportReport
Posted on by
 Hi guys, 
 
This might be a simple fix but cannot seem to go around it. When i run the code there is not error which is weird and nothing seems to change. I have subgrid (Table Test opportunity split)called Subgrid_new_1 in Test Opportunity Form. Ever time a new record is saved in that subgrid and i refresh the form it should trigger. 

It would be great if i could get some input from you guys! 

Cheers,
 
function calculateAndUpdateOpportunity() {
    var totalSplit = null;
    var originalSplit = null;
 
    // Retrieve all records from the Opportunity Split subgrid
    var opportunitySplitRecords = Xrm.Page.getControl(/Subgrid_new_1/).getGrid().getRows().get();
    console.log(/Opportunity record updated successfully/ +opportunitySplitRecords);
    // Iterate through each record in the subgrid
    opportunitySplitRecords.forEach(function(row) {
        // Retrieve the split value from the current record in the subgrid
        var split = row.getData().getEntity().attributes.get(/tgd_SplitPercentage/).getValue();
       
        // Check if either totalSplit or originalSplit is null
        if (totalSplit === null || originalSplit === null) {
            // Initialize totalSplit and originalSplit
            totalSplit = totalSplit === null ? split : totalSplit;
            originalSplit = originalSplit === null ? 100 - totalSplit : originalSplit;
        } else if (originalSplit >= totalSplit + split) {
            // Reduce the originalSplit by the current split value
            originalSplit -= split;
            // Update the totalSplit
            totalSplit += split;
            console.log(/Original split: / + originalSplit + /, Total Split: / + totalSplit);
        } else {
            console.log(/Original split cannot be less than total split/);
            return; // Break out of the loop if originalSplit cannot cover the current totalSplit + split
        }
    });
 
    // Update Total Split and Original Split fields on the Opportunity record
    var opportunityRecord = {};
    opportunityRecord[/tgd_TotalSplit/] = totalSplit; // Replace /totalsplit/ with the schema name of Total Split field
    opportunityRecord[/tgd_OriginalSplit/] = originalSplit; // Replace /originalsplit/ with the schema name of Original Split field
 
    // Update the Opportunity record Opportunity Logical name
    Xrm.WebApi.updateRecord(/tgd_testopportunity/, Xrm.Page.data.entity.getId(), opportunityRecord).then(
        function success(result) {
            console.log(/Opportunity record updated successfully/);
        },
        function (error) {
            console.log(/Error updating Opportunity record: / + error.message);
        }
    );
}
  • Suggested answer
    AzAddict Profile Picture
    12 on at
    Reading Data from a subgrid
    A few things to check:
    a. Xrm.Page is depreciated (check out https://learn.microsoft.com/en-us/power-platform/important-changes-coming#some-client-apis-are-deprecated), use the ExecutionContext instead (make sure you tick the "Pass Execution Context as first parameter" when you add your function to the event handler).
    b. You are using slashes in your string literals, use the single/double quotes around them.
    c. In your getId statements the record will be returned enclosed in the curly brackets, you might want to trim them if you plan to use it down the line.
    d. After you added your web resource, make sure to "save & publish" and refresh the app so that your changes take place.
    e. And, of course, make sure to incorporate console.log statements throughout your code to make sure you are capturing the output you desire in the appropriate sections of the code.
     
    Best of luck troubleshooting.
  • Dengliang Li Profile Picture
    Microsoft Employee on at
    Reading Data from a subgrid
    Hi,
     
    What event have you registered the code to?
    The form's onload event or the event on the editable subgrid?
    Does the log message from the code appear in the browser console?
     
    Does the output in the success callback function of the last update operation show up in the browser console?
     
    You can set breakpoint /debugger/ in the code for debugging.
     
    Best Regards,
    Dengliang Li
     
     

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Product updates

Dynamics 365 release plans