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
sindhu@demo.comsindhu@demo.com 

How to execute the trigger on click of a custom button

Hi, I have written a trigger and is perfectly fine.I want this trigger to be executed when clicked on a custom button in Case object. Can anyone help me out with this??
Best Answer chosen by Admin (Salesforce Developers) 
Jerun JoseJerun Jose
What you can do is to have the trigger logic moved to a class method, and then call that class method called on a button click.

You can refer to this cookbook on how to call classes using javascript
http://developer.force.com/cookbook/recipe/creating-a-button-with-apex

Or you can call a vf page action on click of the button.

I would prefer the VF route, gives more control.

All Answers

Niket SFNiket SF

Hello Siddhu,

       you need to update any field on the button clink. Because trigger will fire only when any DML operation will happen. Please marked as solved if it hepls you :)

kerwintangkerwintang

Trigger is executed only when the Object is updated/deleted. If there is no change in the Object then i think the trigger won't be called.

 

What i would do is transfer the Trigger Logic inside a separate Apex Class file, then call that Apex Class in both the Trigger and upon click of the Custom Button. Hope this helps!

sindhu@demo.comsindhu@demo.com
Hi Nik, I have a trigger as following and it is working perfectly fine when a record is created on case object. I want this trigger to be executed on click of a button on "case" detail page. ----------------------------------**------------------------------------------- trigger AutoCheck on Case (after insert) { for (Case cs : Trigger.new) { cs=[select AccountId,Engine_Model__c,Case_Type__c from Case where Id =: cs.Id]; Account acc=[select id,Name from Account where Id =: cs.AccountId]; integer n=[SELECT Count() from Support_Agreement__c Where Company__r.Id=:cs.AccountId]; if(n>0) { Support_Agreement__c sp=[SELECT Company__r.Id,CFM_3__c,CFM_5__c,CFM_7__c,Engines__c,Publications__c,Spares__c,Training__c,Tool_Drawings__c FROM Support_Agreement__c WHERE Company__r.Id=:cs.AccountId]; if( ((sp.CFM_3__c==true && cs.Engine_Model__c=='CFM-3') ||(sp.CFM_5__c==true && cs.Engine_Model__c=='CFM-5') || (sp.CFM_7__c==true && cs.Engine_Model__c=='CFM-7')) && ((sp.Engines__c==true && cs.Case_Type__c=='Engines')||(sp.Publications__c==true && cs.Case_Type__c=='Publications')||(sp.Spares__c==true && cs.Case_Type__c=='Spares')||(sp.Training__c==true && cs.Case_Type__c=='Training')||(sp.Tool_Drawings__c==true && cs.Case_Type__c=='Tool Drawings')) ) { cs.GTA_Check_Status__c='Valid GTA'; } if( (cs.Engine_Model__c=='CFM-2')||(sp.CFM_3__c==false && cs.Engine_Model__c=='CFM-3') ||(sp.CFM_5__c==false && cs.Engine_Model__c=='CFM-5') || (sp.CFM_7__c==false && cs.Engine_Model__c=='CFM-7') ) { cs.GTA_Check_Status__c='GTA Model Invalid'; } if( (cs.Case_Type__c=='Diagnostics')||(cs.Case_Type__c=='Engines'&&sp.Engines__c==false)||(cs.Case_Type__c=='Publications' && sp.Publications__c==false)||(cs.Case_Type__c=='Spares' && sp.Spares__c==false)||(cs.Case_Type__c=='Training' && sp.Training__c==false )||(cs.Case_Type__c=='Tool Drawings' && sp.Tool_Drawings__c==false) ) { cs.GTA_Check_Status__c='GTA Type Invalid'; } } if(n==0) { cs.GTA_Check_Status__c='No GTA Found'; } update cs; } } -------------------------**---------------------------------------- I want the same trigger execution to be performed on click of a button. I have heard that it can done by writing a apex class using webservices and Onclick javascript functionality in button. Can You please help me out with this? Regards, Sindhu
Jerun JoseJerun Jose
What you can do is to have the trigger logic moved to a class method, and then call that class method called on a button click.

You can refer to this cookbook on how to call classes using javascript
http://developer.force.com/cookbook/recipe/creating-a-button-with-apex

Or you can call a vf page action on click of the button.

I would prefer the VF route, gives more control.
This was selected as the best answer
Niket SFNiket SF
Hello Jerun, Agree with you. -- Thanks and Regards, *Niket Chandane.* Mobile: +91 9730250818 E-mail: niket.c2009@gmail.com Blog: http://niketc.blogspot.com *Passionate for your Success*
sindhu@demo.comsindhu@demo.com
Thank you!! Prob solved with VF page and Action method Regards, Sindhura.G
sethi0007771.395862520819969E12sethi0007771.395862520819969E12
If you don''t want to use VF page , and want to execute trigger on button click , the you may use below code to update any field through trigger
{!requireScript("/soap/ajax/26.0/connection.js")} 
var account = new sforce.SObject("case"); 
case.id = ("{!case.Id}") 

case.reminder__c = true; 
sforce.connection.update([case); 
window.location.reload();

After that, You may execute trigger on After Update (checking if(reminder__c  == true)  .. 
Thanks & Regards
Rahul Sethi