How to start a sub process from an AgilePart

Lately I was having the problem I had a list of email messages to be send but I could not make a foreach loop in the workflow builder itself. So I figured out that maybe there was a solution by creating an AgilePart that would retrieve a list of messages from the database and then for every message start a new process.
And actually there was a way of doing it like this.

First of all I made 2 workflows. One with the AgilePart that retrieves the messages from the database (in my case it was a connection to a webservice).
Second I created a workflow for sending the email itself. The mailTemplate is filled with some parameters (from, to, subject, body).

Here are some pictures of my workflows.

Now the part of the source:

First I retrieve a list of messages from the WebService.
Then with a foreach loop I go through all the messages and for each message I create a new SubProcess.

What code is needed to start a SubProcess:

XmlElement element = notificationService.GetNotificationMessages();
List<EmailMessage> messages = DeserializeObject(element.OuterXml);

foreach (EmailMessage mes in messages)
    string processTemplateName = "NotificationMail";

    //Get UUID fot he released process definition
    string pid = api.GetReleasedPID(processTemplateName);

    //Generate a unique process instance ID
    string piid = Ascentn.Workflow.Base.UUID.GetID();

    //Generate a unique name for the process instance
    string piname = "MyProcess_" + DateTime.Now.ToString("MMdd-HHmmss");

    //Generate a unique work object ID
    string workObjectId = string.Format("MyProcess-{0}", UUID.GetID());

    //Process ID of the parent
    string superPIID = w.ProcInstID;

    //Create a list with the attributes that are in the mailtemplate
    string mailFrom = mes.From;
    string mailTo = mes.To;
    string mailSubject = mes.Subject;
    string mailBody = mes.HTMLBody;
    NameValue[] attrs = NameValue.Array(
        "MailFrom", mailFrom,
        "MailTo", mailTo,
        "MailSubject", mailSubject,
        "MailBody", mailBody

    // Start the process
    WFEvent evt = api.CreateSubProcInst(api.GetProcInst(w.ProcInstID), api.GetActivityInst(w.ActivityInstID), false, pid, piid, piname, workObjectId, "", superPIID, "", "", attrs, true);

Leave a Reply

Your email address will not be published. Required fields are marked *