Passing Multiple Values within CommandValue for more advanced flows


#1

Although probably not a common scenario, I hit a case where I needed to pass several values through an execute automation command and I have come up with an alternate solution available now using scripts to pass multiple variables as JSON.
I adopted this approach over using program settings as like the ‘on the fly’ flow plus some scenarios may not allow for it.

function newValues(input) {
	var input					=input.toString();
	var jsonArray				= {};
	var splitParams				= input.split(',');
	for (var p = 0; p < splitParams.length; p++) {
		var singleParam			= splitParams[p];
		var splitSingleParam	= singleParam.split('=');
		var paramName			= splitSingleParam[0].toString();
		var paramValue			= splitSingleParam[1].toString();
		jsonArray[paramName]	= paramValue;
	}
	return JSON.stringify(jsonArray);
}

function getValue(input,getValue){
	var jsonArray = JSON.parse(input);
	return jsonArray[getValue]
}

function addValues(input,newValues){
	if(input){
		var jsonArray = JSON.parse(input);
	}else{
		var jsonArray = {};
	}
	var splitParams				= newValues.split(',');
	for (var p = 0; p < splitParams.length; p++) {
		var singleParam			= splitParams[p];
		var splitSingleParam	= singleParam.split('=');
		var paramName			= splitSingleParam[0].toString();
		var paramValue			= splitSingleParam[1].toString();
		jsonArray[paramName]	= paramValue;
	}
	return JSON.stringify(jsonArray);
}

You would then place multiple values in the command value field using a CALL which will render as a JSON string for parsing in the rule later.

The scripts could probably do with some refinement and verification and you would need to be careful with special characters including quotes, double quotes, comma, colons and curly brackets which would interfere with JSON format. Be sure to test using show message or ask question to display values and check they are as expected.

Values should be entered as a comma separated list of name=value ie;
'<name1>=<value1>,<name2>=<value2>,<name3>=<value3>'
There is no limit to the number of name=value pairs.
In my example I used [?Prompt] for values to be entered for testing but you should be able to use most system variables available to the rule event or scenario used.


Calling values needs the JSON sting in [:CommandValue] input first followed by the name for the value you wish to call ie;
'[:CommandValue]','<name1>'


#2

Interesting.

Is there a way we can create like a drop-down menu with a list of options using this type of method?


#3

That is awesome @JTRTech love your last couple tutorials I am sure people can get good use out of those.


#4

Sure you can set drop-down in [?prompt]


#5

Really? How? Is there a command we add to the [?prompt] im unaware of?


#6

Sure there is something in the mask, comma separated list, its on forum somewhere, was playing with it a few weeks ago.


#7

@Shivan

Pipe not comma separated.

[?Choose a value;;<option1>|<option2>|<option3>|<option4>]

Sure 3rd section is mask, so its alternate to mask since dropdown would surpass the need for a mask.


#8

Ohh I never saw that before.

Thanks for the reference.


#9

Works Perfectly. Thanks for that :slight_smile:


#10

Didnt say in firt post but you can also add values and extend the JSON.

{CALL:commandValue.addValues(’[:CommandValue]’,’<name8>=<value8>,<name9>=<value9>’)} will add the new values to the input command value array.

I did add a little validation into that to take into account if the input commandvalue doesn’t have anything. May not work if input isnt valid JSON though so again test well.