Tridion Workflows - How to get the Component at the Activity in Event Handler

StackOverflow https://stackoverflow.com/questions/14849053

  •  09-03-2022
  •  | 
  •  

Pregunta

I need to get the component associated to a Activity at the event system.

I try to get the component ID using:

    public void OnActivityInstanceFinishPost(ActivityInstance activityInstance, string finishMessage, string nextActivity, string dynamicAssignee)
    {

        if (activityInstance.ProcessInstance.ProcessDefinition.Title.Equals("Component Process IESE"))
        {
            if (activityInstance.ActivityDefinition.Title.Equals("Create or Edit Component"))
            {
                WFE workflow = tdse.GetWFE();
                try
                {
                    Component comp = (Component)activityInstance.ProcessInstance.Item;

                    XMLReadFilter filter = new XMLReadFilter();
                    String processHistoryId = activityInstance.ProcessInstance.ID.Replace("131076", "131080");

                   ProcessHistory hist = (ProcessHistory)tdse.GetObject(activityInstance.ProcessInstance.ID, EnumOpenMode.OpenModeView, Constants.URINULL, filter);

                }
                catch (Exception e)
                { }

            }
        }
    }

we try different options:

    Component comp = (Component)activityInstance.ProcessInstance.Item;

But this solution returns a null.

Then I found in internet the next solution:

 XMLReadFilter filter = new XMLReadFilter();
 String processHistoryId = activityInstance.ProcessInstance.ID.Replace("131076", "131080");

 ProcessHistory hist = (ProcessHistory)tdse.GetObject(activityInstance.ProcessInstance.ID, EnumOpenMode.OpenModeView, Constants.URINULL, filter);
 Component comp = hist.Item as Component;

But the ProcessHistory object is null.

How can I determine the component associated to the activityInstance?

Thank you.

¿Fue útil?

Solución

After reviewing the functionality needed by Guskermitt, I've shown him a neater way to do what he needs to do. In short, EventSystem is not needed in this case.

His goal is to send an email after a component has been approved, the approach will be the following:

  1. Add to workflow a new automatic activity.
  2. Create a new .NET assembly, in this case a C# class to do what he needs to do.
  3. Register the assembly in the GAC.
  4. Add logic in the new automatic activity in workflow to use the .NET assembly.

2#

[ProgId("WfHelper")]
[ComVisible(true)]
public class Helper
{    
public void SendMail(string workItemId)
{
var session = new Session();
.
.
.

4#

dim helper 
set helper = CreateObject("WfHelper")
call helper.SendMail(CurrentWorkItem.ID)
set helper = nothing 
FinishActivity “Email has been sent"

Otros consejos

ActivityInstance has a WorkItems property (inherited from Activity) that contains a reference to your Component.

OnActivityInstanceFinishPost means that your activity is finished. Therefore there is no more work item associated with it. However, you are getting the process instance and the work item associated with that. If you get null there, then it suggests your workflow process is done and the component has moved out of workflow. From looking at your code, it is quite likely that your ProcessInstance is completed (it won't be null, but it won't have any item associated with it).

I suspect that you've read this post http://www.tridiondeveloper.com/autopublishing-on-workflow-finish suggesting to look in the history. Have you looked into the history via the CM GUI, is the history item there? If it isn't, that's why you get null. A workflow process gets moved to history when it is completed. So double check that you are indeed on the last workflow activity before looking at the history.

By looking at your code, the error seems to be that you are trying to get a history object using activityInstance.ProcessInstance.ID. GetObject() should return an item, but your cast to a ProcessHistory should break and then you quietly eat the exception. You need to pass in the History ID, not the ProcessInstance ID as follows:

ProcessHistory hist = (ProcessHistory)tdse.GetObject(processHistoryId, EnumOpenMode.OpenModeView, Constants.URINULL, filter);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top