Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Handle string array or string in multifield in workflow metadatamap

Avatar

Level 5

I am using the below code to fetch info from workitem

 

MetaDataMap inputMap = workItem.getMetaDataMap();
String[] customList = (String[]) inputMap.get("customList");

The "customList" is a textfield defined as multifield -

<customList
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
name="customList"/>
</customList>

this throws a class cast exception when only value is configured in the multifield.

java.lang.ClassCastException: class java.lang.String cannot be cast to class [Ljava.lang.String

this is because of the (String[]) casting. what will be the best way to correct it for both String and String[]?

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@aem_noob If its jcr:property you can check with isMultiple() method before reading. But in this case, you can use instanceof before reading 

 

if (x instanceof String) {
  ...
}

if (x instanceof String[]) {
  ...
}

 

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

@aem_noob If its jcr:property you can check with isMultiple() method before reading. But in this case, you can use instanceof before reading 

 

if (x instanceof String) {
  ...
}

if (x instanceof String[]) {
  ...
}