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
Shruti NigamShruti Nigam 

how to update opportunity product from Account

Hi all,

I want to write a trigger in Account to update and create opportunity product when account is created.
There is 2 field in account amount1 and amount2.
If both field is not equal to null then a new record is created  total amount=amount1*amount2 in prouct

Thanks in advance
Ajay K DubediAjay K Dubedi
Hi Shruti,
Try below code:

//trigger code
trigger TriggerOnAccount on Account (after insert){
    if(Trigger.isAfter && Trigger.isInsert){
     CreateProductOnTrigger.productInsert(trigger.new);
    }
}
//helper code
public class CreateProductOnTrigger {
    public static void productInsert(List<Account> accList){
        List<Opportunity> oppList = new List<Opportunity>();
        Map<Id,Account> accountIdvsAccount = new Map<Id,Account>();
        Pricebook2 pb = [select Id, IsActive from PriceBook2 where IsStandard=True];
        
        for(Account accObj : accList){
            if(accObj.Amount1__c == null || accObj.Amount2__c == null) {
                accObj.addError('Amount1__c and Amount2__c can not be null');
            }else {
                System.debug('oppList>>>>>>>>>>>>>>>>>>>>>>>');
                accountIdvsAccount.put(accObj.Id,accObj);
                Opportunity oppObj = new Opportunity();
                oppObj.Name = 'Test Oppo'+accObj.Name;
                oppObj.StageName = 'Prospecting';
                System.debug('oppList>>>>>>>>>>>>>>>>>>>>>>>');
                oppObj.CloseDate = System.today().addDays(10);
                oppObj.AccountId = accObj.Id;
                System.debug('oppList>>>>>>>>>>>>>>>>>>>>>>>');
                oppList.add(oppObj);
                System.debug('oppList'+oppList);
            }
        }
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        insert oppList;
        Product2 p1 = new Product2();
        p1.Name='Prod 1';
        p1.Family='Container';
        p1.Description='Prod 1 Description';
          insert p1; 
        PricebookEntry pbe = new PricebookEntry ();
        pbe.Pricebook2Id=pb.id;
        pbe.Product2Id=p1.id;
        pbe.IsActive=true;
        pbe.UnitPrice=100.0;
        insert pbe;
        for(Opportunity Oppobj : oppList){
            OpportunityLineItem oliObj = new OpportunityLineItem();
            oliObj.OpportunityId = Oppobj.Id;
            oliObj.PricebookEntryId = pbe.Id;
            oliObj.Product2Id = p1.Id;
            oliObj.Quantity = 2;
            Account acc = accountIdvsAccount.get(Oppobj.AccountId);
            oliObj.TotalPrice = acc.Amount1__c*acc.Amount2__c;
            oliList.add(oliObj);   
        }
        insert oliList;
    }
}

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