- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 11:54 AM
Hello,
I need to update the quantity in a catalog item based on number of records in the reference field of a variable before submission. I think I need to write a onChange catalog client script .Kindly provide assistance in writing the script. Thanks in advance.
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2022 10:01 PM
Okay.
I don't think you can modify the quantity based on the change of Multi Row Variable Set.
Have tried in my PDI when you add a new Row in MVRS , I have not seen any method to get the length of it based in which you can set the quantity of the cat item.
What you can do if you need this to be done with an On Change Script then that On Change Script need to be written on some other variable on the form and then you can achieve this using an On Change Script.
For example, I have a Variable as "Requested For" which I am selecting after adding Rows in MVRS and it will work fine:
Below script can be used:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue) {
var multiRowVariableSet = JSON.parse(g_form.getValue('demo_variable_set')); // Replace "variable_set_1" with your Variable Set Name. Make sure to replace it with Name and not with Label of Multi Row Variable Set
var numberOfRows = multiRowVariableSet.length;
g_cart.setQuantity(numberOfRows);
}
//Type appropriate comment here, and begin script below
}
Make sure the Rows are added to your MVRS and then you are changing the other variable value:
This works for me in my PDI. Let me know if you are facing an issue.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 11:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 06:09 PM
Hello Saurav,
Thanks for the reply. Can you please share the script. I'm having trouble in that part.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2022 01:04 AM
Hello,
Please find the scripts below:-
Onchange Client script:-
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user=g_form.getValue('user');//change to your reference field name
var ga = new GlideAjax('getcount');//script include name
ga.addParam('sysparm_name','getCount');//main function
ga.addParam('sysparm_user_name',user);//variable name
ga.getXML(getRetValue);
function getRetValue(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('test',answer);//change to the field test with your quantity field name
}
}
Script Inlcude:-
I quries the Incident table change the table name to yours
var getcount = Class.create();
getcount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCount: function() {
var count = new GlideAggregate('Incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if(count.next())
incidents = count.getAggregate('COUNT');
return incidents;
},
type: 'getcount'
});
Also attaching screenshot of both:-
Client Script:-
Script Include:-
Please mark my answer correct/helpful based on Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2022 09:02 AM
Hello,
Did you get a chance to check?