Catalog client scripts and record deletion

midsummerbreeze
Giga Contributor

Greetings,

I have a catalog client script that performs validation on a record producer onSubmit.

I am running into a bit of a problem when I attempt to delete a user record from within the catalog client script.

I found the following Servicenow document that discusses limited use of some GlideRecord functionality inside client side scripts, such as client scripts

http://wiki.servicenow.com/index.php?title=Client_Side_GlideRecord#deleteRecord.28responseFunction.2...

However, the code isn't working and I am beginning to believe that the documentation is incorrect and record deletions cannot be performed in client scripts for regular ITIL users.

Below is a snapshot of my catalog client script:

clientscript.png

If I can't perform a delete from a catalog client script, does anyone have any recommendations on how I can accomplish my goal of deleting a user record as an ITIL user when validating a record producer?

Thanks much!

13 REPLIES 13

The SN Nerd
Giga Sage
Giga Sage

Move your first gr.addQuery into your server side script include.


Glide record queries should be avoided on the client side.



Add in some gs.addInfoMessage calls server side to see what is going on.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

midsummerbreeze
Giga Contributor

Ok, I resolved the issue. The issue was with this code:



  var rec = new GlideRecord('sys_user');


  if (rec.get(argParam1)) {



Somehow it didn't like the "get" command



I had to change it to:




var rec = new GlideRecord('sys_user');


rec.addQuery('user_name', argParam1);


rec.query();


    if (rec.next()) {



Thanks to all who responded.


You are running two queries on the User table; one on the client side (a big no-no) and one on the Server Side.


Only one is necessary. Also, alerts make for poor UX.



May I suggest refactoring as follows:



Client Script


function onSubmit() {


  var un = g_form.getValue('secondary_account');


  var ga = new GlideAjax('OITUserDelete');


  ga.addParam('sysparm_name','removeUserRecordIfNoUUID');


  ga.addParam('sysparm_param1',un);


  ga.getXML(outputAccountMessage);



  function outputAccountMessage(response) {


  g_form.hideAllFieldMsgs();


  var answer = response.responseXML.documentElement.getAttribute("answer");


  if (answer == true) {


            g_form.showFieldMsg('secondary_account',"Account '" + un + "' already exists, however, the UUID is empty.\n\nTherefore, this stud account will be deleted and the new account will be provisioned.");


  } else {


  g_form.showFieldMsg('secondary_account',"Nothing will be done. Account '" + un + "' already exists and the UUID is: " + answer);


            }


  }


}



Script Include


var OITUserDelete = Class.create();


OITUserDelete.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      removeUserRecordIfNoUUID: function() {


  var argParam1 = this.getParameter('sysparm_param1');


  var rec = new GlideRecord('sys_user');


  gr.addQuery('user_name', un);


  gr.query();


  if (gr.next()) {


  if (gr.uniqueid_euuid_.nil() ) {


  return rec.deleteRecord();


  } else {


  return gr.getValue('uniqueid_euuid_');


  }


  }


  return null;


  },




      type: 'OITUserDelete'


});



The code above has not been tested and is for educational purposes only



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi Paul,



Thanks a bunch for the feedback. In our case, the alerts are a necessary evil. "showFieldMsg" is shown only on the field and since we are using a record producer to add the user record, that message is not visible to the user once the "Submit" button is clicked, which is why I used "alert" instead.



Also, in the script include,



if (gr.uniqueid_euuid_.nil() ) {


doesn't evaluate to true when that field is null and as a result, the user record with the empty UUID is not deleted when I use the modified code. In previous attempts, I used "nil()" and found that it didn't work which is why the initial code included:



if (!gr.uniqueid_euuid_) {



Solo