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 :
Microsoft Dynamics CRM (Archived)

Adding additional onload functions to a javascript

(0) ShareShare
ReportReport
Posted on by 260

I have a javascript that runs onload to populate a field based on the values in a set of child records presented in a subgrid on the opportunity form. The script is as follows and works exactly as intended:

function timeout ()

 { 

  setTimeout(contractactsum, 3000);

  function contractactsum()

    {  

     var grid = document.getElementById('contracts');  

     var ids = grid.control.get_allRecordIds();  

     var sum = 0.00;  var cellValue;  for(i = 0; i < ids.length; i++)  

    {  

     var cellValue = grid.control.getCellValue('xma_totalactualrev',ids[i]);  

     var number = Number(cellValue.replace(/[^0-9\.]+/g,""));  

     sum = sum + number;  

    }

 Xrm.Page.data.entity.attributes.get('actualvalue').setValue(sum);  

}

 

 

What I would now like to do is repeat this for a different column form the sub grid and send the results to the actualvalue field on the opportunity form. That would look something like the following. I've literally just started working with javascript so I'm really unsure about how to integrate this next step. Any advice gratefully received!

function contractactsum()

 {

 var grid = document.getElementById('contracts');

 var ids = grid.control.get_allRecordIds();

 var sum = 0.00;  var cellValue;  

 for(i = 0; i < ids.length; i++)  

 {  

  var cellValue = grid.control.getCellValue('xma_totalactualrev',ids[i]);  

  var number = Number(cellValue.replace(/[^0-9\.]+/g,""));  

  sum = sum + number;  

 }

 Xrm.Page.data.entity.attributes.get('actualvalue').setValue(sum);

 }

 

 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Michael Kipling Profile Picture
    260 on at

    I managed to solve this by coding it all into one script. I repeated the for loop for each variable and reset the 'sum' variable each time.

    function timeout ()

    {

    setTimeout(contractsum, 3000);

    }

    function contractsum()

    {

    var grid = document.getElementById('Opportunity_Contracts');

    var ids = grid.control.get_allRecordIds();

    var sum = 0.00;

    var cellValue;

    for(i = 0; i < ids.length; i++)

     {

     var cellValue = grid.control.getCellValue('xma_totestrev',ids[i]);

     var number = Number(cellValue.replace(/[^0-9\.]+/g,""));

     sum = sum + number;

     }

    Xrm.Page.data.entity.attributes.get('estimatedvalue').setValue(sum);

    var sum = 0.00;

    for(i = 0; i < ids.length; i++)

     {

     var cellValue = grid.control.getCellValue('xma_totmargin',ids[i]);

     var number = Number(cellValue.replace(/[^0-9\.]+/g,""));

     sum = sum + number;

     }

    Xrm.Page.data.entity.attributes.get('py3_margin').setValue(sum);

    var sum = 0.00;

    for(i = 0; i < ids.length; i++)

     {

     var cellValue = grid.control.getCellValue('xma_totactrev',ids[i]);

     var number = Number(cellValue.replace(/[^0-9\.]+/g,""));

     sum = sum + number;

     }

    Xrm.Page.data.entity.attributes.get('actualvalue').setValue(sum);

    }

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans