Populate the parent field with the sys_id of the record it was openned from

F_bio Santos
Kilo Sage

Hi everyone, Im trying to create a UI Action that will redirect the user from the "Problem" to the Catalog, and when the user opens a "Incident" or a "Ritm" the "Parent" field should be the number of the problem is was created from, but Im not being abble to make this work ... 

Does anyone know how to do this?

Code that I have right now:

//Update saves incidents before going to the catalog homepage
current.update();
var url;
var activeCatalogsCount = sn_sc.Catalog.getCatalogCount();
if (activeCatalogsCount === 1) {
    url = "catalog_home.do?sysparm_view=catalog_default&sysparm_parent_table=" + current.sys_class_name + "&sysparm_parent_sys_id=" + current.sys_id;
}
else {
    url = "catalogs_home.do?sysparm_view=catalog_default&sysparm_parent_table=" + current.sys_class_name + "&sysparm_parent_sys_id=" + current.sys_id;
}


action.setRedirectURL(url);
2 REPLIES 2

Ivan Betev
Mega Sage
Mega Sage

Hi @F_bio Santos ,

 

Look for Request Parent Mapping module and create mapping for problem like below:

IvanBetev_0-1710683773426.png

 

Then try to create request from the problem with the code above (I presume you copied it from Incident), it should work. It is at least in my case.

 

Regards, Ivan

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @F_bio Santos ,

 

It seems like you're trying to accomplish two tasks:

  1. Redirecting the user from the "Problem" record to the Catalog homepage.
  2. Setting the "Parent" field of an "Incident" or "RITM" record to the number of the problem it was created from.

Let's address these tasks separately:

  1. Redirecting from Problem to Catalog:

Your code snippet seems generally correct for redirecting users to the catalog homepage. Ensure that sn_sc.Catalog.getCatalogCount() returns the correct count of active catalogs. Also, make sure that the URL construction is accurate for your instance.

  1. Setting Parent Field for Incident/RITM:

To set the Parent field of an Incident or RITM to the number of the problem it was created from, you'll need to modify the script a bit. Assuming that the Incident/RITM is being created from a Problem and the link between them is established through a field (let's say problem_id), you can achieve this by querying the Problem record and then setting the Parent field accordingly.

Here's an example of how you might do this:

 

// Check if the current record is an Incident or a RITM
if (current.sys_class_name == 'incident' || current.sys_class_name == 'sc_req_item') {
    // Query the Problem record based on the problem_id field
    var problemGr = new GlideRecord('problem');
    if (problemGr.get(current.problem_id)) {
        // If the Problem record is found, set the Parent field to the Problem number
        current.parent = problemGr.number;
        // Update the current record
        current.update();
    }
}

// Redirect users to the Catalog homepage
var url;
var activeCatalogsCount = sn_sc.Catalog.getCatalogCount();
if (activeCatalogsCount === 1) {
    url = "catalog_home.do?sysparm_view=catalog_default";
} else {
    url = "catalogs_home.do?sysparm_view=catalog_default";
}
action.setRedirectURL(url);

 

 

Make sure to replace 'problem' with the correct table name if it differs in your instance. Also, ensure that problem_id is the correct field name that links the Incident/RITM to the Problem record.

Remember to test this code thoroughly in a non-production environment before deploying it to your live instance.

 

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)