You are viewing limited content. For full access, please sign in.

Question

Question

Finding the index of a specific value in a multi-value token

asked on August 30, 2016 Show version history

I have a multi-value token, let's say ["Bob", "Jack", "Max", "Aaron"]. Those values are just examples, and will change every time.

I need to find the index of a specific value in this multi-value token. For example, for "Max", I need it to return 2 (starting from 0).

Is there an easy way to do this, or do I have to iterate through the token (either via a script or using For Each Value) and figure it out manually? I looked at the available functions in the token editor but couldn't find anything useful there.

EDIT: I should add some context to this...

I'm making a web request and receiving a JSON response. One item in the JSON structure is an array, like this:

metadata = [
    {
        "field_name": "Name",
        "field_value": "Bob"
    },
    {
        "field_name": "Date of Birth",
        "field_value": "11/20/1978"
    },
    {
        "field_name": "Has disability",
        "field_value": "No"
    }
]

The tricky part is that the items in the JSON array can be in a different order each time, and some may not exist. For instance, not every JSON payload has the "Has disability" piece of metadata.

So what I ended up doing was use the Read JSON activity to create two multi-value tokens:

Field Names = $.metadata[*].field_name

Field Values =  $.metadata[*].field_value

Now I need to pair them up using their index values (since Workflow doesn't natively support a key-value pair...). And in order to do that, I need to check the index location of each field name in the Field Names mv token, and look at that index in the Field Values token.

A pain in the neck, basically... frown

1 0

Replies

replied on August 30, 2016

Ege - Not sure if it answers your question but a one or two line script will return an index of an element if you provide the list and the item to look for.  (I didn't see a good way to do this in the token editor either);

protected override void Execute()
{
	List<object> mvList = this.GetTokenValue("mvList");
	object findValue = this.GetTokenValue("findValue");

	this.SetTokenValue("index", mvList.IndexOf(findValue));

}

Returns the index of the item in the 'index' token, returns a -1 if not found.  (Probably more academic than a good solution for you!)  wink

1 0
replied on August 30, 2016

Yeah, I actually tried an almost identical script using IndexOf(), which returned -1 when it should have returned the actual index of the item (which I knew for a fact existed in that particular dataset). So I got frustrated and posted here. wink

You're right though - we try to not use scripts too much, since they make workflows a lot less maintainable. I ended up iterating over the list using For Each Value and referring to the %(Iteration) index of the other multi-value token if %(Current Value) matches certain strings. Again, messy solution (I have like 8 branches...) but it'll do for now.

1 0
replied on August 30, 2016

Is there any value in a custom workflow activity that will accept a multivalue list and a key and return the index of the key in the multivalue list?  The list and the key could be  token enabled, maybe also configurable for exact key matches or case-sensitive matches, maybe being able to select how to cast the key for the match i.e. numeric, string, etc?  Also able to resolve multiple indexes if multiple multivalue lists and keys are provided?

Maybe another free workflow activity from Qfiche?  wink

0 0
replied on August 30, 2016

It's among the features I've asked for before.

http://answers.laserfiche.com/questions/88788/Workflow-feature-requests

0 0
replied on August 30, 2016

I must have somehow missed that post!  I will start to work on them ASAP and let you know as they are completed!  ;-)

You are not allowed to follow up in this post.

Sign in to reply to this post.