Get Sysid of the Target Record produced by the Record Producer in Catalog Client Script

EZHIL ARASAN1
Tera Contributor

I need to get the Sysid of the Record created by the record producer on Submit in a Catalog Client Script. The record producer is created for a Custom Table.

Is there an OOB way to achieve it?

 

I found a similar post below where it works for the incident table. But for my requirement i need the code for a custom one.

https://www.servicenow.com/community/developer-forum/get-sysid-of-record-producer-in-catalog-client-... 

1 ACCEPTED SOLUTION

That's correct and another reason why we as people who are trying to help others on these forums, shouldn't really reply to someone offering suggestions if we haven't verified them ourselves or at least feel pretty confident in it. This not only affects the current poster, but all future readers.

 

It wasn't appropriate, in my opinion, to use AI to generate suggestions which none of them are correct or useful in this given scenario.

 

For your issue, unfortunately, accessing it prior to submission may not be able to be accomplished.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

7 REPLIES 7

Kristen Ankeny
Kilo Sage

What need are you trying to fulfill by getting the produced record's sys_id in a catalog client script?

Hi Kristen,

The requirement is that if the record producer variables match certain values, i should trigger an email/notification. For that i am using gs.eventQueue() method to trigger and i need to pass the sysid of the record as a parameter.

For this Functionality, i could create a business rule on the target table as well. But needed to check if the same functionality could be implemented through a catalog client Script in a OOB way?

I don't believe this is possible because the id of the produced record does not exist until after submission. The business rule on the created record is the right way to go - if any of the variables are not available on the produced record you can still get to the answers by looking at question_answer table. 

Mark Manders
Mega Patron

I tried some ways to get this done, but nothing worked, so against my principles when answering questions (because I did wanted to know the answer), AI gave me this. Please validate any solution you use, because I've seen more wrong than correct answers coming from most GPT's (at least this one warns against DOM manipulation):

 

 

In ServiceNow, accessing elements directly by ID using methods like gel('element_id') is not recommended, especially in scoped applications, due to potential conflicts with the scoped API and platform security rules. Scoped applications have more restrictions to ensure they don't interfere with the global scope or other applications unintentionally.

For getting the Sys ID of a record created through a record producer in a scoped application, consider using the following approaches:

 

1. After Script of Record Producer

You can use the After Script of the record producer to pass the Sys ID of the created record to the redirect URL or store it in a variable that can be accessed later. For example:

 

 

// After Script in Record Producer
var createdRecordSysId = current.sys_id.toString(); // 'current' represents the created record
action.setRedirectURL(current); // Redirects to the created record, where you can extract the Sys ID from the URL if needed

 

 

2. Client Script with g_form.getUniqueValue()

In client scripts within forms, you can use the g_form API to get the Sys ID of the current record. This is applicable if you're trying to access the Sys ID after the record has been created and the form is loaded:

 

// onLoad or onChange Client Script
var sysId = g_form.getUniqueValue();

 

 

This function works in scoped applications as well and is the recommended way to access the Sys ID on forms.

 

3. Using the GlideAjax API

If you need to access the Sys ID from the client side and the record is not available on the form, consider using GlideAjax to call a server-side script that can retrieve and return the Sys ID. This method involves more setup but is versatile for various use cases.

 

Client-Side Script:

 

var ga = new GlideAjax('YourScopedServerScript');
ga.addParam('sysparm_name', 'getSysId');
ga.addParam('sysparm_your_parameter', 'your_value');
ga.getXMLAnswer(function(response) {
    console.log("Sys ID: " + response);
});

 

 

 

Server-Side Script Include (Scoped):

 

var YourScopedServerScript = Class.create();
YourScopedServerScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getSysId: function() {
        var yourParameter = this.getParameter('sysparm_your_parameter');
        // Logic to get the Sys ID based on yourParameter
        return sysId; // The Sys ID you want to return
    }
});

 

 

 

When working in scoped applications, it's essential to adhere to the best practices and use the APIs provided by the ServiceNow platform for scoped development. Direct DOM manipulation and global functions like gel() are discouraged and may not work as expected in scoped environments.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark