Multi-Row Variable Set: Client Script to Do Something on the Cat Item when a new row is added

Jamsta1912
Tera Guru

Hello all,

I have a catalog item that includes a multi-row variable set. I've just read this handy little article about how to access the content of rows that have been added:

https://community.servicenow.com/community?id=community_article&sys_id=2a508caedbf47f48d82ffb2439961...

What I want to do is trigger a client script on the catalog item itself (not the variable set) to update another variable on the catalog item. Specifically, it will total up the figures in one column of the multi-row variable set. But I can't see how to trigger this when a row is added. I know that on submit client scripts are not available to multi-row variable sets, but in any case I think the client script would need to run at the level of the catalog item itself.

Here's a screenshot to clarify...

find_real_file.png

So, as a row is added, I want to update the variable at the bottom, 'Total Cost (£ / Euro)' to be the sum of the figures in the final column on each row in the variable set, 'Cost Per Item (£ / Euro)'. I'm able to access the values OK in a client script - my dilemna is all about how to trigger the rule.

Any thoughts?

Thank you.

 

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

I am thinking of a two fold solution.

The trigger should be on the Multi Row Variable Set. Because that is changing. Something like this, onChange on for example total cost in the MRVS:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	if(parent.g_form){
        parent.g_form.setValue('trigger_calculation', true);
    }
}

 

The trigger_calculation is a new Variable on the Catalog item. On the Catalog Item we can then have an onChange on the trigger_calculation field like so:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //get the MRVS
    var multiRowVariableSet = JSON.parse(g_form.getValue('variable_set_1'));

    var totalCost = 0
    for (var i = 0; i < multiRowVariableSet.length; i++) {
        totalCost += multiRowVariableSet[i].totalCostVariableInMRVS;//add all totals from the MRVS to 1 total
        }
    }
    g_form.setValue("trigger_calculation", false);//reset the trigger
    g_form.setValue("totalCostOnCatalogItem");//set the total
    
}

View solution in original post

17 REPLIES 17

Hi Willem,

I haven't quite got this to work, but I can still see that it will work. In my case, it's further complicated by the fact that the totalCostVariableInMRVS is itself updated during the input of the row, by other client scripts at the level of the variable set. So I just need to make some refinements - but I will be sticking with this methodology, thank you!

Hey jamsta,

how did you end up getting the most recent input into the MRVS?

I'm having the same issue as you.

Thanks!

 

 

Thank you for the this post as well as your others on manipulating MRVS data. The issue I'm running into in your example above is how to trigger on that "new" variable created in the first step.

find_real_file.png

On the second Catalog Client Script, u_trigger_calculation is not available as a dropdown for variable name onChange:

find_real_file.png

I tried creating a Yes / No variable on the Catalog Item level with the name u_trigger_calculation but that had no effect. Any ideas on what I'm missing?

Hi William,

I tried the same approach for one of the use case, where I need data from outside MRVS and based on that updating variables in MRVS. But unfortunately, parent.g_form is not working in my case.

Regards,

Manisha Kumari

can u send me script include.

scripting