Scriptrunner Confluence - Communardo Metadata
Asked by Michal Selinger:

Dear Community,

I would love to set Communardo Metadata on Confluence Page using Scriptrunner's script.

I am able to set String, Date, User fields easily. I am just simply unable to set Select or Dropdown Metadata **++at all++**. I have tried multiple options but I have failed all the time.

**Just a little overview:**

++String Metadata++

```
createDataObject("My value");
```

++Date Metadata++

```
createDataObject("YYYY-MM-DD"); // This depends on your Confluence system date format
```

++User Metadata++

```
createDataObject("[{\"key\":\"" + user1.key + "\",\"name\":\"" + user1.name + "\"},{\"key\":\"" + user2.key + "\",\"name\":\"" + user2.name + "\"}]");
```

++Dropdown / Select Metadata++

???

```
createDataObject(310); // MissingMethodException: No signature of method: com.communardo.confluence.metadata.field.predefined.DropdownMetadataFieldType.createDataObject() is applicable for argument types: (Integer) values: [310]
createDataObject("310"); // MetadataImportException: Parsing data object export format failed
createDataObject("[310]"); // MetadataImportException: Creating data object from export format failed because not all predefined values exist

```

When I get the current value of dropdown Metadata (via  
*loadContentMetadataValue* method), I get e.g. **\[310\]** . When I try to save the same value back (via *saveContentMetadataValue*) I am getting above errors.

Does anyone have experience with update of Dropdown or Select Communardo Metadata fields on Confluence Page using Scriptrunner?

Thank you for any inputs.

**Code**

```
// Components
import com.atlassian.sal.api.component.ComponentLocator;
// Page
import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.pages.PageManager;
// ScriptRunner
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
// Metadata
@WithPlugin("com.communardo.confluence.metadata")
import com.communardo.confluence.metadata.service.MetadataManager
import com.communardo.confluence.metadata.*;

// Managers
PageManager pageManager = ComponentLocator.getComponent(PageManager) as PageManager;
MetadataManager metadataManager = ScriptRunnerImpl.getOsgiService(MetadataManager) as MetadataManager;

// Test page
Page page = pageManager.getPage(191223349);

// Metadata field
MetadataField mf = metadataManager.loadMetadataFieldByKey("metadatafield.mainprocess", page.space);
DataObject mfVal = metadataManager.loadContentMetadataValue(page, mf);

// Properties
log.warn "Metadata field properties: " + mf.properties;
log.warn "Metadata page value properties: " + mfVal.value.properties;

// Load current metadata field value(s) from test page
log.warn "Current metadata value = " + mfVal;

// Save back the same value

try {
    log.warn "=== SETTING 310 ===";
    metadataManager.saveContentMetadataValue(page, mf, mf.getType().createDataObject(310));

} catch (Exception e) { log.warn e.toString(); }

try {
    log.warn "=== SETTING \"310\" ===";
    metadataManager.saveContentMetadataValue(page, mf, mf.getType().createDataObject("310"));

} catch (Exception e) { log.warn e.toString(); }

try {
    log.warn "=== SETTING \"[310]\" ===";
    metadataManager.saveContentMetadataValue(page, mf, mf.getType().createDataObject("[310]"));

} catch (Exception e) { log.warn e.toString(); }
```

**Log**

```
2023-11-23 10:39:04,406 WARN [runner.ScriptBindingsManager]: Metadata field properties: [title:Main process, description:, id:316, key:metadatafield.mainprocess, required:false, hidden:false, type:DROPDOWN, space:Space{key='ABC'}, PREFIX_METADATAKEY:metadatafield., PREFIX_GLOBAL:global., class:class com.communardo.confluence.metadata.MetadataField]
2023-11-23 10:39:04,410 WARN [runner.ScriptBindingsManager]: Metadata page value properties: [modCount:1, serialVersionUID:8683452581122892189, DEFAULT_CAPACITY:10, EMPTY_ELEMENTDATA:[], DEFAULTCAPACITY_EMPTY_ELEMENTDATA:[], elementData:[310], size:1, MAX_ARRAY_SIZE:2147483639, class:class java.util.ArrayList, empty:false]
2023-11-23 10:39:04,410 WARN [runner.ScriptBindingsManager]: Current metadata value = [310]
2023-11-23 10:39:04,410 WARN [runner.ScriptBindingsManager]: === SETTING 310 ===
2023-11-23 10:39:04,411 WARN [runner.ScriptBindingsManager]: groovy.lang.MissingMethodException: No signature of method: com.communardo.confluence.metadata.field.predefined.DropdownMetadataFieldType.createDataObject() is applicable for argument types: (Integer) values: [310] Possible solutions: createDataObject(java.util.Map), createDataObject(java.lang.String), createDataObject(java.util.Map), removeDataObject(com.atlassian.confluence.core.ContentEntityObject), removeDataObject(com.atlassian.confluence.core.ContentEntityObject)
2023-11-23 10:39:04,411 WARN [runner.ScriptBindingsManager]: === SETTING "310" ===
2023-11-23 10:39:04,412 WARN [runner.ScriptBindingsManager]: com.communardo.confluence.metadata.MetadataImportException: Parsing data object export format failed
2023-11-23 10:39:04,412 WARN [runner.ScriptBindingsManager]: === SETTING "[310]" ===
2023-11-23 10:39:04,414 WARN [runner.ScriptBindingsManager]: com.communardo.confluence.metadata.MetadataImportException: Creating data object from export format failed because not all predefined values exist
```


Answers:
author: Michal Selinger
body: Code attached.


author: Michal Selinger
body: **FYI - Code**

```
// Components
import com.atlassian.sal.api.component.ComponentLocator;
// Page
import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.pages.PageManager;
// ScriptRunner
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
// Metadata
@WithPlugin("com.communardo.confluence.metadata")
import com.communardo.confluence.metadata.service.MetadataManager
import com.communardo.confluence.metadata.*;
```

```
// Managers
PageManager pageManager = ComponentLocator.getComponent(PageManager) as PageManager;
MetadataManager metadataManager = ScriptRunnerImpl.getOsgiService(MetadataManager) as MetadataManager;

// Test page
Page page = pageManager.getPage(191223349);
// Metadata field
MetadataField mf = metadataManager.loadMetadataFieldByKey("metadatafield.mainprocess", page.space);
DataObject mfVal = metadataManager.loadContentMetadataValue(page, mf);

// Properties
log.warn "Metadata field properties: " + mf.properties;
log.warn "Metadata page value properties: " + mfVal.value.properties;

// Load current metadata field value(s) from test page
log.warn "Current metadata value = " + mfVal;

// Save back the same value

try {
    log.warn "=== SETTING 310 ===";
    metadataManager.saveContentMetadataValue(page, mf, mf.getType().createDataObject(310));
} catch (Exception e) { log.warn e.toString(); }

try {
    log.warn "=== SETTING \"310\" ===";
    metadataManager.saveContentMetadataValue(page, mf, mf.getType().createDataObject("310"));
} catch (Exception e) { log.warn e.toString(); }

try {
    log.warn "=== SETTING \"[310]\" ===";
    metadataManager.saveContentMetadataValue(page, mf, mf.getType().createDataObject("[310]"));
} catch (Exception e) { log.warn e.toString(); }


```

> **Log**
>
> 2023-11-23 10:39:04,406 WARN \[runner.ScriptBindingsManager\]: Metadata field properties: \[title:Main process, description:, id:316, key:metadatafield.mainprocess, required:false, hidden:false, type:DROPDOWN, space:Space{key='DBINTRA'}, PREFIX_METADATAKEY:metadatafield., PREFIX_GLOBAL:global., class:class com.communardo.confluence.metadata.MetadataField\] 2023-11-23 10:39:04,410 WARN \[runner.ScriptBindingsManager\]: Metadata page value properties: \[modCount:1, serialVersionUID:8683452581122892189, DEFAULT_CAPACITY:10, EMPTY_ELEMENTDATA:\[\], DEFAULTCAPACITY_EMPTY_ELEMENTDATA:\[\], elementData:\[310\], size:1, MAX_ARRAY_SIZE:2147483639, class:class java.util.ArrayList, empty:false\] 2023-11-23 10:39:04,410 WARN \[runner.ScriptBindingsManager\]: Current metadata value = \[310\] 2023-11-23 10:39:04,410 WARN \[runner.ScriptBindingsManager\]: === SETTING 310 === 2023-11-23 10:39:04,411 WARN \[runner.ScriptBindingsManager\]: groovy.lang.MissingMethodException: No signature of method: com.communardo.confluence.metadata.field.predefined.DropdownMetadataFieldType.createDataObject() is applicable for argument types: (Integer) values: \[310\] Possible solutions: createDataObject(java.util.Map), createDataObject(java.lang.String), createDataObject(java.util.Map), removeDataObject(com.atlassian.confluence.core.ContentEntityObject), removeDataObject(com.atlassian.confluence.core.ContentEntityObject) 2023-11-23 10:39:04,411 WARN \[runner.ScriptBindingsManager\]: === SETTING "310" === 2023-11-23 10:39:04,412 WARN \[runner.ScriptBindingsManager\]: com.communardo.confluence.metadata.MetadataImportException: Parsing data object export format failed 2023-11-23 10:39:04,412 WARN \[runner.ScriptBindingsManager\]: === SETTING "\[310\]" === 2023-11-23 10:39:04,414 WARN \[runner.ScriptBindingsManager\]: com.communardo.confluence.metadata.MetadataImportException: Creating data object from export format failed because not all predefined values exist


Tags: confluence-server

scriptrunner

server