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
raj jordanraj jordan 

i have created a custom field on account object along with one checkbox field and another custom field on custom object pricing and written a trigger such that when the check box field is selected the pricing should also get inserted

Now i want these trigger code in apex class and i want to call that apex class in trigger 
below is my code
trigger insertobject2 on Account (after insert, after update) {
 
Set<Id> Ids= new Set<Id>();
          for (Account  acc : Trigger.new) {
          Ids.add(acc.Id);        
          }
List<Account> accList = new List<Account>();
accList=[Select Id,name,SynctoPricing__c,Relatedpricing__c From Account where Id in :Ids];
          for(Account temp : accList )
          {
        if(temp.SynctoPricing__c==true){
	
         Pricing__c pri = new Pricing__c();
   
			pri.RelatedAccount__c = temp.Relatedpricing__c	;
			insert pri;
        }   
          } }

can anyone please help me through this
Thanaks in Advance
Best Answer chosen by raj jordan
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- Never add DML inside the for loop.

Please check below post to learn about trigger framwork
1) http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Update your code like below
trigger insertobject2 on Account (after insert, after update) {
	AccountTriggerHandler.insertPricing(Trigger.New);
}

Handler class like below
public with sharing class AccountTriggerHandler 
{
	public static void insertPricing( List<Account> newAccount)
    {
		List<Pricing__c> lstPricing = new List<Pricing__c>();
		for(Account temp : newAccount)
		{
			if(temp.SynctoPricing__c==true)
			{
				Pricing__c pri = new Pricing__c();
				pri.RelatedAccount__c = temp.Relatedpricing__c	;
				lstPricing.add(pri);
			}
		}
		if(lstPricing.size() > 0 ){
			insert lstPricing;
		}
	}
}

Let us know if this wll help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
NOTE:- Never add DML inside the for loop.

Please check below post to learn about trigger framwork
1) http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Update your code like below
trigger insertobject2 on Account (after insert, after update) {
	AccountTriggerHandler.insertPricing(Trigger.New);
}

Handler class like below
public with sharing class AccountTriggerHandler 
{
	public static void insertPricing( List<Account> newAccount)
    {
		List<Pricing__c> lstPricing = new List<Pricing__c>();
		for(Account temp : newAccount)
		{
			if(temp.SynctoPricing__c==true)
			{
				Pricing__c pri = new Pricing__c();
				pri.RelatedAccount__c = temp.Relatedpricing__c	;
				lstPricing.add(pri);
			}
		}
		if(lstPricing.size() > 0 ){
			insert lstPricing;
		}
	}
}

Let us know if this wll help you

 
This was selected as the best answer
raj jordanraj jordan
itb working fine amit 
thanks for ur help
raj jordanraj jordan
hi amit if  i want to do same from custom object to pricing to account is it possible?
if so can you please help me 
thanks in advance