Workflow script unable to find assetEntry on creation but works on update - why?

Hi all

I’m improving the notifications for a workflow and I want to include the asset title in the notification. For that I want to create an entryTitle variable in freemarker, adding it to workflowContext. I’ve created a small groovy script attached to an action on the “created” state. It works, but only if the asset is being updated. It doesn’t work when the asset has just been created.

Here’s my code:

	// Get the class name and primary key from the workflow context
    def className = workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME);
    long classPK = Long.parseLong(workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
    println ts()+" wkFlowSetEntryTitle: " + entryClassName + " / " + classPK

	// Fetch the AssetEntry for the given classPK
	def assetEntry = AssetEntryLocalServiceUtil.fetchEntry(className, classPK);

	if (assetEntry != null) {
		// Put the title into the workflow context
		workflowContext.put("entryTitle", assetEntry.getTitle(Locale.forLanguageTag("pt-PT")));
	} else {
		workflowContext.put("entryTitle", "Untitled");
	}

Anybody has any idea why AssetEntryLocalServiceUtil.fetchEntry always fails when the content has just been created? Any idea on how to solve this?

TIA

Fernando

OK, this seems to be a timing problem. The asset is not yet created when the workflow starts, so I found a solution using AssetRenderer. Here is the code, in case it might help somebody:

import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.service.AssetEntryLocalServiceUtil;
import com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil;
import com.liferay.asset.kernel.model.AssetRendererFactory;
import com.liferay.asset.kernel.model.AssetRenderer;

def ts() {
    def now = new Date()
    return now.format("HH.mm.ss.SSS")
}

def locale = Locale.forLanguageTag("pt-PT");
// Get the class name and primary key from the workflow context
def className = workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME);
long classPK = Long.parseLong(workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
println ts()+" wkFlowSetEntryTitle: " + entryClassName + " / " + classPK

// Get title from an AssetRenderer, add to workflowContext
def assetRendererFactory = AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(className);
def assetRenderer = assetRendererFactory.getAssetRenderer(classPK);
def title = assetRenderer.getTitle(locale);
workflowContext.put("entryTitle", title ?: "No title");