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

Hi Ivan,



Yes, because I get the correct pop-up text for that condition.



Solo


Abhinay Erra
Giga Sage

Use the script without callback function. That should do the trick.


I did try it without the callback initially and it didn't work. It was original like so:



function onSubmit() {
        //Type appropriate comment here, and begin script below
        un = g_form.getValue('secondary_account');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('user_name', un);
        gr.query();
        if (gr.next()) {
                if (!gr.uniqueid_euuid_) {
                        alert("Account '" + un + "' already exists, however, the UUID is empty.\n\nTherefore, this stud account will be deleted and the new account will be provisioned.");
                        gr.deleteRecord();
                        return true;


              }
                else {
                        alert("Nothing will be done. Account '" + un + "' already exists and the UUID is: " + gr.uniqueid_euuid_);
                        return false;
                }
        }
}


The SN Nerd
Giga Sage
Giga Sage

As a rule of thumb, business logic should not be implemented client side.



You should do this on the workflow for this item, or in a After Business Rule.



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

midsummerbreeze
Giga Contributor

I changed the code so that a script include is called from the catalog client script and I still can't get a record deleted as an ITIL user:



Script Include:


var OITUserDelete = Class.create();


OITUserDelete.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      removeUserRecord: function() {


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


            var rec = new GlideRecord('sys_user');


      if (rec.get(argParam1)) {


  return rec.deleteRecord();


    }


  },


      type: 'OITUserDelete'


});



Catalog Client Script:


function onSubmit() {


  //Type appropriate comment here, and begin script below


  un = g_form.getValue('secondary_account');


  var gr = new GlideRecord('sys_user');


  gr.addQuery('user_name', un);


  gr.query();


  if (gr.next()) {


  if (!gr.uniqueid_euuid_) {


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


  var ga = new GlideAjax('OITUserDelete');


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


                      ga.addParam('sysparm_param1',un);


                      ga.getXML(callBackFunction);


  }


  else {


  alert("Nothing will be done. Account '" + un + "' already exists and the UUID is: " + gr.uniqueid_euuid_);


  return false;


  }


  }



  function callBackFunction(response) {


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


  if (answer == true) {


      return;


  }


      }


}



I just don't get why the record isn't being deleted. The client callable check box is checked in the script include. What am I missing now?