Add GlideRecord UI Action

kirsvandenbergh
Kilo Explorer

Hi all,

I'm trying to add a record to a scoped application when someone clicks on a custom form button. I have used a UI action for this.

The UI action does not save the GlideRecord and when I debug I get following error: "Uncaught TypeError: GlideRecord is not a constructor".

Here's the code I'm using:

function test() {

  alert("dfkdf");

var sur = new GlideRecord("x_mytable");

sur.initialize();

sur.state = 2;

var successful = sur.insert();

if (successful.isNewRecord()){

alert('inserted');

}

else {

alert('not_inserted');

}

action.setRedirectURL("www.blabla.be");

}

The aim is to save the record and subsequently redirect the user to the newly created record form to add some more detail.

Best regards,

Kris

7 REPLIES 7

Chuck Tomasi
Tera Patron

You seem to have a combination of client side (alert) and server side (action.setRedirectURL) code.



Do you happen to have the Client checkbox turned on in your UI action? It doesn't appear that you need it here. If you want some simple debug messages on the server side, use gs.addInfoMessage()



Recommended reading: UI Actions - ServiceNow Wiki


Thanks Chuck. I have the client indeed activated. Record is not being saved and i cannot redirect the user


This this (caveat: untested) -



Name: Create New Record (adjust as necessary)


Show Insert: true


Show Update: true


Form Button: true


Client: false (unchecked)


Table: (your table name where you want this to appear)


Conditions: (blank for now)


Script:


var sur = new GlideRecord("x_mytable");


sur.newRecord();


sur.state = 2;


if (sur.insert())


    gs.addInfoMessage('New record created');


else


        gs.addErrorMessage('Error creating new record');



action.setRedirectURL(sur);


Updates made: Be sure to check my comments carefully - client is unchecked