Question
SkillStorm
US
Last activity: 20 Apr 2018 13:59 EDT
How to get name of calling activity
If ActivityA calls ActivityB then in ActivityB how do you get the name of the activity that called it?
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
SkillStorm
US
Oh I see what you're saying. That won't fit our needs though as it would require updating every activity that calls ActivityB.
My requrement is that every log message includes the activity name and other elements before the message. To ensure this I have created a standardized activity for logging that takes in the elements including the activity name and then logs the message. Taking the activity name in as a parameter is more work and error prone than setting it dynamically if that is possible.
I looked at the linked discussion but that uses a stack trace so it would be hard to implement and would negatively impact performance getting the stack and building a complex enough regex to pull out the right activity name for every log message.
So it looks like just using a param that ActivityA will have to pass is the easiest and most maintainable option.
Updated: 12 Mar 2018 12:36 EDT
Pegasystems Inc.
US
Hi Hermy,
I have a simple approach for you regarding this requirement, you can add a Parameter lets say ActivityName with default value "ActivityA" in ActivityA activity. Now in your ActivityB you can reference ActivityName parameter to get the parent activity name.
Hope this answers your query.
Regards
Mahesh
SkillStorm
US
The field for defining a default for the parameter seems to be explicitely a string literal. I tried putting property references there and when I go to call ActivityB the default shows as the literal string, not the property value. So as far as I can tell this approach won't work. Thank you for the suggestion though.
Pegasystems Inc.
US
If your intention is to get the name of the called activity(ActivityA) in the calling activity then below approach should work:
- Declare ActivityName parameter of type String in ActivityA.
- You can either set the value of ActivityName="ActivityA" in default value field or set in the activity step.
- Now in your ActivityB you can get the name of called activity i.e. "ActivityA" using ActivityName parameter.
If this is not your requirement, kindly let me know your exact requirement.
Alternatively, if your intention is just to see the name of called activity then you can use the code which John has specified in this discussion.
Accepted Solution
SkillStorm
US
Oh I see what you're saying. That won't fit our needs though as it would require updating every activity that calls ActivityB.
My requrement is that every log message includes the activity name and other elements before the message. To ensure this I have created a standardized activity for logging that takes in the elements including the activity name and then logs the message. Taking the activity name in as a parameter is more work and error prone than setting it dynamically if that is possible.
I looked at the linked discussion but that uses a stack trace so it would be hard to implement and would negatively impact performance getting the stack and building a complex enough regex to pull out the right activity name for every log message.
So it looks like just using a param that ActivityA will have to pass is the easiest and most maintainable option.
Pegasystems Inc.
US
As i don't see any OOTB rule to get the name of the activity using the param in ActivityA and then passing it would be the easiest approach Hermy.
SkillStorm
US
No problem. Thanks for your help.
Pegasystems Inc.
US
If we have a activity that is getting called from multiple places in the application and we do not want to pass a parameter from each of the activities that we call this activity from, we can write a small java code to get the name of the calling activity.
String javaClassname = "";
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
int j=0;
for(int i=0; i< stacktrace.length ; i++){
if(stacktrace[i].getMethodName().equals("perform")){
j++;
}
if(j==2){
javaClassname = stacktrace[i].getClassName();
break;
}
}
int startIndex = javaClassname.indexOf("_action_")+8;
int endIndex = javaClassname.lastIndexOf("_");
String actName = javaClassname.substring(startIndex, endIndex );
Reference: https://community.pega.com/support/support-articles/how-capture-name-calling-activity
SkillStorm
US
Oh nice! Unfortunately, because of how frequently I'm expecting my activity to be called this would become an expensive operation on performance. Thanks for sharing this solution though.