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 ...
To achieve your goal, you can accomplish this in a few steps. Let's break down the solution into two major parts:
Creating a UI Action to Redirect from a Problem to the Catalog:
Automatically setting the "Parent" field in an Incident or RITM (Requested Item) to the Problem's number.
Part 1: Creating a UI Action to Redirect to the Catalog
UI Action Setup:
Navigate to the UI Actions module in your ServiceNow instance. This is typically done by searching for "UI Actions" in the application navigator.
Create a new UI Action associated with the Problem table.
Fill in the form with the appropriate details. Ensure you select the form where you want the action to appear, such as "Form Button" or "Form Link" based on your preference.
Scripting the Redirect:
In the Script field, you're going to use JavaScript to redirect the user to the catalog with parameters. Note that ServiceNow uses GlideRecord for querying records and window.location for redirection. Below is a pseudocode example for doing this:
javascript
// Replace 'catalog_sys_id' with your actual Catalog's Sys ID
// Replace 'catalog_item_sys_id' with the Catalog Item's Sys ID you wish to redirect to
var url = "com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=catalog_item_sys_id&sys_id=catalog_sys_id";
action.setRedirectURL(url);
Make sure to replace 'catalog_sys_id' and 'catalog_item_sys_id' with accurate Sys IDs of your Catalog and Catalog Item.
Note: The parameters for sysparm_id and sys_id might change based on your specific catalog configuration and what you intend to pass. You might also need to pass additional parameters to pre-fill fields in the Catalog form. Research or consult the ServiceNow documentation for more details on URL parameters.
Part 2: Setting the "Parent" Field in an Incident or RITM
Capturing Problem Number:
To automatically populate the "Parent" field when creating an Incident or RITM from a Problem record, you will likely need to implement a solution that captures the Problem's number or Sys ID and then pass it along to your Incident or RITM record.
Using Business Rules or Client Scripts:
For Client Scripts, you might need to write a script that gets executed when creating an Incident or RITM from the Problem form (usually through a reference field or a redirect). This can involve using the g_form.setValue() method to set the "Parent" field based on a URL parameter or a previously set global variable.
Business Rules could be set up to automatically update the new Incident or RITM based on certain conditions, such as if the creation is triggered from a Problem record. You can use the current object in a before or after insert business rule to set the "Parent" field based on the originating Problem record.
Final Note:
This is a high-level solution. Depending on your ServiceNow environment and custom requirements, you may need to adapt this solution. Consider your ServiceNow version, access permissions, and consider testing this solution in a sub-production instance before applying it in production. Additionally, the ServiceNow community and official documentation are valuable resources when working on customized solutions like this.
Your script conceptually addresses the task of redirecting from a Problem to the appropriate Catalog or Catalogs Home page depending on the number of active catalogs. However, there are a couple of improvements and points to consider:
Updating Current Record: The call to current.update(); seems to imply you're making changes to the current Problem record within the UI Action. Make sure this is necessary for your specific use case. If no changes are being made to the current Problem record that need saving prior to redirection, you could remove this line to avoid unintended updates or performance impacts.
Generating the Redirect URL: Your conditional logic for checking the number of active catalogs and generating the URL for redirection is correct. You correctly differentiate between redirecting to a specific catalog (catalog_home.do) and the general catalogs page (catalogs_home.do) based on the activeCatalogsCount.
URL Parameters and Usage:
Ensure that the parameters you are passing (sysparm_view, sysparm_parent_table, sysparm_parent_sys_id) are correctly utilized in your catalog item configurations. These parameters suggest you may be trying to associate the new catalog item directly with this Problem record or customize the view.
Confirm that catalog_default is the correct view for your use case. You may need to adjust sysparm_view depending on the views configured in your instance.
Consideration for Catalog Item Selection: If you have multiple active catalogs but want to direct the user to a specific item within one of those catalogs, you may need a more refined approach to select the appropriate catalog and catalog item. This script assumes redirection to a catalog home without specifying an item.
Redirect Execution: Using action.setRedirectURL(url); is the correct approach for redirecting the user based on the generated URL.
Code Improvement Suggestion:
If you're sure about updating the current Problem record each time this action is invoked, the core logic of your script is sound. If updating is not always necessary, consider adding a conditional check around current.update(); to only update when certain conditions are met (for example, specific field changes).
Here's a refined version of your script, minimally adjusted as your logic is already well-suited for the described functionality:
javascript
// Uncomment the line below if you need to save changes to the current record before redirecting.
// 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);
Additional Notes:
Always test your script in a non-production environment before deploying it to production to avoid unintended consequences.
If this UI action is intended for use in a scoped application, ensure that any API or method (such as sn_sc.Catalog.getCatalogCount()) is available and behaves as expected within your application's scope.
Finally, consider enhancing user experience through confirmation dialogs or additional notifications if significant actions (like data updates) are performed as part of this process.