Run a workflow to update a property on an asset | Community
Skip to main content
Level 2
July 19, 2023
Solved

Run a workflow to update a property on an asset

  • July 19, 2023
  • 2 replies
  • 1012 views

Hello,

 

We've been trying to create a process where a workflow updates a property on an asset.

 

Has anyone had any success with creating a workflow like this?

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 EstebanBustamante

@rawvarun  explained how you need to approach your solution, you can check here a sample of how to do it https://redquark.org/aem/day-13-aem-workflows-2/ . 

 

I highlight the most important part took from the previous link:

@Component( service = WorkflowProcess.class, property = { PROCESS_LABEL + EQUALS + PROCESS_LABEL_VALUE } ) public class UpdateReferencedAssetsWorkflow implements WorkflowProcess { protected static final String PROCESS_LABEL_VALUE = "Update Referenced Asset"; private static final String TAG = UpdateReferencedAssetsWorkflow.class.getSimpleName(); private static final Logger LOGGER = LoggerFactory.getLogger(UpdateReferencedAssetsWorkflow.class); @Reference ReferencedAssetService referencedAssetService; @Reference ResourceResolverService resourceResolverService; @Override public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) { // Getting payload from Workflow - workItem -> workflowData -> payload String payloadType = workItem.getWorkflowData().getPayloadType(); LOGGER.debug("{}: Payload type: {}", TAG, payloadType); // Check type of payload; there are two - JCR_PATH and JCR_UUID if (StringUtils.equals(payloadType, "JCR_PATH")) { // Get the JCR path from the payload String path = workItem.getWorkflowData().getPayload().toString(); LOGGER.debug("{}: Payload path: {}", TAG, path); // Get process arguments which will contain the properties to update String[] processArguments = metaDataMap.get("PROCESS_ARGS", "default").split("="); // Get the referenced assets Map<String, Asset> referencedAssets = referencedAssetService.getReferencedAssets(path); LOGGER.debug("{}: Starting updating asset metadata with following values: {}", TAG, Arrays.toString(processArguments)); // Get resource resolver ResourceResolver resourceResolver = resourceResolverService.getResourceResolver(); try { // Loop for each referenced asset for (Map.Entry<String, Asset> entry : referencedAssets.entrySet()) { // Asset path String assetPath = entry.getKey(); LOGGER.debug("{}: Updating metadata for asset: {}", TAG, assetPath); // Get resource corresponding to the path of asset Resource assetResource = resourceResolver.getResource(assetPath); // Get the metadata for the asset resource Resource assetResourceMetadata = Objects.requireNonNull(assetResource).getChild("jcr:content/metadata"); // Get metadata properties as modifiable map ModifiableValueMap modifiableValueMap = Objects.requireNonNull(assetResourceMetadata).adaptTo(ModifiableValueMap.class); // Add the new property Objects.requireNonNull(modifiableValueMap).put(processArguments[0], processArguments[1]); } resourceResolver.commit(); } catch (PersistenceException e) { LOGGER.error("{}: exception occurred: {}", TAG, e.getMessage()); } } else { LOGGER.warn("{}: payload type - {} is not valid", TAG, payloadType); } } }

 

2 replies

rawvarun
Community Advisor
Community Advisor
July 19, 2023

You need to create a Custom process step in the Workflow Model. This custom process step will update the asset property.

https://experienceleague.adobe.com/docs/experience-manager-65/developing/extending-aem/extending-workflows/workflows-customizing-extending.html?lang=en

 

Also, you may need to create Workflow Launcher based on your use case.

 

 

mbrennanAuthor
Level 2
July 19, 2023

I don't see anything in that documentation around updating asset property. Do you have any more guidance or examples where this is a successful case?

EstebanBustamante
Community Advisor and Adobe Champion
EstebanBustamanteCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
July 19, 2023

@rawvarun  explained how you need to approach your solution, you can check here a sample of how to do it https://redquark.org/aem/day-13-aem-workflows-2/ . 

 

I highlight the most important part took from the previous link:

@Component( service = WorkflowProcess.class, property = { PROCESS_LABEL + EQUALS + PROCESS_LABEL_VALUE } ) public class UpdateReferencedAssetsWorkflow implements WorkflowProcess { protected static final String PROCESS_LABEL_VALUE = "Update Referenced Asset"; private static final String TAG = UpdateReferencedAssetsWorkflow.class.getSimpleName(); private static final Logger LOGGER = LoggerFactory.getLogger(UpdateReferencedAssetsWorkflow.class); @Reference ReferencedAssetService referencedAssetService; @Reference ResourceResolverService resourceResolverService; @Override public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) { // Getting payload from Workflow - workItem -> workflowData -> payload String payloadType = workItem.getWorkflowData().getPayloadType(); LOGGER.debug("{}: Payload type: {}", TAG, payloadType); // Check type of payload; there are two - JCR_PATH and JCR_UUID if (StringUtils.equals(payloadType, "JCR_PATH")) { // Get the JCR path from the payload String path = workItem.getWorkflowData().getPayload().toString(); LOGGER.debug("{}: Payload path: {}", TAG, path); // Get process arguments which will contain the properties to update String[] processArguments = metaDataMap.get("PROCESS_ARGS", "default").split("="); // Get the referenced assets Map<String, Asset> referencedAssets = referencedAssetService.getReferencedAssets(path); LOGGER.debug("{}: Starting updating asset metadata with following values: {}", TAG, Arrays.toString(processArguments)); // Get resource resolver ResourceResolver resourceResolver = resourceResolverService.getResourceResolver(); try { // Loop for each referenced asset for (Map.Entry<String, Asset> entry : referencedAssets.entrySet()) { // Asset path String assetPath = entry.getKey(); LOGGER.debug("{}: Updating metadata for asset: {}", TAG, assetPath); // Get resource corresponding to the path of asset Resource assetResource = resourceResolver.getResource(assetPath); // Get the metadata for the asset resource Resource assetResourceMetadata = Objects.requireNonNull(assetResource).getChild("jcr:content/metadata"); // Get metadata properties as modifiable map ModifiableValueMap modifiableValueMap = Objects.requireNonNull(assetResourceMetadata).adaptTo(ModifiableValueMap.class); // Add the new property Objects.requireNonNull(modifiableValueMap).put(processArguments[0], processArguments[1]); } resourceResolver.commit(); } catch (PersistenceException e) { LOGGER.error("{}: exception occurred: {}", TAG, e.getMessage()); } } else { LOGGER.warn("{}: payload type - {} is not valid", TAG, payloadType); } } }

 

Esteban Bustamante