Assign Task to Group and send E-Mail

1
0
-1

Hi all,

I'm stuck in my project and hope you can help me.

In the first Task of the project I select a Group and get the GroupId as String back.
I need to assign the second Task to members of this Group, I think via Actor-Filter.
I have tried this:
List users = apiAccessor.getIdentityAPI().getUsersInGroup(accessAuthorizationInput.groupId.toLong(), 0, 100, UserCriterion.FIRST_NAME_ASC);
for (User user : users) {
def departement = new DepManager();
departement.firstname = user.firstName;
departement.lastname = user.lastName;
departement.title = user.title;
accessAuthorizationVar.addToDepartement(departement);
}
but it doesn't work.

The next thing where I'm stuck is sending an Email to this Users in the Task.
I have tried this toScript with the Email-Connector:
import org.bonitasoft.engine.identity.ContactData;
import org.bonitasoft.engine.identity.Group;
import org.bonitasoft.engine.identity.User;
import org.bonitasoft.engine.identity.UserCriterion;

Group groupSelected=authorization.getGroupId();
List usersInGroup=apiAccessor.getIdentityAPI().getUsersInGroup(groupSelected.getId(), 0, 100, UserCriterion.FIRST_NAME_ASC);
StringBuilder sb=new StringBuilder();
for (User user:usersInGroup){
//get professional contact data
ContactData contactData = apiAccessor.getIdentityAPI().getUserContactData(user.getId(), false);
if (contactData!=null && contactData.getEmail()!=null && !contactData.getEmail().isEmpty()){
sb.append(contactData.getEmail()).append(",");
}
}
return sb;
but also get errors.

Thanks for your time.

Comments

Submitted by Sean McP on Wed, 05/10/2017 - 13:01

OK - so what are the errors? We are not mind readers...

regards
Seán

Submitted by max.schmidt on Wed, 05/10/2017 - 13:34

Hi, sry of course.
But I also see this is what I use for Actor:
import org.bonitasoft.engine.identity.User;
import org.bonitasoft.engine.identity.UserCriterion;

List result = new ArrayList();
List users = apiAccessor.getIdentityAPI().getUsersInGroup(authorization.placeholder.toLong(), 0, 100, UserCriterion.FIRST_NAME_ASC);
for (User user : users) {
result.add(user.id)
}
return result

placeholder is the String with the GroupId

This is the Error:
2017-05-10 13:25:04.756 +0200 INFO: org.bonitasoft.engine.api.impl.ProcessAPIImpl THREAD_ID=459 | HOSTNAME=Bon01 | TENANT_ID=1 | The user <stephane.walrave> has executed the task [name = <Vérification   des données>, display name = <Vérification   des données>, id = <200008>, parent process instance = <10003>, root process instance = <10003>, process definition = <8404392087493323471>] with task inputs: {isApproved=oui}
2017-05-10 13:25:05.056 +0200 SEVERE: org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork THREAD_ID=468 | HOSTNAME=Bon01 | TENANT_ID=1 | The work [ExecuteFlowNodeWork: processInstanceId:10003, flowNodeInstanceId: 200010] failed. The failure will be handled.
2017-05-10 13:25:05.063 +0200 SEVERE: org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork THREAD_ID=468 | HOSTNAME=Bon01 | TENANT_ID=1 | org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException : "PROCESS_DEFINITION_ID=8404392087493323471 | PROCESS_NAME=Access Authorization - Arrival | PROCESS_VERSION=1.1 | PROCESS_INSTANCE_ID=10003 | ROOT_PROCESS_INSTANCE_ID=10003 | FLOW_NODE_DEFINITION_ID=6185471763732643814 | FLOW_NODE_INSTANCE_ID=200010 | FLOW_NODE_NAME=Dep. Manager confirmation | org.bonitasoft.engine.core.filter.exception.SUserFilterExecutionException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: FLOW_NODE_INSTANCE_ID=200010 | Declared return type class java.lang.Long is not compatible with evaluated type class java.util.ArrayList for expression userId()"
org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException: PROCESS_DEFINITION_ID=8404392087493323471 | PROCESS_NAME=Access Authorization - Arrival | PROCESS_VERSION=1.1 | PROCESS_INSTANCE_ID=10003 | ROOT_PROCESS_INSTANCE_ID=10003 | FLOW_NODE_DEFINITION_ID=6185471763732643814 | FLOW_NODE_INSTANCE_ID=200010 | FLOW_NODE_NAME=Dep. Manager confirmation | org.bonitasoft.engine.core.filter.exception.SUserFilterExecutionException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: FLOW_NODE_INSTANCE_ID=200010 | Declared return type class java.lang.Long is not compatible with evaluated type class java.util.ArrayList for expression userId()

And E-Mail Error:
2017-05-10 13:33:42.056 +0200 INFO: org.bonitasoft.engine.api.impl.ProcessAPIImpl THREAD_ID=65 | HOSTNAME=Bon01 | TENANT_ID=1 | The user <stephane.walrave> has executed the task [name = <Vérification   des données>, display name = <Vérification   des données>, id = <200016>, parent process instance = <10005>, root process instance = <10005>, process definition = <4985366349716173731>] with task inputs: {isApproved=oui}
2017-05-10 13:33:46.376 +0200 SEVERE: org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork THREAD_ID=476 | HOSTNAME=Bon01 | TENANT_ID=1 | org.bonitasoft.engine.expression.exception.SExpressionEvaluationException : "PROCESS_DEFINITION_ID=4985366349716173731 | PROCESS_NAME=Access Authorization - Arrival | PROCESS_VERSION=1.1 | PROCESS_INSTANCE_ID=10005 | ROOT_PROCESS_INSTANCE_ID=10005 | FLOW_NODE_DEFINITION_ID=5942250958390834156 | FLOW_NODE_INSTANCE_ID=200018 | FLOW_NODE_NAME=Dep. Manager confirmation | CONNECTOR_IMPLEMENTATION_CLASS_NAME=testMail | CONNECTOR_INSTANCE_ID=200004 | Groovy script throws an exception of type class org.codehaus.groovy.runtime.typehandling.GroovyCastException with message = Cannot cast object '141' with class 'java.lang.Long' to class 'java.util.List'
Expression : SExpressionImpl [name=totest(), content=import org.bonitasoft.engine.identity.ContactData;

import org.bonitasoft.engine.identity.Group;

import org.bonitasoft.engine.identity.User;

import org.bonitasoft.engine.identity.UserCriterion;

List test= authorization.placeholder.toLong()

List usersInGroup=apiAccessor.getIdentityAPI().getUsersInGroup(test(), 0, 100, UserCriterion.FIRST_NAME_ASC);

StringBuilder sb=new StringBuilder();

for (User user:usersInGroup){

   //get professional contact data<br />

   ContactData contactData = apiAccessor.getIdentityAPI().getUserContactData(user.getId(), false);

    if (contactData!=null && contactData.getEmail()!=null && !contactData.getEmail().isEmpty()){

       sb.append(contactData.getEmail()).append(",");

   }

}

return sb;, returnType=java.lang.String, dependencies=[SExpressionImpl [name=apiAccessor, content=apiAccessor, returnType=org.bonitasoft.engine.api.APIAccessor, dependencies=[], expressionKind=ExpressionKind [interpreter=NONE, type=TYPE_ENGINE_CONSTANT]], SExpressionImpl [name=authorization, content=authorization, returnType=com.company.model.AccessAuthorization, dependencies=[], expressionKind=ExpressionKind [interpreter=NONE, type=TYPE_BUSINESS_DATA]]], expressionKind=ExpressionKind [interpreter=GROOVY, type=TYPE_READ_ONLY_SCRIPT]]"
org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: PROCESS_DEFINITION_ID=4985366349716173731 | PROCESS_NAME=Access Authorization - Arrival | PROCESS_VERSION=1.1 | PROCESS_INSTANCE_ID=10005 | ROOT_PROCESS_INSTANCE_ID=10005 | FLOW_NODE_DEFINITION_ID=5942250958390834156 | FLOW_NODE_INSTANCE_ID=200018 | FLOW_NODE_NAME=Dep. Manager confirmation | CONNECTOR_IMPLEMENTATION_CLASS_NAME=testMail | CONNECTOR_INSTANCE_ID=200004 | Groovy script throws an exception of type class org.codehaus.groovy.runtime.typehandling.GroovyCastException with message = Cannot cast object '141' with class 'java.lang.Long' to class 'java.util.List'
Expression : SExpressionImpl [name=totest(), content=import org.bonitasoft.engine.identity.ContactData;

import org.bonitasoft.engine.identity.Group;

import org.bonitasoft.engine.identity.User;

import org.bonitasoft.engine.identity.UserCriterion;

List test= authorization.placeholder.toLong()

List usersInGroup=apiAccessor.getIdentityAPI().getUsersInGroup(test(), 0, 100, UserCriterion.FIRST_NAME_ASC);

StringBuilder sb=new StringBuilder();

for (User user:usersInGroup){

   //get professional contact data<br />

   ContactData contactData = apiAccessor.getIdentityAPI().getUserContactData(user.getId(), false);

    if (contactData!=null && contactData.getEmail()!=null && !contactData.getEmail().isEmpty()){

       sb.append(contactData.getEmail()).append(",");

   }

}

return sb;, returnType=java.lang.String, dependencies=[SExpressionImpl [name=apiAccessor, content=apiAccessor, returnType=org.bonitasoft.engine.api.APIAccessor, dependencies=[], expressionKind=ExpressionKind [interpreter=NONE, type=TYPE_ENGINE_CONSTANT]], SExpressionImpl [name=authorization, content=authorization, returnType=com.company.model.AccessAuthorization, dependencies=[], expressionKind=ExpressionKind [interpreter=NONE, type=TYPE_BUSINESS_DATA]]], expressionKind=ExpressionKind [interpreter=GROOVY, type=TYPE_READ_ONLY_SCRIPT]]

Submitted by Sean McP on Wed, 05/10/2017 - 22:33

A Tip on displaying CODE/LOGS correctly in Posts:

Do not use the Supplied Buttons above, for some reason they refuse to work correctly, and despite bringing it to Bonitasofts attention, it's low priority.

To Show Code/Logs correctly use

< code >
your code/log
< /code >

removing the spaces to allow the showing of code/logs correctly as here:

your code/log

Thanks and regards

Submitted by Sean McP on Wed, 05/10/2017 - 22:51

You say I need to assign the second Task to members of this Group, I think via Actor-Filter.

It's not clear - do you want to assign the second task to the group so only ONE user does it, or do you want to assign the second task to ALL members of the group so that each does it?

If the former just assign to the group, if the latter then you cannot do this via Actor-Filter, you will need to create X task and assign them individually.

Regarding the Actor errors:

org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: FLOW_NODE_INSTANCE_ID=200010 | Declared return type class java.lang.Long is not compatible with evaluated type class java.util.ArrayList for expression userId()"

you are trying to return ArrayList when you should be returning Long...you have to return the right type.

and for the email error it is a similar issue:

Groovy script throws an exception of type class org.codehaus.groovy.runtime.typehandling.GroovyCastException with message = Cannot cast object '141' with class 'java.lang.Long' to class 'java.util.List'

You cannot cast Long to List

regards

Submitted by max.schmidt on Thu, 05/11/2017 - 08:28

Hi again,
I need to assign the task to a group, so that on Member of this Group can take it and do it.
The Group is like I said, selected in the Form, but I do not get how it can be assigned to it. So I tried to get the GroupId back and make an Actor-Filter.
Is there a better way?
I posted my script in the beginning, can you tell me what needs to be changed to get it working?
Thanks and regards

1 answer

1
0
-1

There's near no need for any code for this one, here is what I would do:

You already have the groupid (as a string) that's JSON for you...
In your output operation/task change this to an int using

int result = Integer.parseInt(groupid);

then

Ignore everything I've just said :) ... took me a while but here is the correct way to do it...I think ;)

Define an Empty Group in the Organization (dynamicGroup)
Define a new Actor (dynamicActor) and assign the Empty Group (dynamicGroup)
//* the above makes sure the process has a defined Actor/member pairing for deployment

Define your process and make sure the Pool and Every human task have the appropriate Actor defined. For the task(s) you want to be assigned based on Selection assign the new Actor (dynamicActor)

In your code for assigning the process to the group

Get the groupId

then use one of the following to add the group to the new Actor (dynamicActor)

addGroupToActor(long, long) - Method in interface org.bonitasoft.engine.api.ProcessManagementAPI
Maps the group to the actor.
addGroupToActor(String, long, ProcessDefinition) - Method in interface org.bonitasoft.engine.api.ProcessManagementAPI
Maps the group to the actor of the process definition.

See: http://documentation.bonitasoft.com/javadoc/api/7.4/index.html (find addGroupToActor)

You will have to see exactly how it works, but I think you will have to add the group to the Actor before starting the process...

The process then should be visible to all actors of the selected group.

I can see one possible issue and that is starting multiple versions of the process and changing the involved groups on the actor while other processes are executing, it may cause some hick ups...as I say You will have to see exactly how it works.

Otherwise I would fix the issue of having to select a group to execute, why would you want to do this?

Sorry but not tried it...

regards
Seán

PS: As this reply offers an answer your question, and if you like it, please Mark UP and/or as Resolved.

Comments

Submitted by max.schmidt on Mon, 05/15/2017 - 09:03

Hi,
ok i have tried to use addGroupToActor(long,long) by using addGroupToActor("201".toLong(), authorization.placeholder.toLong()).
201 is the ID of swapGroup, the actor of the task.
But I get

2017-05-15 08:46:40.176 +0200 SEVERE: org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork THREAD_ID=124 | HOSTNAME=Bon01 | TENANT_ID=1 | The work [ExecuteFlowNodeWork: processInstanceId:14005, flowNodeInstanceId: 240008] failed. The failure will be handled.
2017-05-15 08:46:40.182 +0200 SEVERE: org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork THREAD_ID=124 | HOSTNAME=Bon01 | TENANT_ID=1 | org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException : "PROCESS_DEFINITION_ID=8513330874536809795 | PROCESS_NAME=Access Authorization - Arrival | PROCESS_VERSION=1.1 | PROCESS_INSTANCE_ID=14005 | ROOT_PROCESS_INSTANCE_ID=14005 | FLOW_NODE_DEFINITION_ID=6722984142159500886 | FLOW_NODE_INSTANCE_ID=240008 | FLOW_NODE_NAME=Vérification   des données | org.bonitasoft.engine.core.operation.exception.SOperationExecutionException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: groovy.lang.MissingMethodException: No signature of method: BScript8.addGroupToActor() is applicable for argument types: (java.lang.Long, java.lang.Long) values: [201, 141]"
org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException: PROCESS_DEFINITION_ID=8513330874536809795 | PROCESS_NAME=Access Authorization - Arrival | PROCESS_VERSION=1.1 | PROCESS_INSTANCE_ID=14005 | ROOT_PROCESS_INSTANCE_ID=14005 | FLOW_NODE_DEFINITION_ID=6722984142159500886 | FLOW_NODE_INSTANCE_ID=240008 | FLOW_NODE_NAME=Vérification   des données | org.bonitasoft.engine.core.operation.exception.SOperationExecutionException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: groovy.lang.MissingMethodException: No signature of method: BScript8.addGroupToActor() is applicable for argument types: (java.lang.Long, java.lang.Long) values: [201, 141]
Submitted by Sean McP on Mon, 05/15/2017 - 11:47

what's your actual code?

Submitted by max.schmidt on Mon, 05/15/2017 - 11:53

I dont know, if it is right.
I just made a new Bussinesobject, test1.
so I just mad an operation of a Java-Method setTest1 then a script addGroupToActor("201".toLong(), authorization.placeholder.toLong())

Submitted by Sean McP on Mon, 05/15/2017 - 12:07

http://documentation.bonitasoft.com/javadoc/api/7.4/org/bonitasoft/engin...

But shouldn't you actually use the full method of the API

processAPI.addGroupToActor("201".toLong(), authorization.placeholder.toLong())

regards

Submitted by max.schmidt on Mon, 05/15/2017 - 12:16

tried ProcessDefinitionAPI.addGroup.....

2017-05-15 12:10:59.222 +0200 INFO: org.bonitasoft.engine.api.impl.ProcessAPIImpl THREAD_ID=228 | HOSTNAME=Bon01 | TENANT_ID=1 | The user <stephane.walrave> has executed the task [name = <Vérification   des données>, display name = <Vérification   des données>, id = <240024>, parent process instance = <14013>, root process instance = <14013>, process definition = <5859263508127651109>] with task inputs: {isApproved=oui}
2017-05-15 12:10:59.298 +0200 SEVERE: org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork THREAD_ID=134 | HOSTNAME=Bon01 | TENANT_ID=1 | The work [ExecuteFlowNodeWork: processInstanceId:14013, flowNodeInstanceId: 240024] failed. The failure will be handled.
2017-05-15 12:10:59.304 +0200 SEVERE: org.bonitasoft.engine.execution.work.FailureHandlingBonitaWork THREAD_ID=134 | HOSTNAME=Bon01 | TENANT_ID=1 | org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException : "PROCESS_DEFINITION_ID=5859263508127651109 | PROCESS_NAME=Access Authorization - Arrival | PROCESS_VERSION=1.1 | PROCESS_INSTANCE_ID=14013 | ROOT_PROCESS_INSTANCE_ID=14013 | FLOW_NODE_DEFINITION_ID=8408122990622997275 | FLOW_NODE_INSTANCE_ID=240024 | FLOW_NODE_NAME=Vérification   des données | org.bonitasoft.engine.core.operation.exception.SOperationExecutionException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: groovy.lang.MissingMethodException: No signature of method: static org.bonitasoft.engine.api.ProcessManagementAPI.addGroupToActor() is applicable for argument types: (java.lang.Long, java.lang.Long) values: [201, 149]"
org.bonitasoft.engine.core.process.instance.api.exceptions.SActivityStateExecutionException: PROCESS_DEFINITION_ID=5859263508127651109 | PROCESS_NAME=Access Authorization - Arrival | PROCESS_VERSION=1.1 | PROCESS_INSTANCE_ID=14013 | ROOT_PROCESS_INSTANCE_ID=14013 | FLOW_NODE_DEFINITION_ID=8408122990622997275 | FLOW_NODE_INSTANCE_ID=240024 | FLOW_NODE_NAME=Vérification   des données | org.bonitasoft.engine.core.operation.exception.SOperationExecutionException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: groovy.lang.MissingMethodException: No signature of method: static org.bonitasoft.engine.api.ProcessManagementAPI.addGroupToActor() is applicable for argument types: (java.lang.Long, java.lang.Long) values: [201, 149]

that is what I am getting back.

I also tried to understand what is done here: link

But I just cannot understand how he does it.

Submitted by Dibyajit.Roy on Tue, 09/26/2017 - 09:55

Hello
This is How I assign a Task to a Specific User.
I display all the members from a Group to the User.
He selects one Member.
I save the value of the Username of the Member in a Process Variable.
In the Next task, I use actor filter to Assign the Task to the Specific User.

Below is my Code

import org.bonitasoft.engine.api.IdentityAPI;
import org.bonitasoft.engine.identity.User;

IdentityAPI identity = apiAccessor.getIdentityAPI();
User user = identity.getUserByUserName(requestor);

return user.getId();

For Emails , I use the Following Code

import com.bonitasoft.engine.api.IdentityAPI;
import com.bonitasoft.engine.api.ProcessAPI;

Same as Before
return getUserContactData(userId,false).getEmail();

regards

Notifications