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

No alerts are working at all.



Yes, the names are correct.   Event = 'name'.



The thing that is niggling in the back of my mind is that we are trying to issue queries to the database.   Is that allowed client side?


Hi Curtis,



GlideAggregate can be used for calculated fields:



http://wiki.servicenow.com/?title=GlideAggregate#gsc.tab=0



The fact that you are not getting even the first alert (which is before GlideAggregate) is a bit strange.


This field, "number_of_attendees_registered" is present on the form you are loading, right?



Can you put only this in your script and see if it works?



var vevent = current.name;


alert('Returned the value of vEvent = ' + vevent);



Regards,


Sergiu


Rushit Patel2
Tera Guru

Hi Curtis,



I added code above that works perfectly fine in calculated script. did u try?



if u want to use glide aggregate than se below code


var count = new GlideAggregate('x_11777_mar_events_attendee');   //replace with your table




count.addQuery('marketing_event',current.sys_id);     //first paramenter should be your event field name


count.addAggregate('COUNT');


count.query();


var reg = 0;


if (count.next()) {


  reg = count.getAggregate('COUNT');


}


current.att_cal = reg;   //replace with your field name


Thanks Rushit,



I just tried the following and it works!!!   So if I am understanding correctly, I WAS mixing Client with Server commands.



I'm not sure why you used 'current.sys_id' on line 2 though.   Wait, we are retrieving the value for the 'event' field in the current record and adding that to the query command.   Then we used the 'count' instance to execute the Aggregate('COUNT') command followed by the execution of the query.



The rest is just a count if loop and finally setting the current field equal to the result of the count.




var count = new GlideAggregate('x_21333_marketing_attendee');


count.addQuery('event',current.sys_id);


count.addAggregate('COUNT');


count.query();


var reg = 0;


if (count.next()) {


  reg = count.getAggregate('COUNT');


}


current.number_of_attendees_registered = reg;