Need to update the quantity field in catalog item

Savitha4
Tera Contributor

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.

1 ACCEPTED SOLUTION

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:

find_real_file.png

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

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

11 REPLIES 11

Saurav11
Kilo Patron
Kilo Patron
Yes on change client script You need to pass the ref valu to script include Query the table get the row count and pass it ack to client script Anf then set it on the field Please mark answer correct/helpful based on impact Thanks

Hello Saurav,

Thanks for the reply. Can you please share the script. I'm having trouble in that part. 

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:-

find_real_file.png

 

 

Script Include:-

find_real_file.png

 

Please mark my answer correct/helpful based on Impact.

Hello,

Did you get a chance to check?