Auto Populate Count based on another variable in Catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 12:40 AM
Hello,
I have requirement of 2 variables which are in the catalog item. Based on the first variables Arrays selection the second variable will show the count of the variable which is taken in the first variable.
Please someone please suggest how can we achieve this requirement.
Thank you in Advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 12:43 AM
Hi @Surendra6 ,
Refer the below example, which help you on this.
Please appreciate the efforts of community contributors by marking appropriate response as Mark my Answer Helpful or Accept Solution this may help other community users to follow correct solution in future.
Thanks
AJ
Linkedin Profile:- https://www.linkedin.com/in/ajay-kumar-66a91385/
ServiceNow Community Rising Star 2024

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 12:46 AM
@Surendra6 You can write an onChange client script on the first variable. As soon as user makes any changes on the first variable, the onChange script would get called.
Inside the script, you can get the value of the first variable, if it is a comma separated string sys_ids you can use string.split(',') to get an array of the sys_ids.
Finally you can get the count using array.length and show the same count in the second variable.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var stringArray = newValue.split(',') ;
g_form.setValue('second_variable',stringArray.length);
}
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 01:07 AM
Hello @Surendra6 -
Please write OnChange Client script and try below code:
var variable1 = g_form.getValue('//variable1_name');
var count = 0;
if (variable1) {
var variable1Array = variable1.split(',');
count = variable1Array.length;
}
g_form.setValue('//variable2_name', count);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Pratiksha