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

3

5

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.

Zabrine answered 13/2, 2013 at 8:11 Comment(1)
Which TCM version is this?Tamandua
A
5

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"
Accumulate answered 14/2, 2013 at 15:15 Comment(0)
T
3

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

Tamandua answered 13/2, 2013 at 8:46 Comment(1)
Thank you, Arjen. We can't find the workItems associated to the Activity. We have installed Tridion 2009 SP1. Can you pass an example?Zabrine
G
2

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);
Goodwin answered 13/2, 2013 at 14:58 Comment(3)
hello, I reviewed my code with your change but it didn't work.Zabrine
Have you looked into the history via the CM GUI, is the history item there, and are you on your last activity in the workflow?Goodwin
I reviewed my "history list" in CM GUI and all the tasks have a tcm finished with 131076. I can't see the activities with tcm finished in 131080. The Activity that I process at OnActivityInstanceFinishPost is the first Activity that I finish. Thank you.Zabrine

© 2022 - 2024 — McMap. All rights reserved.