Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Reading Data from a subgrid

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
    AzAddict 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
    Dengliang Li 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
     
     

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,914 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,549 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans