How to update the Catalog Task's cmdb_ci field based on the catalog form variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2023 06:15 AM - edited 08-24-2023 06:16 AM
As per mentioned, I need some help with getting a catalog task cmdb_ci field populated with certain CI based on one of the catalog form variable.
I tested out with the script below but its not working..
function onSubmit() {
var OrgValue = g_form.getValue(OS);
if (OrgValue === 'Windows10') {
g_form.setValue('cmdb_ci', 'Windows10 Support Team');
} else if (OrgValue === 'Linux') {
g_form.setValue('cmdb_ci', 'Linux Support Team');
} else if (OrgValue === 'Mac') {
g_form.setValue('cmdb_ci', 'Mac Support Team');
}
}
So basically for the variable OS, if the value is either one of the above, then it will auto update the cmdb_ci value
I also came across a post where it can done via flow designer - https://www.servicenow.com/community/itsm-forum/update-form-field-with-catalog-item-variable-input-f... but somehow I am stuck at the part on how to add the value "1 -> instruction text" -- as per attachment..
Thank you in advance..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2023 06:42 AM
@Happy S ,
cmdb_ci on the task level is a reference field. You should be giving sys_id in the second parameter of setValue rather than names. Also when you do
var OrgValue = g_form.getValue(OS);
Is OS the backend name of your variable. Can you put it in quotes while getting the value
like g_form.getValue('OS');
alert it to see whether you are getting the value or not.
Next, for your question related to flow designer. You can do it similar to the way, its done there. You can have the 'Get catalog variables' where you can specify your catalog item name and then fetch all the variables from the data pill. But as you need to update on the task level. You will need to do 'Look up record', then find your record by filtering request_item is you ritm created, which you will be able to get from the trigger record, as you trigger will be 'Service Catalog' and then using, 'Update record'. You should be able to pull the variable value and set it in cmdb_ci field.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2023 07:26 AM
Hello @Happy S,
There are a few possible reasons why your script is not working as expected. Here are some suggestions to troubleshoot and fix your script:
- Make sure that you have defined the OS variable as a question choice type with the values Windows10,...1. If you have defined it as a different type, such as a reference or a list collector, you may need to....
- Make sure that you have enclosed the OS variable name in quotes when calling g_form.getValue, like this: var OrgValue = g_form.getValue('OS');. If you omit the quotes, the script will treat OS as an undefined variable and throw an error.
- Make sure that the values that you are setting for the cmdb_ci field are valid sys_ids of existing CIs in the cmdb_ci table. If you are using names instead of sys_ids, such as ‘Windows10 Support Team’, you may need to use a GlideRecord query to get the corresponding sys_id, like this: var gr = new GlideRecord('cmdb_ci'); gr.addQuery('name', 'Windows10 Support Team'); gr.query(); if (gr.next()) { g_form.setValue('cmdb_ci', gr.sys_id); }.
- Make sure that you have added your script as a client script on the catalog item, and that you have selected the onSubmit option for the type field. If you have added your script as a different type of script, such as a UI policy or a catalog client script, it may not run when the form is submitted.
Alternatively, you can also use the flow designer to populate the cmdb_ci field based on the OS variable. To do this, you need to follow these steps:
- Navigate to Flow Designer > Flows and click New to create a new flow.
- Give your flow a name and description, and select Catalog Item Requested as the trigger.
- Select the catalog item that contains the OS variable as the source table.
- Add a Switch action to your flow and configure it as follows:
- For the Input value, select Requested Item > Variables > OS.
- For each case, enter one of the possible values of the OS variable, such as Windows10, Linux, or Mac.
- For each case, add a Set Values action and configure it as follows:
- For the Table, select Requested Item.
- For the Field, select Configuration item.
- For the Value, enter or select the sys_id of the CI that you want to set for that case.
- Save and publish your flow.
This flow will run whenever a user requests the catalog item and will set the cmdb_ci field of the requested item based on the value of the OS variable.
Hope this helps.
Kind Regards,
Swarnadeep Nandy