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
Brad007Brad007 

System.ListException: Duplicate id in list

Hi 

 

I am having the above error and i understand that its beacuse i am updating my object over and over . As the if contion is true i am updating again and again. Can anyone please suggest a  work around as how i can have updates even after if conditions.

trigger APFileUpdates on Files__c (after insert) {

    if(trigger.isInsert)
    {  
      System.debug('Trigger is fired.');
      List<Files__c> APFiles  = trigger.new;
      List<AP_Application__c> UpdateAPApp = new List<AP_Application__c>();
      //List<Files__c> processList= new List<Files__c>();       
        set<String> zips = new set<String>();
      string P1 = 'Photo1.jpg';
      string P2 = 'Photo2.jpg';
      string P3 = 'Photo3.jpg';
      string P4 = 'Photo4.jpg';      
      if(trigger.new != null && trigger.new.size() > 0)
      {
        System.debug('Trigger is fired.');
        for (Files__c file: APFiles)
        {  
          string path = file.Path__c;                      
          AP_Application__c App = [Select a.Id,a.IsExist_Photo1__c, a.IsExist_Photo2__c, a.IsExist_Photo3__c, a.IsExist_Photo4__c, a.Au_Pair__c,External_ID_Photo1__c From AP_Application__c a where id =: file.AP_Application__c ];             
          if(path.contains(p1))
          {
            App.IsExist_Photo1__c = true;
            App.External_ID_Photo1__c = file.External_ID__c;
            UpdateAPApp.add(App);  
          }          
          System.debug('APResultApplication:' +App);                      
          
          if(path.contains(p2))
          {              
            App.IsExist_Photo2__c = true;
            App.External_ID_Photo2__c = file.External_ID__c;
            UpdateAPApp.add(App);                        
          }
          if(path.contains(p3))
          {            
            App.IsExist_Photo3__c = true;
            App.External_ID_Photo3__c = file.External_ID__c;
            UpdateAPApp.add(App);                        
          }
          if(path.contains(p4))
          {             
            App.IsExist_Photo4__c = true;
            App.External_ID_Photo4__c = file.External_ID__c;
            UpdateAPApp.add(App);                        
          }
                          
        }
        update UpdateAPApp;
      }
      
    }
}

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Hi Brad,

 

Your code contains query inside a for loop which would throw exception at time of bulk insertion.

I have optimised the code for you, check if it helps:

trigger APFileUpdates on Files__c (after insert) 
{
	if(trigger.isInsert)
	{
		Set<Id> SetAppId = new Set<Id>();
		for (Files__c file: trigger.new)
			SetAppId.add(file.AP_Application__c);
		//	Using map to get all the AP_Applications related to Inserted files.
		Map<Id, AP_Application__c> mapApp = new Map<Id, AP_Application__c>([Select a.Id,a.IsExist_Photo1__c, a.IsExist_Photo2__c, a.IsExist_Photo3__c, a.IsExist_Photo4__c,
			a.Au_Pair__c,External_ID_Photo1__c From AP_Application__c a where id =: SetAppId]);
		set<String> zips = new set<String>();
		string P1 = 'Photo1.jpg';
		string P2 = 'Photo2.jpg';
		string P3 = 'Photo3.jpg';
		string P4 = 'Photo4.jpg';
		for (Files__c file: trigger.new)
		{
			//	Check if this Id exists in the mapApp
			If(mapApp.contains(file.AP_Application__c))
			{
				if(file.Path__c.contains(p1))
				{
					mapApp.get(file.AP_Application__c).IsExist_Photo1__c = true;
					mapApp.get(file.AP_Application__c).External_ID_Photo1__c = file.External_ID__c;
				}          
				System.debug('APResultApplication:' +mapApp.get(file.AP_Application__c));                      

				if(file.Path__c.contains(p2))
				{              
					mapApp.get(file.AP_Application__c).IsExist_Photo2__c = true;
					mapApp.get(file.AP_Application__c).External_ID_Photo2__c = file.External_ID__c;
				}
				if(file.Path__c.contains(p3))
				{            
					mapApp.get(file.AP_Application__c).IsExist_Photo3__c = true;
					mapApp.get(file.AP_Application__c).External_ID_Photo3__c = file.External_ID__c;
				}
				if(file.Path__c.contains(p4))
				{             
					mapApp.get(file.AP_Application__c).IsExist_Photo4__c = true;
					mapApp.get(file.AP_Application__c).External_ID_Photo4__c = file.External_ID__c;                        
				}
			}
		}
		//	update mapApp.values List
		update mapApp.values();      
	}
}

 

All Answers

Starz26Starz26

I took a quick stab not knowing your use case. The Duplicate ID issue was due to the multiple UpdaeAPApp.add(App) for the same APP row.

 

trigger APFileUpdates on Files__c (after insert) {

    if(trigger.isInsert)
    {  
      System.debug('Trigger is fired.');
      List<AP_Application__c> UpdateAPApp = new List<AP_Application__c>();
      set<String> zips = new set<String>();
      string P1 = 'Photo1.jpg';
      string P2 = 'Photo2.jpg';
      string P3 = 'Photo3.jpg';
      string P4 = 'Photo4.jpg';      
        System.debug('Trigger is fired.');

        for (Files__c file: trigger.new)
        {  
          string path = file.Path__c;                      
          AP_Application__c App = [Select a.Id,a.IsExist_Photo1__c, a.IsExist_Photo2__c, a.IsExist_Photo3__c, a.IsExist_Photo4__c, a.Au_Pair__c,External_ID_Photo1__c From AP_Application__c a where id =: file.AP_Application__c ];             
          if(path.contains(p1))
          {
            App.IsExist_Photo1__c = true;
            App.External_ID_Photo1__c = file.External_ID__c;
          }          
          System.debug('APResultApplication:' +App);                      
          
          if(path.contains(p2))
          {              
            App.IsExist_Photo2__c = true;
            App.External_ID_Photo2__c = file.External_ID__c;
          }
          if(path.contains(p3))
          {            
            App.IsExist_Photo3__c = true;
            App.External_ID_Photo3__c = file.External_ID__c;
          }
          if(path.contains(p4))
          {             
            App.IsExist_Photo4__c = true;
            App.External_ID_Photo4__c = file.External_ID__c;
          }
      UpdateAPApp.add(App)        
                  
        }
        update UpdateAPApp;
      
      
    }
}

 

Brad007Brad007

Hi,

 

Thank you for your response. Yes its due to multiple updates on the same App obj based on the if condition. Is there a work around to fix this issue. Please provide any sample if you do have that would be greatly helpful .

 

 

Thank you,

Sandeep001Sandeep001

Try changing your collection from List to Set, since Set maintains unique values.

Rahul SharmaRahul Sharma

Hi Brad,

 

Your code contains query inside a for loop which would throw exception at time of bulk insertion.

I have optimised the code for you, check if it helps:

trigger APFileUpdates on Files__c (after insert) 
{
	if(trigger.isInsert)
	{
		Set<Id> SetAppId = new Set<Id>();
		for (Files__c file: trigger.new)
			SetAppId.add(file.AP_Application__c);
		//	Using map to get all the AP_Applications related to Inserted files.
		Map<Id, AP_Application__c> mapApp = new Map<Id, AP_Application__c>([Select a.Id,a.IsExist_Photo1__c, a.IsExist_Photo2__c, a.IsExist_Photo3__c, a.IsExist_Photo4__c,
			a.Au_Pair__c,External_ID_Photo1__c From AP_Application__c a where id =: SetAppId]);
		set<String> zips = new set<String>();
		string P1 = 'Photo1.jpg';
		string P2 = 'Photo2.jpg';
		string P3 = 'Photo3.jpg';
		string P4 = 'Photo4.jpg';
		for (Files__c file: trigger.new)
		{
			//	Check if this Id exists in the mapApp
			If(mapApp.contains(file.AP_Application__c))
			{
				if(file.Path__c.contains(p1))
				{
					mapApp.get(file.AP_Application__c).IsExist_Photo1__c = true;
					mapApp.get(file.AP_Application__c).External_ID_Photo1__c = file.External_ID__c;
				}          
				System.debug('APResultApplication:' +mapApp.get(file.AP_Application__c));                      

				if(file.Path__c.contains(p2))
				{              
					mapApp.get(file.AP_Application__c).IsExist_Photo2__c = true;
					mapApp.get(file.AP_Application__c).External_ID_Photo2__c = file.External_ID__c;
				}
				if(file.Path__c.contains(p3))
				{            
					mapApp.get(file.AP_Application__c).IsExist_Photo3__c = true;
					mapApp.get(file.AP_Application__c).External_ID_Photo3__c = file.External_ID__c;
				}
				if(file.Path__c.contains(p4))
				{             
					mapApp.get(file.AP_Application__c).IsExist_Photo4__c = true;
					mapApp.get(file.AP_Application__c).External_ID_Photo4__c = file.External_ID__c;                        
				}
			}
		}
		//	update mapApp.values List
		update mapApp.values();      
	}
}

 

This was selected as the best answer
Starz26Starz26

Brad007 wrote:

Hi,

 

Thank you for your response. Yes its due to multiple updates on the same App obj based on the if condition. Is there a work around to fix this issue. Please provide any sample if you do have that would be greatly helpful .

 

 

Thank you,


Um, yea, I rewrote your code in my post and the solution was there. HOWEVER, Rahul also solved the issue you did not inquire about and it should be used as it addresses the bulk data issue which I did not address.

Brad007Brad007

Hi Rahul ,

 

Thank you so much for your time and response it worked. But the obly change i did was 

if(mapApp.contains(file.AP_Application__c)) says incorrect signature of Map<Id,Ap_Application> with contains method.

So when i removed that if condition it worked fine as i already do get the related list.

 

Thank you once again.

Brad007Brad007

Hi Starzz,

 

Yes solution did work.

 

Thank you for your time and effort starzz.