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
MaxaMaxa 

Calling apex code from trigger

I have piece of code that I can call fine if I connect it to the button, however I would like to automate it with a trigger and I’m not sure how to call my Apex class from the trigger can anyone help?

Best Answer chosen by Admin (Salesforce Developers) 
trekbintrekbin

Hi Maxa,

 

You can create a new instance of your class in the Trigger and call your function from there.

For Example, if classTest were your class and functionTest is what you want to invoke, your Trigger code will look like:

 

classTest obj = new classTest();

obj.functionTest(<params>); 

 

-Santosh Prasad

 

Force.com and Rich Internet Applications!

 

 

 

+1 (646)  682-6536


All Answers

trekbintrekbin

Hi Maxa,

 

You can create a new instance of your class in the Trigger and call your function from there.

For Example, if classTest were your class and functionTest is what you want to invoke, your Trigger code will look like:

 

classTest obj = new classTest();

obj.functionTest(<params>); 

 

-Santosh Prasad

 

Force.com and Rich Internet Applications!

 

 

 

+1 (646)  682-6536


This was selected as the best answer
JimRaeJimRae

If you have a static method in your class, you can call it directly.

 

 

trigger mytrig on Account(before insert){ myclass.mymethod(parm1,parm2); }

 

 

 

MaxaMaxa

Hi Jim,

I'm having some problems when i'm trying to implement your suggestion, it keeps telling me that there is no such variable as Accountid

here is my class code

global class post2Odin{ WebService static string Call_Odin_Webservice(String Accountid)

 

and here is my trigger that doen not work when i try to call this calss

trigger submit on Account (After Update) { post2Odin.Call_Odin_Webservice(Accountid) }

i must be doign somethign wrong

JimRaeJimRae

If that is your whole trigger code, then I can see your problem.  AccountID is never defined.  Remember that you want to make your trigger bulksafe, since multiple records can be added together in batch.

 

You need something like this:

 

 

trigger submit on Account (After Update) { for(Account a:Trigger.New){ post2Odin.Call_Odin_Webservice(a.ID); } }

 

 

 

MaxaMaxa

this is great it worked trigger ran fine all code was executed, thanks Jim, but the problem i get now is different

Callout from triggers are currently not supported.

i get SF to check this out

JimRaeJimRae

You might need to have an intermediary class and method to get this to work.

Here is a great tutorial on how to construct a callout from a trigger

http://www.cheenath.com/?tutorial/sfdc/sample1/index.html

MaxaMaxa
but it can only be void and this methd cannot return results , i need to be able to send data to external ervice and receive acknowlegements from it
MaxaMaxa

Hi Jim,

 the moment i disabled responce result, it worked call out was made and our system accepted the changes and worked as it should have, shame i cannot get back desired result, but much better than nothign, thank you for all your help

Suresh RaghuramSuresh Raghuram

can you provide a detailed example .

 

suppose my method is list<opportunity> type and in some class and i am updating the values that are returned by the method in the trigger , can you provide a code sample .

 

Thanks 

Nani44Nani44

Hi All,

 

when a class update the reset password automatically  one trigger is running in background. Now I need to stop the trigger when reset password  is updated in the user object. Can we able to call the statice variable  form class to trigger. If so how to call the statice variable in the tirgger.