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

Hi Diane,



Please find few suggestions below.



var dmnd = new GlideRecord('dmn_demand');


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


dmnd.query();  


gs.log(dmnd.getRowCount());


while(dmnd.next()){


var count = 0;


var ctsk = new GlideRecord('change_task');


ctsk.addQuery('sys_id',dmnd.u_demand); //Hoping u_demand is a reference field on dmn_demand table and contains the Change Task number


ctsk.query();


while(ctsk.next()){


var parCtask = ctsk.getValue('u_effort_man_day').replace(/,/g,''); //do we need replace here?


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


count = parseFloat(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,



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();


}


cool, that means there was an issue with if condition. Thank you for the update.


issue was in line number 2. good job dianemiro


Thank you guys for your help. The new lines of code "logs" and "getvalue" will be useful in the future.