Question

Error when using 'patchItems' to update field value

  • 14 March 2024
  • 1 reply
  • 23 views

Badge +3

Hello all! I’m posting this as a new topic, although I believe it is related to Using ElementProxy to get data fields for selected Jira card. When I try to update a field value using ‘patchItems’ on the CollectionProxy I am getting an error message saying it “could not find collection...” 

I set up a simple demonstration in the code below for your review, with a hard-coded update value in string format. I picked a field called “summary” which is just a string to hopefully rule out type compatibility issues. Thanks again for all of your help!

Here’s my function getFieldValueFromElement, and you can see that all of the read the fields from the collection (and display the collection itself) up until I execute the patch command.

export async function getFieldValueFromElement(
client: EditorClient,
elementId: string,
fieldName: string
) {
// Obtain an ElementProxy for the given elementId
const elementProxy = new ElementProxy(elementId, client);
// Check if elementProxy is null or undefined
if (!elementProxy) {
throw new Error(`Element with ID ${elementId} not found.`);
}

// Iterate over reference keys to find the associated data item
for (const referenceKey of elementProxy.referenceKeys.values()) {
const dataItemProxy = referenceKey.getItem();
if (!dataItemProxy) {
continue; // Skip if no data item proxy is associated with this reference key
}

// Obtain the CollectionProxy from the DataItemProxy
const collectionProxy = dataItemProxy.collection;
if (collectionProxy) {
// Get all field names for the collection
const fields = collectionProxy.getFields();
console.log("Fields in Collection:", fields);
if (fields.includes(fieldName)) {
// Get the value for the specified field
const fieldValue = dataItemProxy.fields.get(fieldName);

if (fieldValue !== undefined) {
/************************************************************** */
// Test code for patch ability
// Assuming passed in field name contains a string value
/************************************************************** */

const testNewValue = "Test updated value";

console.log(`Attempting to change value of ${fieldName}`);
console.log(`Current Value of ${fieldName}:`, fieldValue);

const primaryKey = dataItemProxy.primaryKey;

console.log(`Primary Key:`, primaryKey);

const changedItems = new Map();
changedItems.set(primaryKey, { [fieldName]: testNewValue });

console.log("Changed Items Map:", changedItems);
console.log("CollectionProxy:", collectionProxy);

collectionProxy.patchItems({ changed: changedItems });

console.log(
`New Value of ${fieldName}:`,
dataItemProxy.fields.get(fieldName)
);
/************************************************************** */

return fieldValue; // Return the found value
}
}
}
}

// If the loop completes without returning, the field was not found
throw new Error(
`Field '${fieldName}' not found in any referenced data items.`
);
}

Here’s a capture of the console log:

 


Comments

Userlevel 3
Badge +7

Hi @mwyman thank you for your question! I understand you’re receiving ongoing support from our Development team. We will share an update in this thread when we can!

Reply