+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Apex Update with bulk Records

Hi,

I have 2 scenarios in custom object

1. If it's old user system should get update 

2. If it's a new user system get insert with list of users.

i have done with single record and getting confused for bulk records.

Can you please refer my coding and advise me how can i create for bulk records for update existing records.

If you are suggesting with MAP it's really appriciate.

Apex Controller
/*Upadte Existing Record*/
            set<id> ExUser = new set<id>();
            set<id> newUser = new set<id>();
            string newRole;
            string newAccess;
            List<GD_Quote_Team__c> ExMembers = [select ID,GD_User__c,GD_User__r.Name from GD_Quote_Team__c where GD_Quotes__c =: quoteId];
            
            for(GD_Quote_Team__c ex_mem : ExMembers){
               GD_Quote_Team__c qtm = new GD_Quote_Team__c();
                qtm.Id = ex_mem.Id;
                qtm.GD_User__c = ex_mem.GD_User__c;
                ExUser.add(qtm.GD_User__c);
               
            }
            
            for(GD_Quote_Team__c new_mem : qtTeamList){
                newUser.add(new_mem.GD_User__c);
                newRole = new_mem.GD_Team_Role__c;
                newAccess = new_mem.GD_Quote_Access__c;
                
            }
            List<GD_Quote_Team__c> updtMembers = new List<GD_Quote_Team__c>();
            if(ExUser == newUser){
                GD_Quote_Team__c ExMembers1 = [select ID,GD_User__c,GD_User__r.Name,GD_Team_Role__c,GD_Quote_Access__c from GD_Quote_Team__c where GD_Quotes__c =: quoteId and GD_User__c =: ExUser];
                GD_Quote_Team__c NwMembers1 = [select ID,GD_User__c,GD_User__r.Name,GD_Team_Role__c,GD_Quote_Access__c from GD_Quote_Team__c where GD_Quotes__c =: quoteId and GD_User__c =: newUser]; 
                GD_Quote_Team__c qtm1 = new GD_Quote_Team__c();
                qtm1.Id = ExMembers1.Id;
                qtm1.GD_User__c = NwMembers1.GD_User__c;
                qtm1.GD_Team_Role__c = newRole;
                qtm1.GD_Quote_Access__c = newAccess;
                updtMembers.add(qtm1);  
                update updtMembers;
            } else if(ExUser != newUser){
                if(qtTeamList.size()>0){upsert qtTeamList;}
            }

Thanks In Advance,
Soundar.
Kiran ChodavadiyaKiran Chodavadiya
You can use UPSERT DML instead of Update.
By this way if record has existatnce it only update or if not then it will create new.