function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VFVF 

Triggers

Here i am using an Apex Trigger and would like to create an apex method from an apex class in the trigger is that possible to use an apex custom class in an apex trigger.

for example:

 

public class myclass

{

 public String mymethod()

{

.............. 

 

Apex Trigger:

 

trigger Testobj on myObject__c (after insert) { 

 myObject__c  myobj = Trigger.new[0];

 

 myclass clas = new myclass();

class. mymethod(.......);

 

 

 

Here in this trigger the afterInsert action is taken such that after the values are inserted the action from the apex method takes place.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jeffdonthemicjeffdonthemic

It is possible to call custom classes from your trigger. I'm not sure what you are trying to accomplish but an example would be:

 

 

trigger Testobj on myObject__c (after insert) { MyClass myClass = new MyClass(); myClass.myMethod(Trigger.new); }

 

Jeff Douglas

Informa Plc

http://blog.jeffdouglas.com

 

 

All Answers

jeffdonthemicjeffdonthemic

It is possible to call custom classes from your trigger. I'm not sure what you are trying to accomplish but an example would be:

 

 

trigger Testobj on myObject__c (after insert) { MyClass myClass = new MyClass(); myClass.myMethod(Trigger.new); }

 

Jeff Douglas

Informa Plc

http://blog.jeffdouglas.com

 

 

This was selected as the best answer
VFVF

when we are using classes in a trigger an exception is being raised 

System.CalloutException: Callout from triggers are currently not supported"

could you please help me out on this..

when used a method in a class with @future anntation infront of a ststic method. for a globalclass with static methods i am able to access only a single method when i am attempting to use more than one method in a trigger   an exception:

Too many future calls is being displayed .

 

please help me out on this issue. 

jeffdonthemicjeffdonthemic

The first error is correct... you must implement a class that contains a future method and then have your trigger call that method.

 

There is a limit of 10 future method calls per Apex invocation. See the docs for more info and sample code.


Jeff Douglas
Informa Plc
http://blog.jeffdouglas.com

VFVF

Hi,

Thanks for ur reply, i went through the docs  according to the docs...

"There is a limit of 10 future method calls per Apex invocation"  but my question is that can i make more than one  external webservice callout in a single future method.When i am trying to so an error is being raised as :

"Too many future calls:".

 

Here, i am sending an email using an external webservice API methods.

 

Please let me know if it is possible to make no: of webservice callouts in a single future method:

 

 @future public static void PostCampaign()

{

Integer segmentId=null;

Integer mailingListId = null;Integer campaignId =

null;

CCMDcommon common = new CCMDcommon();List<String> emails=

new List<String>{abc@gmal.com'};

segmentId = common.createImmediateSegment('UserName','PassWord','Version',emails);Integer[] segmentList =

new Integer[1];

segmentList[0] = segmentId;

mailingListId = common.createMailingList('UserName','PassWord','Version',, segmentList, 'dummyMailingList');

campaignId = common.createCampaign('UserName','PassWord','Version','dummyCampaign',793,mailingListId,'2009-04-10 03:30:00');common.postCampaign(

'UserName','PassWord','Version',campaignId);

 

 

}

 

This is a @future method inside a class here i am making a number of external webservice call out to post a campaign or to send an email when doing so i am facing an error as:"Too many future calls:".

 

 

Please help me out as i would like to know whether i can do so or not using triggers and @future methods.

 

 

 

 

jeffdonthemicjeffdonthemic

One good way to figure out you limits for a particular call is to write to the debug log some these values (eg getLimitFutureCalls) It should give you a little more insight.

 

You might want to look at structuring your web service so that is accepts an array of objects instead of calling the web service one at a time. This way you could batch up your web service calls with your @future method. You can create a static member that maintains all of the objecs that need to be sent to the web service during your trigger invocations and then finally send these all out with a single @future call. Here is some more info on how to share static members within your trigger invocation.

 

Hope this helps.

 

Jeff Douglas
Informa Plc
http://blog.jeffdouglas.com