Auto Populate Count based on another variable in Catalog item

Surendra6
Tera Contributor

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.

3 REPLIES 3

AJ-TechTrek
Giga Sage
Giga Sage

Hi @Surendra6 ,

 

Refer the below example, which help you on this.

 

https://www.servicenow.com/community/itsm-forum/automatically-populate-one-variable-on-a-catalog-ite...

 

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

Sandeep Rajput
Tera Patron
Tera Patron

@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.

Pratiksha2
Mega Sage

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