Handle string array or string in multifield in workflow metadatamap | Community
Skip to main content
December 8, 2023
Solved

Handle string array or string in multifield in workflow metadatamap

  • December 8, 2023
  • 1 reply
  • 522 views

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[]?

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Saravanan_Dharmaraj

@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[]) {
  ...
}

 

1 reply

Saravanan_Dharmaraj
Community Advisor
Saravanan_DharmarajCommunity AdvisorAccepted solution
Community Advisor
December 8, 2023

@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[]) {
  ...
}