Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
I would like to automatically create a subscription to object events inside a workflow, using java code.
looking into the windchill class documentation, I found that I should probably be using the following code :
wt.notify.NotificationHelper.manager.createObjectSubscription (wt.notify.NotificationSubscription, wt.notify.NotifySubscriptionRecipient, wt.notify.Notifiable, java.util.Hashmap<String, wt.fc.WTStringMap>, boolean)
but I found no explanation on how to get or build the parameters for this function.
I made some tries to create and initialise a wt.notify.NotificationSubscription object, a wt.notify.NotifySubscriptionRecipient object and a java.util.Hashmap<String, wt.fc.WTStringMap> object.
I used a cast on my primaryBusinessObject to get a wt.notify.Notifiable object.
But all without a lot of success.
Does somebody have a small working code example that would show how to use this createObjectSubscription function ?
Thanks in advance.
Solved! Go to Solution.
Following code works for 9.1 M40.
void constructSubscription(Notifiable targetObject) throws WTException, WTPropertyVetoException{
//Declare and initialize NotificationSubscription variable
NotificationSubscription subscriptionAttributes = new NotificationSubscription();
//set attributes for subscriptionAttributes
subscriptionAttributes.setName("My Custom Subscription Name");
subscriptionAttributes.setSubject("My Custom Subscription Subject");
subscriptionAttributes.setMessage("My Custom Subscription Message");
subscriptionAttributes.setSendImmediate(true);
//create Map for events. Events are case sensitive.
//Capitalize all characters and add "_" (underscore) between words
//Here is list of a few events
//CHECK_IN_FROM_PROJECT, CHECK_OUT_IN, COPY, DELETE,
//EDIT_ACCESS_CONTROL, EDIT_ATTRIBUTES, EDIT_IDENTITY, MARKUP,
HashMap<String,WTStringMap> eventsMap = new HashMap<String,WTStringMap>();
eventsMap.put("CHECK_OUT_IN", (WTStringMap)null);
eventsMap.put("DELETE", (WTStringMap)null);
//get subscriber, currently setting to session user
NotifySubscriptionRecipient subscriber = NotifySubscriptionRecipient.newNotifySubscriptionRecipient(SessionHelper.getPrincipal(), NotifySubscriptionRecipient.TO_ADDRESS);
//create subscription
NotificationHelper.manager.createObjectSubscription(subscriptionAttributes,subscriber, targetObject, eventsMap, true);
}
Following code works for 9.1 M40.
void constructSubscription(Notifiable targetObject) throws WTException, WTPropertyVetoException{
//Declare and initialize NotificationSubscription variable
NotificationSubscription subscriptionAttributes = new NotificationSubscription();
//set attributes for subscriptionAttributes
subscriptionAttributes.setName("My Custom Subscription Name");
subscriptionAttributes.setSubject("My Custom Subscription Subject");
subscriptionAttributes.setMessage("My Custom Subscription Message");
subscriptionAttributes.setSendImmediate(true);
//create Map for events. Events are case sensitive.
//Capitalize all characters and add "_" (underscore) between words
//Here is list of a few events
//CHECK_IN_FROM_PROJECT, CHECK_OUT_IN, COPY, DELETE,
//EDIT_ACCESS_CONTROL, EDIT_ATTRIBUTES, EDIT_IDENTITY, MARKUP,
HashMap<String,WTStringMap> eventsMap = new HashMap<String,WTStringMap>();
eventsMap.put("CHECK_OUT_IN", (WTStringMap)null);
eventsMap.put("DELETE", (WTStringMap)null);
//get subscriber, currently setting to session user
NotifySubscriptionRecipient subscriber = NotifySubscriptionRecipient.newNotifySubscriptionRecipient(SessionHelper.getPrincipal(), NotifySubscriptionRecipient.TO_ADDRESS);
//create subscription
NotificationHelper.manager.createObjectSubscription(subscriptionAttributes,subscriber, targetObject, eventsMap, true);
}
Thanks for the code.
It is not so far from what I tried, but it brings some information I missed.
I saw from your code that I do not need to build a WTStringMap for the event map
eventsMap.put("CHECK_OUT_IN", (WTStringMap)null);
That's a good point because I did not know how to do it.
I saw also that I should use a factory function to build a NotifySubscriptionRecipient, instead of calling "new".
However, when trying this new code in a workflow robot, I got the same error as before.
The root error seems to be an SQLException saying that a "LONG value can only be bound to a column of type LONG" and causing ROLLBACK exceptions.
Not very helpfull and not easy to relate to my code...
Anyway, thanks for your help.