OnLoad ClientScript for GlideRecord

kagarwni
Kilo Expert

Hi Team,

Reaching out for expert eyes again !

I am trying to capture the total hrs logged on all timecards for a particular Master Record.

This onLoad is on the form for Master Record.

The Master Record has currently 6 timecards associated with it and I do get the alert 6 times which means the records are fetched.

However, the gr1.total value is blank and not returning any value. For that matter, I tried few more values out of that glide record but no success.

function onLoad() {

              var est_hrs_utilised = 0;

  g_form.setValue("u_estimated_hours_utilized",est_hrs_utilised);

        var vsar_sysid = g_form.getUniqueValue();

  alert(vsar_sysid);

  var gr1 = new GlideRecord('time_card');

        gr1.addQuery("state", 'Approved').addOrCondition("state", 'Processed');

              gr1.addQuery("u_vsar", vsar_sysid);

  gr1.query();

  while(gr1.next())

    {

    alert("I am in :" , gr1.total);

    est_hrs_utilised = est_hrs_utilised + gr1.total ;

  }

  g_form.setValue("u_estimated_hours_utilized",est_hrs_utilised);

}

Regards

Nitin

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

While I recommend you use a GlideAjax call to do this (much easier to debug and maintain)...



Try changing the gr1.total to gr1.getValue('total');



You may also need to do a parseFloat() to convert it to a decimal if JavaScript thinks it is a string. Hopefully not, but be aware.



Use a "+" instead of a "," in alert for best results.


alert("I am in :" + gr1.getValue('total');


est_hrs_utilised = est_hrs_utilised + gr1.getValue('total') ;



View solution in original post

5 REPLIES 5

I've started using getValue() in almost all situations, especially those in loops (and Jelly code)



The main reason is that the GlideRecord variable you created is actually a pointer and when you do something like answer.push(gr.sys_id), it's copying the pointer value, not the sys_id value so you're loop ends up with all the same values at the end. It's bitten me more times than I can count.



MOST times, you're fine without it, but when things go really nuts, try it and you'll likely find it's a good fix.