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
BegginerBegginer 

Vf page button and trigger

can we execute a trigger when a button say 'submit' which is on the VF page is clicked, then the trigger should create a record on testobj__c
is it possible to do so? if so could anyone help me with the approach 

Thanks in Advance.
Lokesh KumarLokesh Kumar

<apex:commandButton value="create" action="{!doSaveAndNew}"/>




public class somecontroller{

   publis PageReference doSaveAndNew(){
       testObject__c obj = new testObject__c ( /** required fields **/ );
       try{
           insert obj;
       }
       catch(Exception ex){
        /** some actions  **/
       }
       return new PageReference ('/' + obj.Id);
   }
}
HI,

You can write a controller on submit of command button from visualforce page and write a code in controller to create a record.

Thanks !
 
BegginerBegginer
Hi Lokesh,

Thanks for the quick response but I have a business login on the trigger that should be executed and if true a record should be created. so, how should I call my trigger on the above snippet?
 
Lokesh KumarLokesh Kumar
you cannot invoke a trigger until unless create a record so if you create a record using this controller the trigger will get called through the controller on insert event so try this.

Thanks !
rajat Maheshwari 6rajat Maheshwari 6

Begginer,

If submit button is placed on your vfPage, then we will utilize the coding part in corresponding controller/extension.but Trigger only know events.when there will event occur, trigger will operate to fire.

So, As I already told you that, Trigger will not be called from submit button directly, Will discuss over call :)

Thanks
Rajat maheshwari
rajatzmaheshwari@gmail.com

SalesFORCE_enFORCErSalesFORCE_enFORCEr
No you can't invoke a trigger directly from a click of a button so just put your trigger logic in your command button action method and create your record directly in the controller.