How to use MRVS variables in catalog client script

Asmita7
Tera Expert

I have created MRVS variable (agent_details) in a catalog item. MRVS variables are :

1. agent_name

2. recurring_cost

3. forming_cost

4. entity_name

The requirement is that the forming cost should exceed $1000 considering all records created in this MRVS.

eg. If end user creates first MRVS record as:

['Anthony', 100, 500, 'IT']

Now, when the End user is creating second record in MRVS, then after entering forming_cost, an on_change script should run and the employee should not be able select the forming_cost >500 since already 500 $ is utilized in first record.

Number of records may be multiple.

For this I am trying to write an on_change client script on forming_cost field. I am trying to fetch forming cost but not able to do so:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
var multiRowVariableSet = JSON.parse(g_form.getValue('agent_details'));
    var total = 0;
    for (var i = 0; i < multiRowVariableSet.length; i++) {
        total += multiRowVariableSet[i].forming_cost;
    }
///// this code is not working
    g_form.addInfoMessage("Total = ", total);     //// It is not fetching the value for MRVS

 

if( (total + newValue)>1000){

g_form.addInforMessage("Total forming cost exceeding 1000 $");

return false;

}

 

Note : All calculation will happen before we submit the catalog request or saving the form.

Please suggest the correction or new approach.

 

Thank you

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Asmita7 

are you getting the forming cost for the previous rows?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar,

Thank you for your time. 

No. I tried but no luck.

Actually, I need to fetch the forming cost on the form itself before the catalog request is submitted. But not able to do so.

 

Please suggest.

Prince Arora
Tera Sage

Hello @Asmita7 ,

 

Let me suggest you a approach which you can try for your implementation, You can write an OnSubmit client script on mvrs with the below script as example:

 
var formingData = g_form.getValue("formingData");
if(sessionStorage.getItem("data") == null){ //set Value for first time
sessionStorage.setItem("data", formingData);
}else{
var sessionData = sessionStorage.getItem("data");
var total = parseInt(sessionData) + parseInt(formingData);
if(total > 1000){
g_form.addErrorMessage("As total > 1000, please update the value");
return false;
}else{
sessionStorage.setItem("data",total);
}
}
 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.