UI action with server side and client side code

sigmachiuta
Kilo Guru

I am trying to create a UI action that will run some server side code and validate if a record a record exists.   If it does I will update some fields if not abort.

Checking out Client & Server Code in One UI Action - ServiceNow Guru but i am still having some issues I am confused about the onClick and Action name fields I see how they are used in the example but i am still a little confused how to use them

function verify(){

var gr = new GlideRecord('dms_document');

gr.addQuery('u_demand','current.number');

gr.addQuery('type.name', 'foobar');

gr.query();

if(!gr.next()){

g_form.showFieldMsg('Document','ALERT YOU MUST DO SOMETHING','error');

}

}

if(typeof window == 'undefined')

commitRecord();

// commit record if verify function returned a record

function commitRecord(){

current.u_state = 20;

current.assignment_group.setDisplayValue('Team');

current.assigned_to = '';

current.update();

action.setRedirectURL(current);

}

3 REPLIES 3

veena_kvkk88
Mega Guru

Can you elaborate on what exactly you need? That article describes the scenario where you need to make some validations on the current form and then update the database(server-side) accordingly. But looking at your code, I don't see any client side validations/manipulations. So you could just omit the client callable feature here. But if you need client side functionality, there's one missing statement: gsftSubmit(null, g_form.getFormElement(), 'readyPR');


That function call is for the UI action again, but this time skipping the client side function.


Prasun
Giga Guru

Hello,



Check the below settings. You need to check the client checkbox and should write the name of the function which is verify in this case in the OnClick field.



The action name should be unique with respect to UI action, but it can be anything. Make sure you do no give any space in the action name field.



Onclick function runs on the client side and the rest of the functions that are written executes in the server side.



You can use the entire block of code you used, I could not show it in the snapshot as I was not able to capture,   but use the following code in the script section.



  1. function verify(){  
  2. var gr = new GlideRecord('dms_document');  
  3. gr.addQuery('u_demand','current.number');  
  4. gr.addQuery('type.name', 'foobar');  
  5. gr.query();  
  6.  
  7. if(!gr.next()){  
  8. g_form.showFieldMsg('Document','ALERT YOU MUST DO SOMETHING','error');  
  9.  
  10. }  
  11. }  
  12. if(typeof window == 'undefined')  
  13. commitRecord();  
  14.  
  15. // commit record if verify function returned a record  
  16. function commitRecord(){  
  17. current.u_state = 20;  
  18. current.assignment_group.setDisplayValue('Team');  
  19. current.assigned_to = '';  
  20. current.update();  
  21. action.setRedirectURL(current);  
  22.  
  23.  
  24. }  


find_real_file.png



Please Mark as Helpful if helps



Thanks


Prasun


Abhinay Erra
Giga Sage

You are missing gsftSubmit line. Moreover current object is not available on client side. You are using current.number on client side. Instead use this g_form.getValue('number')



function verify(){


var gr = new GlideRecord('dms_document');


gr.addQuery('u_demand',g_form.getValue('number'));


gr.addQuery('type.name', 'foobar');


gr.query();


if(!gr.next()){


g_form.showFieldMsg('Document','ALERT YOU MUST DO SOMETHING','error');  


}