+ Start a Discussion
SFDC Lightning 18SFDC Lightning 18 

Need help on trigger for mass uploaded, for single record trigger is working fine.

Hi, I need to update the trigger for mass update when opportunity created new record there are some lookup fileds (opp.Client__c = p.Client__c) are mapped. I am using data loader to update opportunity records, but lookup's fields not updating.Please check the below code.Thanks Advanced.

 
Trigger UpdateAccLookups on Opportunity (before insert, before update) 
{ 

    Set<Id> ProjectIds = new Set<Id>();
        for (Opportunity  o : trigger.new) 
            {
                ProjectIds.add(o.Project__c);
            }


Map<Id, Project__c> ProjectMap = new Map<Id, Project__c>();

    list<Project__c> Projects = new list<Project__c>();
    Projects = [Select Id, Name,Remarks__c,Main_Contractor__c,Client__c,Architect_Consultant__c,Facade_Consultant__c,Country__c,City__c,Other_City__c from Project__c where Id IN : ProjectIds];

    for (Opportunity  opp : trigger.new) 
    {
       for(project__C p : Projects){        
            opp.Main_Contractor__c = p.Main_Contractor__c;
            opp.Client__c = p.Client__c;
            opp.AccountID = p.Client__c;
            opp.Architect_Consultant__c = p.Architect_Consultant__c;
            opp.Facade_Consultant__c = p.Facade_Consultant__c;
            opp.Country__c = p.Country__c;
            opp.City__c= p.City__c;
            opp.Other_City__c = p.Other_City__c; 
 
            
      }  
    }
    
}
Best Answer chosen by SFDC Lightning 18
Raj VakatiRaj Vakati
Try this can you explan what you are tryng to do 
 
Trigger UpdateAccLookups on Opportunity (before insert, before update) 
{ 


		Map<Id,Id> ProjectIds = new Map<id,Id>();
        for (Opportunity  o : trigger.new) 
            {
                ProjectIds.put(o.Project__c,o.Id);
            }


	Map<Id, Project__c> ProjectMap = new Map<Id, Project__c>();

   Map<Id,Project__c> Projects = new Map<Id,Project__c>([Select Id, Name,Remarks__c,Main_Contractor__c,Client__c,Architect_Consultant__c,Facade_Consultant__c,Country__c,City__c,Other_City__c from Project__c where Id IN : ProjectIds.keySet()]);
   for(Opportunity o :Trigger.new){
	   Project__c p = Projects.get(o.Project__c);
	   opp.Main_Contractor__c = p.Main_Contractor__c;
            opp.Client__c = p.Client__c;
            opp.AccountID = p.Client__c;
            opp.Architect_Consultant__c = p.Architect_Consultant__c;
            opp.Facade_Consultant__c = p.Facade_Consultant__c;
            opp.Country__c = p.Country__c;
            opp.City__c= p.City__c;
            opp.Other_City__c = p.Other_City__c; 
   }
    

    
}

 

All Answers

Raj VakatiRaj Vakati
try this
 
Trigger UpdateAccLookups on Opportunity (before insert, before update) 
{ 

    Map<Id,Id> ProjectIds = new Map<id,Id>();
        for (Opportunity  o : trigger.new) 
            {
                ProjectIds.add(o.Project__c .o.Id);
            }


Map<Id, Project__c> ProjectMap = new Map<Id, Project__c>();

   Map<Id,Project> Projects = new Map<Id,Project>([Select Id, Name,Remarks__c,Main_Contractor__c,Client__c,Architect_Consultant__c,Facade_Consultant__c,Country__c,City__c,Other_City__c from Project__c where Id IN : ProjectIds.keySet()]);

   List<Opportunity> oppToUpdate = new List<Opportunity>();
       for(project__C p : Projects.values()){ 
Opportunity opp = new Opportunity(Id =ProjectId.get(p.Id);       
            opp.Main_Contractor__c = p.Main_Contractor__c;
            opp.Client__c = p.Client__c;
            opp.AccountID = p.Client__c;
            opp.Architect_Consultant__c = p.Architect_Consultant__c;
            opp.Facade_Consultant__c = p.Facade_Consultant__c;
            opp.Country__c = p.Country__c;
            opp.City__c= p.City__c;
            opp.Other_City__c = p.Other_City__c; 
 oppToUpdate.add(opp);
            
      }  
	  if(oppToUpdate.size()>0){
	  update oppToUpdate;
	  }
  
}

 
Raj VakatiRaj Vakati
Use this
Trigger UpdateAccLookups on Opportunity (before insert, before update) 
{ 

    Map<Id,Id> ProjectIds = new Map<id,Id>();
        for (Opportunity  o : trigger.new) 
            {
                ProjectIds.add(o.Project__c,o.Id);
            }


Map<Id, Project__c> ProjectMap = new Map<Id, Project__c>();

   Map<Id,Project> Projects = new Map<Id,Project>([Select Id, Name,Remarks__c,Main_Contractor__c,Client__c,Architect_Consultant__c,Facade_Consultant__c,Country__c,City__c,Other_City__c from Project__c where Id IN : ProjectIds.keySet()]);

   List<Opportunity> oppToUpdate = new List<Opportunity>();
       for(project__C p : Projects.values()){ 
Opportunity opp = new Opportunity(Id =ProjectIds.get(p.Id);       
            opp.Main_Contractor__c = p.Main_Contractor__c;
            opp.Client__c = p.Client__c;
            opp.AccountID = p.Client__c;
            opp.Architect_Consultant__c = p.Architect_Consultant__c;
            opp.Facade_Consultant__c = p.Facade_Consultant__c;
            opp.Country__c = p.Country__c;
            opp.City__c= p.City__c;
            opp.Other_City__c = p.Other_City__c; 
 oppToUpdate.add(opp);
            
      }  
	  if(oppToUpdate.size()>0){
	  update oppToUpdate;
	  }
  
}

 
SFDC Lightning 18SFDC Lightning 18
I getting the error like this 

User-added image
 
Trigger UpdateAccLookups on Opportunity (before insert, before update) 
{ 

    Map<Id,Id> ProjectIds = new Map<id,Id>();
        for (Opportunity  o : trigger.new) 
            {
                ProjectIds.put(o.Project__c,o.Id);
            }


Map<Id, Project__c> ProjectMap = new Map<Id, Project__c>();

   Map<Id,Project__c> Projects = new Map<Id,Project__c>([Select Id, Name,Remarks__c,Main_Contractor__c,Client__c,Architect_Consultant__c,Facade_Consultant__c,Country__c,City__c,Other_City__c from Project__c where Id IN : ProjectIds.keySet()]);

   List<Opportunity> oppToUpdate = new List<Opportunity>();
       for(project__C p : Projects.values()){ 
Opportunity opp = new Opportunity(Id = ProjectIds.get(p.Id));       
            opp.Main_Contractor__c = p.Main_Contractor__c;
            opp.Client__c = p.Client__c;
            opp.AccountID = p.Client__c;
            opp.Architect_Consultant__c = p.Architect_Consultant__c;
            opp.Facade_Consultant__c = p.Facade_Consultant__c;
            opp.Country__c = p.Country__c;
            opp.City__c= p.City__c;
            opp.Other_City__c = p.Other_City__c; 
 oppToUpdate.add(opp);
            
      }  
      if(oppToUpdate.size()>0){
      update oppToUpdate;
      }
  
}

 
Raj VakatiRaj Vakati
Try this
Trigger UpdateAccLookups on Opportunity (before insert, before update) 
{ 

    Map<Id,Id> ProjectIds = new Map<id,Id>();
        for (Opportunity  o : trigger.new) 
            {
                ProjectIds.put(o.Project__c,o.Id);
            }


Map<Id, Project__c> ProjectMap = new Map<Id, Project__c>();

   Map<Id,Project__c> Projects = new Map<Id,Project__c>([Select Id, Name,Remarks__c,Main_Contractor__c,Client__c,Architect_Consultant__c,Facade_Consultant__c,Country__c,City__c,Other_City__c from Project__c where Id IN : ProjectIds.keySet()]);

   List<Opportunity> oppToUpdate = new List<Opportunity>();
       for(project__C p : Projects.values()){ 
Opportunity opp = new Opportunity(Id = ProjectIds.get(p.Id));       
            opp.Main_Contractor__c = p.Main_Contractor__c;
            opp.Client__c = p.Client__c;
            opp.AccountID = p.Client__c;
            opp.Architect_Consultant__c = p.Architect_Consultant__c;
            opp.Facade_Consultant__c = p.Facade_Consultant__c;
            opp.Country__c = p.Country__c;
            opp.City__c= p.City__c;
            opp.Other_City__c = p.Other_City__c; 
 oppToUpdate.add(opp);
            
      }  
      if(oppToUpdate.size()>0){
      upsert oppToUpdate;
      }
  
}

 
SFDC Lightning 18SFDC Lightning 18
User-added image


Apex trigger UpdateAccLookups caused an unexpected exception, contact your administrator: UpdateAccLookups: execution of BeforeInsert caused by: System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity Name, Opportunity Stage, Opportunity Close Date]: [Opportunity Name, Opportunity Stage, Opportunity Close Date]: Trigger.UpdateAccLookups: line 30, column 1
SFDC Lightning 18SFDC Lightning 18
I debug the oppToUpdate list, below is the result,,, Opportunity id comes null.

00:53:49.0 (16906851)|USER_DEBUG|[28]|DEBUG|oppToUpdate====(Opportunity:{Id=null, Main_Contractor__c=null, Client__c=0011D00000M1MV8QAN, AccountId=0011D00000M1MV8QAN, Architect_Consultant__c=null, Facade_Consultant__c=null, Country__c=UAE, City__c=Abu Dhabi, Other_City__c=null})
Raj VakatiRaj Vakati
Try this can you explan what you are tryng to do 
 
Trigger UpdateAccLookups on Opportunity (before insert, before update) 
{ 


		Map<Id,Id> ProjectIds = new Map<id,Id>();
        for (Opportunity  o : trigger.new) 
            {
                ProjectIds.put(o.Project__c,o.Id);
            }


	Map<Id, Project__c> ProjectMap = new Map<Id, Project__c>();

   Map<Id,Project__c> Projects = new Map<Id,Project__c>([Select Id, Name,Remarks__c,Main_Contractor__c,Client__c,Architect_Consultant__c,Facade_Consultant__c,Country__c,City__c,Other_City__c from Project__c where Id IN : ProjectIds.keySet()]);
   for(Opportunity o :Trigger.new){
	   Project__c p = Projects.get(o.Project__c);
	   opp.Main_Contractor__c = p.Main_Contractor__c;
            opp.Client__c = p.Client__c;
            opp.AccountID = p.Client__c;
            opp.Architect_Consultant__c = p.Architect_Consultant__c;
            opp.Facade_Consultant__c = p.Facade_Consultant__c;
            opp.Country__c = p.Country__c;
            opp.City__c= p.City__c;
            opp.Other_City__c = p.Other_City__c; 
   }
    

    
}

 
This was selected as the best answer
SFDC Lightning 18SFDC Lightning 18
Under Parent (Project__c) lookup fields I am mapping to child(Opportunity) lookup fields
Lookup fields are Client__c,Architect_Consultant__c Facade_Consultant__c etc.

When i create child record under parent the trigger is working fine, it's mapping all the lookup fields, but I have excel file to upload records in opportunity, at that time the trigger lookup, not mapping correct with parent lookups, its mismatching values for mass insert in opportunity.