Set field value via client script

crowell001
Kilo Explorer

I have a demo app with two tables, Attendees and Marketing Events.   The Attendees form/table has a field (events).   The Marketing Events has a field Registered.

In Configure Dictionary for the Registered field, I have the following script as a 'calc'. What I'm attempting to do is to do a count of all records in Attendees where the 'event' field matches the Marketing Event field called 'Name'.   In the Attendees table/form, Event references the Name field in Marketing Event.

However, I am getting nothing out of this - not even logging information.

var count = new GlideAggregate('Attendees');

var vevent = current.Name;

gs.Debug("Returned the value of vEvent = " + vevent);

count.addAggregate('COUNT');

COUNT.QUERY('Event', vevent);

var registered = 0;

if (count.nex()) {

  registered = count.getAggregate('COUNT');

}

gs.Debug("Returned the number of rows = " + registered);

g_form.setValue('registered', registered);

13 REPLIES 13

sunilsafare
Mega Guru

Hi Curtis,



Where are you writing this script? Client or Server side. Because g_form is not used on server side and current/gs is not used on client side.



Thanks,


Sunil Safare


Rushit Patel2
Tera Guru

Hi Curtis,



you can only use server side code there.



below script works fine for me.


var gr = new GlideRecord('x_11777_mar_events_attendee');
gr.addQuery('marketing_event',current.sys_id);
gr.query();


var attendee = gr.getRowCount();


current.att_cal = attendee;



you can also use glide aggregate.



Regards,


Rushit Patel.


sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Curtis,



As far as I can see you are not using table name or proper field names in your script, but rather Labels, which will not work.



Therefore:



1.   This should use table name rather than Label of the table ("Attendees" is a table Label).



var count = new GlideAggregate('Attendees');



2. The following lines should use field names rather than Labels:



var vevent = current.Name;     //"Name" is probably a field label, you need the fieldname


...


count.QUERY('Event', vevent);  


//"Event" is probably a field label, you need the fieldname


//there is no QUERY method with a condition, it should be replaced with the following 2 lines:


//count.addQuery('Event', vevent);  


//count.query()



If I assume the tablename is "attendees" I would write the script as:



var count = new GlideAggregate('attendees');


var vevent = current.name;


gs.Debug("Returned the value of vEvent = " + vevent);


count.addQuery('event', vevent);


count.addAggregate('COUNT');


count.query()


var reg = 0;


if (count.next()) {


  reg = count.getAggregate('COUNT');


}


gs.Debug("Returned the number of rows = " + reg);


current.registered = reg;



Hope this helps.



Regards,


Sergiu


Thank you Sergiu,



I have modified this client script (Configure Dictionary/calc value for the field Registered (number_of_attendees_registered) and now have the following:



var count = new GlideAggregate('x_21333_marketing_attendee');


var vevent = current.name;


gs.Debug("Returned the value of vEvent = " + vevent);


count.addQuery('event', vevent);


count.addAggregate('COUNT');


count.query()


var reg = 0;


if (count.next()) {


  reg = count.getAggregate('COUNT');


  }


gs.Debug("Returned the number of rows = " + reg);


current.registered = reg;   <Is this required to actually set the value for 'number_of_attendees_registered'?



Also, should this not be wrapped into an 'onLoad()' function?   I've tried it with 'function onLoad()' and without it but it isn't working.   I've tried replacing 'current.registered' with 'current.fieldName', also to no avail.



This is what I currently have:



function onLoad(){


  var count = new GlideAggregate('x_21333_marketing_attendee');


  var vevent = current.name;


  gs.Debug("Returned the value of vEvent = " + vevent);


  count.addQuery('event', vevent);


  count.addAggregate('COUNT');


  count.query();


  var reg = 0;


  if (count.next()) {


  reg = count.getAggregate('COUNT');


  }


  gs.Debug("Returned the number of rows = " + reg);


  current.registered = reg;


}




Thanks in advance,