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
Gaurav AgnihotriGaurav Agnihotri 

Trigger on Quotes to Update Opportunity AccountId

I have created a new Account Lookup field in Quotes. As soon as a user chooses that Account from Account Lookup, it should update AccountId on Opportunity with that lookup account. I created a trigger, but it does not seem to work. 
trigger updateOpptyAccount on Quote (before insert,before update) {
    for (Quote QuoteInLoop : Trigger.new){
        Boolean sMainAccount=QuoteInLoop.Main_Account__c;
        String sQuoteAccountId=QuoteInLoop.Pelco_Account_Name__c;
        String sOptyId=QuoteInLoop.OpportunityId;
        if(sMainAccount ){
            //QuoteInLoop.AccountId=sQuoteAccountId;
            List <Opportunity> Oppty1=[SELECT AccountId from Opportunity where Id = :sOptyId];
             Oppty1[0].AccountId=sQuoteAccountId;
            
        }
    }
}
I am a newbie, would appreciate any help/suggestion.
Best Answer chosen by Gaurav Agnihotri
Mohit Bansal6Mohit Bansal6
Hi Gaurav

Kindly refer below code:
 
trigger updateOpptyAccount on Quote (before insert,before update) {
    
     List<Opportunity> lOpportunityUpdate = new List<Opportunity>();
     Set<Id> sOppIds = new Set<Id>();
         
for (Quote QuoteInLoop : Trigger.new){
sOppIds.add(QuoteInLoop.OpportunityID);
     
     Map<Id,Opportunity> map_ID_Opp = new Map<Id,Opportunity>([Select id, name,AccountId from Opportunity where id in: sOppIds]);
      
     for (Quote QuoteInLoop : Trigger.new){
        Boolean sMainAccount=QuoteInLoop.Main_Account__c;
        Opportunity opp =  map_ID_Opp.get(QuoteInLoop.OpportunityID);
         if(QuoteInLoop.Main_Account__c != null && QuoteInLoop.Main_Account__c != opp.AccountId){
             opp.accountid = QuoteInLoop.Main_Account__c;
             lOpportunityUpdate.add(opp); 
         }
     }
     if(lOpportunityUpdate.size() > 0)
          update lOpportunityUpdate;
}

Regards
Mohit Bansal

Note: Please mark it as a "Best Answer", if helps you in resolving your query.

All Answers

MandarKhojeMandarKhoje
Hi Gaurav, I notice two things. 
  1. First things first. The reason your trigger doesnt work is because your trigger is on Quote object and on line 09 you are updating the field of Opportunity, which will never work. Trigger will only change values of Quote object. To update values for Opportunity object, you will have to explicitly call upsert or update  on the opportunities.
  2. ​You should *never* have a SOQL query inside a for loop. The reason is you will execute the query as many times as the number of Quotes, which is not at all desirable in apex development, since you might hit the governer limit and your query will fail. You should rather collect all the sOptyIds and write a query to pull all the Opportunities and populate a Map, outside the 'for' loop. 
Mohit Bansal6Mohit Bansal6
Hi Gaurav

Kindly refer below code:
 
trigger updateOpptyAccount on Quote (before insert,before update) {
    
     List<Opportunity> lOpportunityUpdate = new List<Opportunity>();
     Set<Id> sOppIds = new Set<Id>();
         
for (Quote QuoteInLoop : Trigger.new){
sOppIds.add(QuoteInLoop.OpportunityID);
     
     Map<Id,Opportunity> map_ID_Opp = new Map<Id,Opportunity>([Select id, name,AccountId from Opportunity where id in: sOppIds]);
      
     for (Quote QuoteInLoop : Trigger.new){
        Boolean sMainAccount=QuoteInLoop.Main_Account__c;
        Opportunity opp =  map_ID_Opp.get(QuoteInLoop.OpportunityID);
         if(QuoteInLoop.Main_Account__c != null && QuoteInLoop.Main_Account__c != opp.AccountId){
             opp.accountid = QuoteInLoop.Main_Account__c;
             lOpportunityUpdate.add(opp); 
         }
     }
     if(lOpportunityUpdate.size() > 0)
          update lOpportunityUpdate;
}

Regards
Mohit Bansal

Note: Please mark it as a "Best Answer", if helps you in resolving your query.
This was selected as the best answer