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
The new LearnerThe new Learner 

how to update account with product value

Hi Experts,

I have requirment i need to update account field  product_value__c with the  product  field Family__c( all the product values has to add to product value field),when ever the opprotunity stage is closed. to achieve this i have written batch class on opportunity but its not working as expected. Its taking lot of time to update records.
 
global class batchAccountUpdate implements Database.Batchable<sObject> {
    
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        
        String query = 'SELECT id,Accountid,stagename,IsRenewal__c FROM Opportunity ';
        
        return Database.getQueryLocator(query);
        
    }
    

    
    global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
        
        set<id>accountids= new set<id>();
        set<id>opptyids= new set<id>();
        set<Id> addOn = new set<Id>();
        list<account>accountupdate= new list<account>();
        map<id,account> accmap = new map<id,account>();
        Map<id,OpportunityLineItem > accountid=new map<id,OpportunityLineItem >();  
         Map<id,Product2 > products=new map<id,Product2 >();
        for(Opportunity o : scope)
        {
            if(o.stagename=='closed')
            {
              
                opptyids.add(o.id);
            }  
            
        }
        
        for(Product2 oli : [SELECT id,Business__c,Family__c FROM Product2 where Business__c = 'sales']) 
        {
            
            addOn.add(oli.Id);
            products.put(oli.id,oli);
             
        }
        
        Map<Id,OpportunityLineItem > oppsToget = new Map<Id,OpportunityLineItem >([SELECT id,Product_Lookup__r.Business__c,Product_Lookup__r.Family__c,OpportunityId,Product_Lookup__c,opportunity.accountid,opportunity.StageName FROM OpportunityLineItem where Product_Lookup__c IN:products.keyset() and opportunityid in:opptyids]);
       
        for (OpportunityLineItem oli3: oppsToget.values())
        {
            accountid.put(oli3.opportunity.accountid,oli3);
           
            
        } 
        
        Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,product_value__c from account where id in :accountid.keyset()]);    
        for(account a:mapAccount.values())
        {
            if(accountid.containskey(a.id))
            {
                system.debug('@@accountid.containskey(a.id)'+accountid.containskey(a.id)); 
                for(OpportunityLineItem line: accountid.values())
                {
                    if(line.Product_Lookup__c!= null)
                    {
                     
                           String prodstring='';
                           set<string>unique=new set<String>();  
                           for(String s:line.Product_Lookup__r.Family__c.split(','))
                           {
                           unique.add(s);
                            system.debug('@@unique'+unique);  
                           }
                            if(unique.size()!=0){
                        for(String st : unique ){  
                            prodstring = prodstring +','+ st;
                              system.debug('@@prodstring '+prodstring );
                            }
                         }
                        a.product_value__c = prodstring;
                      
                        accountupdate.add(a);
                      
                        
                       
                    }  
                }
            }
        }
        accmap.putall(accountupdate);

        if(!accmap.isEmpty()){
            Database.SaveResult[] IsShareResult = Database.update(accmap.values(), false);
            
        }
    }   
    
 
    
    global void finish(Database.BatchableContext BC) {
        
    }
    
}

 
sachinarorasfsachinarorasf
Hi The new Learner,

I have gone through your problem.

You can write a trigger for the same scenario on the Opportunity object instead of the batch class. The trigger will run on update (and if you want then on insert too) to update the related records of the Account object only if the stage of the Opportunity record is closed.

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
The new LearnerThe new Learner
hi Sachin,

I am getting cpu limit issue , once i update my code in trigger, can you help me out.