0
Answered

Accessing a multivalue String attribute

Carol Wapshere 1 year ago in PowerShell connector • updated by Curtis Lusmore 1 year ago 3

Been searching the doco and other Voice questions but I can't find this one - how do I access a multivalue string attribute in a PowerShell connector script?

I have tried $entity["attrib"].Values, but that doesn't work.

Using $entity["attrib"].Value seems to return a long string with all the values joined by semi-colons - at least that's how it gets written to the logger. I tried splitting on semi-colon but got "Method invocation failed because [Unify.Framework.StringValue] does not contain a method named "Split"."

It would be helpful if there were examples for different data types including multi-value on this page: https://voice.unifysolutions.net/knowledge-bases/7/articles/2912-powershell-connector-entities.

Affected Versions:
Fixed by Version:

Answer

Answer
Answered

Hi Carol, Thanks for raising this.

When you use the indexing operator on an entity, you get an IValue object, which contains a Value member containing the raw .Net value for that field.

In the case of a multi-value, the raw .Net value will be a List<IValue> containing the individual values - you may then need to access the Value member of each of those to access the individual raw .Net values.

As an example, consider the following script, where MV is a multi-valued integer field.

foreach ($entity in $components.InputEntities) {
    $values = $entity['MV'].Value; # List of IntegerValue
    $logger.LogWarning($values.GetType()); # Logs System.Collections.Generic.List[Unify.Framework.IntegerValue]
    $count = 0;
    $values | % { $logger.LogWarning($_.GetType()); $count += $_.Value } # Logs Unify.Framework.IntegerValue and sums raw .Net integer values into $count
}

Please let me know if this example clarifies this for you, and I'll update the documentation as you suggest.

Answer
Answered

Hi Carol, Thanks for raising this.

When you use the indexing operator on an entity, you get an IValue object, which contains a Value member containing the raw .Net value for that field.

In the case of a multi-value, the raw .Net value will be a List<IValue> containing the individual values - you may then need to access the Value member of each of those to access the individual raw .Net values.

As an example, consider the following script, where MV is a multi-valued integer field.

foreach ($entity in $components.InputEntities) {
    $values = $entity['MV'].Value; # List of IntegerValue
    $logger.LogWarning($values.GetType()); # Logs System.Collections.Generic.List[Unify.Framework.IntegerValue]
    $count = 0;
    $values | % { $logger.LogWarning($_.GetType()); $count += $_.Value } # Logs Unify.Framework.IntegerValue and sums raw .Net integer values into $count
}

Please let me know if this example clarifies this for you, and I'll update the documentation as you suggest.

Thanks Curtis - I think the missing piece is an extra ".Value". I thought I was getting a collection of strings rather than a collection if iValues.

This looks to be working now:

foreach ($val in $entity["attrib"].Value)
{
     $val.Value ## Do something with this
}

Excellent, thanks for confirming. I'll update the documentation with the above example.