Sarit Rotshild
Sarit Rotshild

Reputation: 391

Grails invokeMethod doesn't work

I have 2 method which get the same params:

 void removeJob(def user,def company,String job){}
 void addJob(def user,def company,String job){}

I want to use the invokeMethod() but I'm not sure how. I try:

def methodName = selectedChange[2]=='true'?'add':'remove'
roleService.invokeMethod(methodName+'Job',[userSelected,      selectedCompany,selectedChange[1]])

But I got errors

Upvotes: 0

Views: 41

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75681

You can call the method dynamically using a GString for the method name:

String methodName = selectedChange[2] == 'true' ? 'add' : 'remove'
roleService."${methodName + 'Job'}"(userSelected, selectedCompany, selectedChange[1])

This also works with properties, e.g.

String propertyName = 'fooCount'
int count = person."$propertyName"

Upvotes: 1

Related Questions