Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Custom Catalog Item modification Request

Gaurav Vaze
Kilo Sage

Hello developers,
Today I have one unique requirement as follows:

1] I have to create one variable (I don't know what its type should be a label/custom) which will appear under each service catalog item at the bottom and will say the following:
This catalog Item is owned by <catalog item owner name>, to make any change request to this catalog item, CLICK HERE

now when the user clicks on click here, the user should be redirected to a new catalog item which is previously configured named as Update Catalog Item
the variables are as follows:

Catalog Item name(reference to sc_cat_item)
Short description
Description
Service Owner

These fields should be populated from the catalog item that we clicked previously(for example, if I clicked the CLICK HERE from xyz cat item, then these fields should have the details of that catalog item)


Now I don't know how I can set the service owner dynamically as mentioned in 1]
and secondly how the details can be populated

Any help will be appreciated

1 ACCEPTED SOLUTION

Gaurav Vaze
Kilo Sage

 I got the answer to this
I had to create a widget and a macro
and rest thing were handle using the URL parameters, thus was able to dynamically populate the things
Thank you!!

View solution in original post

4 REPLIES 4

Mark Manders
Mega Patron

My instance has problems waking up, so this AI solution is not validated, but it could give you a start to try:

 

To achieve your requirements, follow these steps to create the necessary functionality in ServiceNow:

Step 1: Create a Variable in the Catalog Item
1. **Type:** Use a "Label" variable to display the message.
2. **Placement:** Add this variable to the bottom of each Service Catalog Item form.

Here's an example of the label text:

This catalog item is owned by <catalog item owner name>. To make any change request to this catalog item, <a href="javascript&colon;redirectToUpdate()">CLICK HERE</a>

 

Step 2: Script to Handle Redirection and Data Population
Create a Client Script to handle the redirection when the user clicks the link.

**Client Script:**

function redirectToUpdate() {
  var currentCatItem = g_form.getValue('sys_id');
  var ga = new GlideAjax('CatalogItemUpdateRedirector');
  ga.addParam('sys_id', currentCatItem);
  ga.getXMLAnswer(function(response) {
    var targetURL = response.responseXML.documentElement.getAttribute("answer");
    window.location.href = targetURL;
  });
}

 

Step 3: GlideAjax Script Include
Create a Script Include that handles the creation of the new Catalog Item and populates the fields.

**Script Include:**

var CatalogItemUpdateRedirector = Class.create();
CatalogItemUpdateRedirector.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  
  getRedirectURL: function() {
    var sysId = this.getParameter('sys_id');
    var catItemGR = new GlideRecord('sc_cat_item');
    if (catItemGR.get(sysId)) {
      var updateCatItem = new GlideRecord('sc_cat_item'); // Assuming 'Update Catalog Item' is a catalog item
      updateCatItem.initialize();
      updateCatItem.short_description = catItemGR.short_description;
      updateCatItem.description = catItemGR.description;
      updateCatItem.u_service_owner = catItemGR.u_service_owner; // Assuming 'u_service_owner' is the field storing the owner
      updateCatItem.insert();
      
      var url = "/sc_cat_item_view.do?sys_id=" + updateCatItem.sys_id;
      return url;
    }
    return "";
  }
});


Step 4: Catalog Client Script to Populate Fields
In the "Update Catalog Item" form, create an OnLoad Client Script to populate fields if redirected.

**OnLoad Client Script:**

function onLoad() {
  var refCatItem = g_form.getParameter('sys_id');
  if (refCatItem) {
    var ga = new GlideAjax('CatalogItemPopulator');
    ga.addParam('sys_id', refCatItem);
    ga.getXMLAnswer(function(response) {
      var data = JSON.parse(response);
      g_form.setValue('short_description', data.short_description);
      g_form.setValue('description', data.description);
      g_form.setValue('u_service_owner', data.u_service_owner);
    });
  }
}

 

Step 5: Script Include to Provide Data for Population
Create another Script Include that fetches the data for the "Update Catalog Item" form.

**Script Include:**

var CatalogItemPopulator = Class.create();
CatalogItemPopulator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  
  getCatalogItemData: function() {
    var sysId = this.getParameter('sys_id');
    var catItemGR = new GlideRecord('sc_cat_item');
    if (catItemGR.get(sysId)) {
      var data = {
        short_description: catItemGR.short_description,
        description: catItemGR.description,
        u_service_owner: catItemGR.u_service_owner
      };
      return JSON.stringify(data);
    }
    return JSON.stringify({});
  }
});

 


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

Thank you for the response
However, your solution doesn't gives how I'll get the catalog service ower name as I mentioned in step 1

I want each catalog Items name dynamically in the <> place too

Gaurav Vaze
Kilo Sage

 I got the answer to this
I had to create a widget and a macro
and rest thing were handle using the URL parameters, thus was able to dynamically populate the things
Thank you!!