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

One of my colleague did use the this.NOW and it works to trigger the calculation when the variable changes. 

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
    
}

Thank you Willem, this looks like a neat idea. I didn't know we could make references like 'parent.g_form...'. I will try this out and let you know how it goes.