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
Projjwal LahiriProjjwal Lahiri 

Update Account Field with an Account in Opportunity using Apex Trigger

I am using standard Opportunity and I am creating an Account once the Opportunity stage "Closed Won" using a trigger. But after creating the Account I want to update the Account field in the Opportunity with the newly created Account (means I want to link this Opportunity with the newly created Account). Please suggest me as soon as possible how to do this ?
(Very Urgent)
Bellow trigger I have written to create an Account once Opportunity "Closed Own"

trigger UpdateAccount on Opportunity (after insert, after update) {
    
    List <Account> acc = new List <Account>();
  
        for(Opportunity opp : Trigger.new)
        {
                if(opp.Name!=NULL && opp.AccountId!=NULL && opp.PB_Customer_Group__c=='First Time User' && opp.StageName=='Closed Won')
                {
                    Account acc1=new Account();
                    
                    acc1.Name=opp.Name;
                    
                    acc.add(acc1);
                    
                }
                
                
        }
        
        
        try {
                insert acc;
                   
            } 
            
        catch (system.Dmlexception e) {
                system.debug (e);
        }
        
  }


 
Suraj TripathiSuraj Tripathi
Hi Projjwal Lahiri,

You can try this code,
 
if(trigger.isafter && trigger.isupdate){

        List <Account> acc = new List <Account>();
        Set<id> oppIds = new Set<Id>();
        List<Opportunity> oppWithoutAccount = new List<Opportunity>();

        for(Opportunity opp : Trigger.new)
        {
            if(opp.Name!=NULL && opp.AccountId!=NULL && opp.PB_Customer_Group__c=='First Time User' && opp.StageName=='Closed Won')
            {
                Account acc1=new Account();
                acc1.Name=opp.Id;

                acc.add(acc1);
                oppIds.add(opp.Id);
            }
        }

        try {
            insert acc;

            oppWithoutAccount = [Select id,name,AccountId from Opportunity where Id in : oppIds];

            for(Opportunity opp : oppWithoutAccount){

                for(Account accinsideopp : acc)
                {
                    if(opp.Id == accinsideopp.name) {
                        accinsideopp.name=opp.name;
                        opp.AccountId = accinsideopp.Id;
                    }
                }
            }
            update oppWithoutAccount;
            update acc;
        }

        catch (system.Dmlexception e) {
            system.debug (e);
        }

    }

This Code is working Fine with your requirement and bulkified also.

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Regards
Suraj