You need to sign in to do that
Don't have an account?
jagan
Callout from triggers are currently not supported.
Hi
All,
We are currently facing an issue regarding SMS
integration.
We
need to send the SMS automatically when a case is saved/edited. For
this we have
an apex class
which consists of HTTPS callouts to
access the SMS gateway of Value first
and a trigger which
invokes this apex class when ever a case is saved/edited.
Now, when ever the trigger is fired i'm getting this
exception "
I think it can be possible by S-Control but
the process should automatic.
And this is very urgent for our project Pls suggest any solution /workaround for the same. Any help is appreciable.
Thanks
Jagan. |
You do need to use the @future annotation in an apex method that is stored in a separate class. Then call that class and method from within your trigger.
For example:
trigger justAnExample on Account (after update){ testCls.myMethod(); //call the static method, with the @future annotation }
global testCls { @future Public static void myMethod(callout=true){ System.debug('inside myMethod...'); //do your callout logic here... System.debug('exiting myMethod...'); } }
Documentation on @future here
All Answers
Jagan, I'm not 100% sure, but I believe any call outs in triggers must be done asynchronously.
I have a call out - but it's being done in an Apex class which is called by my asynchronous trigger- with no issues. I believe to make it asynchronous you need to add "@future" to your trigger.
You do need to use the @future annotation in an apex method that is stored in a separate class. Then call that class and method from within your trigger.
For example:
trigger justAnExample on Account (after update){ testCls.myMethod(); //call the static method, with the @future annotation }
global testCls { @future Public static void myMethod(callout=true){ System.debug('inside myMethod...'); //do your callout logic here... System.debug('exiting myMethod...'); } }
Documentation on @future here
Here is a detailed example:
http://cheenath.com/?tutorial/sfdc/sample1/index.html
Hi all,
Thanks a lot for your prompt response..
This is really helpful to me and my problem is solved.
Now i'm able to achieve the call out from trigger.
As @Future is only for static void methods, we are not able to return a value like
whether the message is sent successfully or any exception raised.
Is there is any other way to achieve this?
Thanks & Regards,
Jagan
Cheenath,
Thank you for posting your example of using a future callout from a trigger. Your example is almost identical to some code I have written - mine fails continually telling me I have outstanding changes and I need to commit or do a rollback. Have you tried this code in Summer '09?
Thanks,
Diane
Cheenath,
thank you for your example using @Future... my callout now works fine (having removed the redirect I was doing on the Grails side). Greatly appreciated.
David
Hi Jagan,
I am doing the integration with Value First. I would really appreciate if you please help me with the direction or sample code.
Thanks,
Sandeep
Since callouts happen asynchronously, you can not return a value to the user advising them of a failure to send as a normal error message. You'll need to find an asynchronous way of notifying the user that a SMS failed to send.
Here are some suggestions:
A custom object that tracks SMS messages sent out could be updated with status changes. A custom field could identify the status of those messages that they could retrieve at any time, maybe through a report or directly looking at a contact.
The user could be notified via a sidebar component that queries the database periodically for messages sent (maybe stored as a task or custom object). This could be in Visualforce, and displayed on all pages using the User Interface option to show custom sidebar components on all pages.
The user could be notified through a workflow rule to assign them a task or send them an email in the event of a failure. Depending on how this is set up, they would be notified the moment an SMS fails to send.
Create a special Visualforce page for sending SMS messages. Since this is not a trigger, you can perform the callout while the user waits and provide immediate feedback.
Cheenath's trigger callout tutorial http://cheenath.com/?tutorial/sfdc/sample1/index.html will fail in bulk triggers as only 10 future calls allowed in trigger context, so be careful.
I was also thinking about the limits on @future and bulk updates. But the limits have changed in the summer 11 release.
https://login.salesforce.com/help/doc/en/salesforce_app_limits_cheatsheet.pdf
Still there are limits and this could be a problem for the proposed solution.
One way to improve the sample is to collect all the Account ids, in a list, in the trigger. Pass the list to the @Future method that updates Accounts. This way a batch of updates will result in one call to a future method.
Also, in that update method you'd need to update a list of Accounts rather than do them one at a time. This is normal.
Amazing, simple and to the point set of instructions. You saved my day !
Your syntax seems incorrect. It should be:
@future (callout=true)
public static void myMethod() {
//...
}