Scripting Help- How to Auto Populated Reference Variables in a Catalog Item from a UI Action

Su522
Kilo Sage

I have a UI Action and an onLoad Catalog Client Script that needs to auto-populate variables from a record to the Catalog Item form.

This is working but I can't seem to get the code right for the Reference fields to Account and Ticket #

 

How it should work:

From a saved record, click the UI Action that directs to the Catalog Item (I have this working) > the Catalog Item needs to be auto-populated with the following fields from the record:

Account name

Ticket #

 

Can someone please help by providing examples of how I can get this to work?

 

Any help is greatly appreciated!!

Thank you,

Susam

1 ACCEPTED SOLUTION

@ssellmeyer 

You can use the above logic if it is in service portal but for the native UI, you can make use of the below function:

function getParmVal(name){
    var url = document.URL.parseQuery();
    if(url[name]){
        return decodeURI(url[name]);
    }
    else{
        return "";
    }
}

(=tested)

 

Thanks,
Murthy

View solution in original post

5 REPLIES 5

Gangadhar Ravi
Giga Sage
Giga Sage

@Su522  Please check this.

 

https://www.servicenow.com/community/itsm-forum/fill-catalog-variable-via-url/m-p/2748704

 

Please mark my answer correct and helpful if this works for you.

Murthy Ch
Giga Sage

Hello @Su522 

Try the below script:

function onLoad() {
    //Type appropriate comment here, and begin script below
    var inc = getParameterValue("sysparm_incident");  //replace with your parm's
    if (inc) {
        g_form.setValue("incident", inc); //replace with your variable name
    }

    function getParameterValue(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(top.location);
        if (results == null) {
            return "";
        } else {
            return unescape(results[1]);
        }
    }

}

(=tested)

Thanks,
Murthy

@Murthy Ch 

Thank you Murthy, I appreciate your response but I haven't been able to get this to work. 

Maybe I'm updating the code wrong?

 

Here is my code for the Catalog Client Script:

function onLoad() {
var sd = g_user.getClientData('short_desc');  
g_form.setValue('short_desc', sd);  

    var co = getParameterValue("sysparm_sn_ind_tmt_orm_order");  //replace with your parm's
    if (co) {
        g_form.setValue("ticket_number", co); //replace with your variable name
    }

    function getParameterValue(number) {
        number = number.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + number + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(top.location);
        if (results == null) {
            return "";
        } else {
            return unescape(results[1]);
        }
    }

}
 
Here is my code for the UI Action:
var session = gs.getSession();

session.putClientData('short_desc', current.short_description);
var short_desc = current.short_description;

session.putClientData('ticket_number', current.number);
var co = current.number;

gs.setRedirect('com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=ea3841fc4751351010057cb2a36d438b&sysparm_short_desc=' + short_desc + '&sysparm_number=' + co);
 
 
This is for the Customer Order table.
"number" is the Customer order field name for the CO #
"ticket_number" is the variable name from the Catalog Item (that is a reference to the CO table)
 
When I click the UI Action from the Customer Order > it redirects to the Catalog Item and the Short Description is filled in on the Catalog Item (same as the short description of the Customer Order) but the Ticket Number is Blank (empty)
See attachment.
 
I really really appreciate any help
Can you please help?
Your help is greatly appreciated!!!
Thank you,
Susan

@ssellmeyer 

You can use the above logic if it is in service portal but for the native UI, you can make use of the below function:

function getParmVal(name){
    var url = document.URL.parseQuery();
    if(url[name]){
        return decodeURI(url[name]);
    }
    else{
        return "";
    }
}

(=tested)

 

Thanks,
Murthy