The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Compute the sum of a String field and display it to a String field

dianemiro
Kilo Sage

Dear All,

We have a business rule to sum up all the Efforts <u_effort_man_day> of a CTASK and display it to the Demand field Estimated Effort <u_effort_man_days>. Here is my code:

var dmnd = new GlideRecord('dmn_demand');

dmnd.addQuery('u_demand',current.sys_id);

dmnd.query();

var ctsk = new GlideRecord('change_task');

ctsk.addQuery('sys_id',current.u_demand);

ctsk.query();

var parCtask = parseFloat(ctsk.u_effort_man_day.replace(/,/g,''));

if(dmnd.next()){

  var count = 0;

  while(ctsk.next()){

  if(ctsk.u_demand_task_type == 'screening'){

  count += parseFloat(parCtask);   //u_effort_man_days field on change task

  }

  }

  dmnd.u_effort_man_days = count; //u_effort_man_days field on demand

  dmnd.update();

}

Apparently, this code is not working. Can you tell me what's wrong? Many thanks.

1 ACCEPTED SOLUTION

Hi Shishir,



Thank you so much for your response, I appreciate your help. I was able to sum up the effort by using the below code:



var dmnd = new GlideRecord('dmn_demand');


dmnd.addQuery('sys_id',current.u_demand);


dmnd.query();


var ctsk = new GlideRecord('change_task');


ctsk.addQuery('u_demand',current.u_demand);


ctsk.query();


if(dmnd.next()){


            var count = 0;


            while(ctsk.next()){



                                if(ctsk.u_demand_task_type != 'screening'){


                                                              count += parseFloat(ctsk.u_effort_man_day);   //u_effort_man_days field on change task


                                                    }


            }



  dmnd.u_effort_man_days = count; //u_effort_man_days field on demand


  dmnd.update();


}


View solution in original post

10 REPLIES 10

Harsh Vardhan
Giga Patron

can you try to put some log in the script. and check one by one where you are missing something. why i am saying bcz you are doing glide record on multiple table.


what's count= 0 in line number 9.


Hi Harsh,



Thank you for your prompt response, can you tell me how can I run the log on the script? Also var count = 0 on line number 9 is for initiating the variable count.


Hello Diane,



Can you please try below code, see if this helps.



var dmnd = new GlideRecord('dmn_demand');  


dmnd.addQuery('u_demand',current.sys_id);  


dmnd.query();    


if(dmnd.next()){  


var count = 0;  


var ctsk = new GlideRecord('change_task');  


ctsk.addQuery('sys_id',current.u_demand);  


ctsk.query();  


while(ctsk.next()){


var parCtask = parseFloat(ctsk.getValue('u_effort_man_day').replace(/,/g,''));


if(ctsk.getValue('u_demand_task_type') == 'screening'){  


count += parseFloat(parCtask);   //u_effort_man_days field on change task  


}  


}  


dmnd.u_effort_man_days = count; //u_effort_man_days field on demand  


dmnd.update();  


}  


Hi Shishir,



Thanks for your help. But the script is not working.