Error in script of Connector Mail

Hi all,

I would send an email to all user of my company using a system task with a mail connector where in the To field I add a script that select all user and create a string with all email.

But this is the exception that I obtain.

 

java.lang.reflect.InvocationTargetException
org.bonitasoft.engine.bpm.connector.ConnectorExecutionException:
org.bonitasoft.engine.core.connector.exception.SConnectorException:
org.bonitasoft.engine.expression.exception.SExpressionEvaluationException:
Script throws an exceptionSExpressionImpl [
    name=getUserMail,
    content=
    
    import org.bonitasoft.engine.search.*;
    import org.bonitasoft.engine.identity.*;
    
    SearchOptionsBuilder so = new SearchOptionsBuilder(0, 100);
    SearchResult<User> userResult = apiAccessor.getIdentityAPI().searchUsers(so.done());

    String mailto = new String();
    for (User u : userResult)
    {
        UserWithContactData proUser = apiAccessor.getIdentityAPI().getUserWithProfessionalDetails(u.getId());
        mailto += ", "+proUser.getContactDactivityInstanceIdata().getEmail();
    }
    
    return  mailto;
    ,
    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
                                                                    ]
                                ]
                ],
    expressionKind=ExpressionKind [interpreter=GROOVY,
                                    type=TYPE_READ_ONLY_SCRIPT
                                ]
]

Thanks for attention,

Francesco

hello Francy,

having a look in your code and I see a few mistakes:

in line num 5 in the second block of code:
it says:
mailto += ", "+proUser.getContactDactivityInstanceIdata().getEmail();

and should be:

mailto += ", "+proUser.getContactData().getEmail();

SearchResult userResult → you cannot iterate over the searchResult object directly. You need to get the result of the search first (with userResult.getResult() method)!

Try something like this:

SearchOptionsBuilder so = new SearchOptionsBuilder(0, 100);
SearchResult userResult = identityAPI.searchUsers(so.done());
List userList = userResult.getResult();
String mailto = new String();
for (User user : userList) {
UserWithContactData proUser = identityAPI.getUserWithProfessionalDetails(user.getId());
if (proUser != null && proUser.getContactData() != null) {
mailto += ", " + proUser.getContactData().getEmail();
}
}
System.out.println(mailto);

Let me know if this helps.
Cheers,
Jordi

HI…

Resolve this problem???

I need help…

Thanks…

Going one step further… why don’t you create a new email list like : “all_company@xxx.com” and set all your company members to that list?

then, you will only need to send a single email to that list. Then the email will be propagated by your email server to whole company. easier, no?

Bare in mind that in your code you are doing a request to the Execution Engine (Bonita DB) per each user in the system to retrieve the email information (not very performant, let’s say). If this is the way you want to go, I recommend you to implement your own command, that retrieves all user emails in your system in a single request. Check the CommandAPI for more info (http://documentation.bonitasoft.com/javadoc/api/6.2/org/bonitasoft/engine/api/CommandAPI.html)

Regards,
Jordi