How to open a catalog item with pre-populated variables from a portal "Create New" button?

CharanV66187530
Tera Contributor

Hello Community,

I have added a "Create New" button on my Service Portal. My goal is that when a user clicks this button, it opens a specific catalog item, and some variables on that item are automatically populated with predefined data.

Could someone please guide me on how to achieve this functionality? Specifically:

  • How do I pass variable values when opening the catalog item from the portal?
  • Is there a recommended way to implement this using URL parameters or client scripts?
  • Any examples or best practices would be much appreciated.

Thank you in advance for your help!

Reference:
HTML :
<button ng-click="c.createNew()">Create New</button>

Server Script:

(function() {
  // Pass the URL to client script
  data.redirectUrl = "/sp?id=sc_cat_item&sys_id=67e2f2da4fff0200086eeed18110c7dd";
   
})();
Client Script:
api.controller = function() {
  var c = this;

  // The URL sent from server
  c.url = c.data.redirectUrl;

  // Function to open URL in new tab
  c.createNew = function() {
    window.open(c.url, '_blank');  // Opens URL in a new browser tab
  };
};




15 REPLIES 15

@CharanV66187530 

when use opens the catalog item through the new button, at that time only the URL will have that parameter and script will populate

If catalog item is opened directly then that parameter won't be there and script won't populate it

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar , I have given link like below but it's not working. Please help me on this.

URL : https://dev342371.service-now.com/sp?id=sc_cat_item&sys_id=67e2f2da4fff0200086eeed18110c7dd&port_num...

 

Ankur Bawiskar
Tera Patron

@CharanV66187530 

yes the only way is to include the data in URL parameter and then use onLoad catalog client script to get and set in variable

    data.redirectUrl = "/sp?id=sc_cat_item&sys_id=67e2f2da4fff0200086eeed18110c7dd&sysparm_port_number=1234";

onLoad catalog client script like this

function onLoad() {

    var url = top.location.href;
    var portNumber = new URLSearchParams(url).get("sysparm_port_number");
    g_form.setValue('variableName', portNumber);
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar , Thanks for reply, I am able to open the form, but I can't set the value of the port number. I want to set the value using widgets, not through a catalog client script. When I use a client script, the value is always set, but I want to set the value only under specific conditions.

My requirement is as follows:

1. After rejecting a catalog item, I want to display a  "Create New" button on the portal side.
2. When the user clicks this button, I want to populate all the data from the rejected ticket and set those values in a new form on the portal.

 

Please find attached images.

 

@Ankur Bawiskar  @Laveena-Agarwal ,I am getting Response is 'null'. Please help me on this?

Script Include:

var checkRequestItemGlobal = Class.create();
checkRequestItemGlobal.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getPortNumber: function(sysID) {
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('sys_id', sysID);
        gr.query();
        if (gr.next()) {
            return gr.variables.port_number.toString();
        }
    },
    type: 'checkRequestItemGlobal'
});


Client Script :
function onLoad() {
    var con = confirm("This request has been prepopulated from your previously rejected ticket. Please make sure to update the information that caused the rejection before resubmitting.");
    if (con) {
        var ga = new GlideAjax('global.checkRequestItemGlobal');
        ga.addParam('sysparm_name', 'getPortNumber');
        ga.getXMLAnswer(function(response) {
            alert("response :" + response);
            var result = JSON.parse(response);
            alert(result);

        });
    }
}

I am passing sys_id from portal widget :
var sysID = record.getValue('sys_id');
        var checkRequest = new global.checkRequestItemGlobal();
        var portNumber = checkRequest.getPortNumber(sysID);