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,
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);
}
);
}