onClick UI Action

cnharris1
Kilo Sage

Good evening everyone,

I have a question about my code. The back story is that I created application for our Working on Wellness Team in ServiceNow. I have a page that shows everyone the upcoming events from the Working on Wellness Team and there's a button that the users can click on that brings them to a form where they can register for the event. They click on the register button and its supposed to take show a message that tells the user that they've been registered and then redirect them to our service portal. And if they have already registered for the event, instead of seeing a register button, the user will see a withdraw button. The withdraw button is working fine but the problem is the register button. It registers the user but I don't see the message saying that they've been registered, nor does it redirect them to the service portal. I also have code that shows whether or not the class if full and adds the user to a waiting list. Can someone help me out to get this register button working as its intended? Here's a copy of the Register button code:

function runClientCode(){
 var attend = new GlideRecord('u_attendee');
 attend.initialize();
 //query for attendees of the current activity
 var gr = new GlideRecord('u_attendee');
 gr.addQuery('u_activity', g_form.getUniqueValue());
 gr.query();
 
 var current_record = new GlideRecord('u_activity');
 current_record.get(g_form.getUniqueValue());
 
 attend.u_activity = g_form.getUniqueValue();
 attend.u_attendee = g_user.userID;
 
 var max = current_record.u_max_attendees;
 var WL_max = current_record.u_wait_list_maximum;
 
 if(gr.rows.length <max || !current_record.u_cap_attendees)
  {
  attend.u_wait_list = false;
  var id = attend.insert();
  //alert(g_form.getValue('u_team_event') == true);
  if( current_record.u_team_event== 'true')
   {
   showMyForm(id);
  }
  else
   {
   gsftSubmit(null, g_form.getFormElement(), 'register');
  }
 }
 else if(gr.rows.length <(max+WL_max))
  {
  attend.u_wait_list = true;
  var id = attend.insert();
  if( current_record.u_team_event == 'true')
   {
   showMyForm(id);
  }
  else
   {
   gsftSubmit(null, g_form.getFormElement(), 'register');
  }
 }
}

if(typeof window == 'undefined')
 showMessage();


function showMessage()
{
 var gr = new GlideRecord('u_attendee');
 gr.addQuery('u_activity', current.sys_id);
 gr.addQuery('u_attendee', gs.getUserID());
 gr.query();
 if(gr.next())
  {
  if(gr.u_wait_list)
   {
   gs.addInfoMessage(current.u_activity_name+" is currently full. You have been placed on the wait list, you will be notified if a spot becomes avaliable");
  }
  else
   {
   gs.addInfoMessage('You have been registered for ' + current.u_activity_name);
  }
 }
 else
  {
  gs.addInfoMessage(current.u_activity_name+" is currently full. Please check back later to see if a spot has opened up.");
 }
 action.setRedirectURL(current);
}


function dothis(action, sys_id, table, displayValue) {
 gsftSubmit(null, g_form.getFormElement(), 'register');
}

 

function showMyForm(id){
 //Create and open the dialog form
 var dialog = new GlideDialogForm('Select Team', 'u_attendee', dothis); //Provide dialog title and table name
 dialog.setSysID(id); //Pass in sys_id to edit existing record, -1 to create new record
 dialog.addParm('sysparm_view', 'Team'); //Specify a form view
 dialog.addParm('sysparm_form_only', 'true'); //Add or remove related lists
 dialog.render(); //Open the dialog
}

 

Also, here's a screenshot of the conditions for the button:

find_real_file.png

And here's a copy of the code for my withdraw button:

find_real_file.png

 

Thanks in advance,

 

cnharris

1 ACCEPTED SOLUTION

Lavlesh Garg1
Giga Expert

cnharris,

 

It's most probably because you're putting GlideRecord query and insert operations on client side. As you would know,  runClientCode() function is running on client side while showMessage() is running on server side. There is a very limited support for GlideRecord on Client Side on Global applications, and none in Scoped Applications:

https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=c_GlideRecordClientSideAPI

The client-side GlideRecord API is not supported in scoped applications. Instead, create a script include and use theGlideAjax API, or use the REST APIs. In addition, the client-side GlideRecord API applies ACLs based on the credentials of the user executing the script. To execute the code on the server without ACLs, use the GlideAJAX API

If it's a Global application, then you must use callback functions for query and insert as mentioned in above snow documentation

Query:

var rec = new GlideRecord('incident');
rec.query(recResponse);
 
function recResponse(rec) {
  while (rec.next()) { 
   alert(rec.number + ' exists');
  } 
}

 

Insert:

insert(Function responseFunction)

Inserts a new record using the field values that have been set for the current record.

Parameter(s):
NameTypeDescription
responseFunctionFunctionThe response function.
Return:
TypeDescription
StringThe sys_id of the inserted record, or null if the record was not inserted.

Example

 

var gr = new GlideRecord('to_do');
gr.initialize(); 
gr.name = 'first to do item'; 
gr.description = 'learn about GlideRecord'; 
gr.insert();

View solution in original post

2 REPLIES 2

Lavlesh Garg1
Giga Expert

cnharris,

 

It's most probably because you're putting GlideRecord query and insert operations on client side. As you would know,  runClientCode() function is running on client side while showMessage() is running on server side. There is a very limited support for GlideRecord on Client Side on Global applications, and none in Scoped Applications:

https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=c_GlideRecordClientSideAPI

The client-side GlideRecord API is not supported in scoped applications. Instead, create a script include and use theGlideAjax API, or use the REST APIs. In addition, the client-side GlideRecord API applies ACLs based on the credentials of the user executing the script. To execute the code on the server without ACLs, use the GlideAJAX API

If it's a Global application, then you must use callback functions for query and insert as mentioned in above snow documentation

Query:

var rec = new GlideRecord('incident');
rec.query(recResponse);
 
function recResponse(rec) {
  while (rec.next()) { 
   alert(rec.number + ' exists');
  } 
}

 

Insert:

insert(Function responseFunction)

Inserts a new record using the field values that have been set for the current record.

Parameter(s):
NameTypeDescription
responseFunctionFunctionThe response function.
Return:
TypeDescription
StringThe sys_id of the inserted record, or null if the record was not inserted.

Example

 

var gr = new GlideRecord('to_do');
gr.initialize(); 
gr.name = 'first to do item'; 
gr.description = 'learn about GlideRecord'; 
gr.insert();

Thanks Lavlesh,

That helped me out and I have my button working now!