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
GMASJGMASJ 

Trigger System.ListException: Duplicate id

Hi, 

 I wrote a custom trigger to update all child based on the parent value Please find trigger below. 
trigger FortuneGlobalTrg on Account (after insert, after update) {

 Set<id> setOfParentId = new Set<Id>();
 Set<id> setOfParentIds = new Set<Id>();
 
 for(Account pt : trigger.new){
  setOfParentId.add(pt.id);
 }
 
 List<Account> listChild = new List<Account>([Select id, Global_2000_Rank__c,Fortune_Rank__c 
                                          from Account where ParentId in :setOfParentId or 
                                                             id in :setOfParentIds ]);
 List<Account> updatedlistChild = new List<Account>();
 
 for(Account pt : trigger.new){
  for(Account ch : listChild){
   ch.Global_2000_Rank__c = pt.Global_2000_Rank__c;
   ch.Fortune_Rank__c = pt.Fortune_Rank__c;
   
   updatedlistChild.add(ch);
  }
 }
 
   if (updatedlistChild.size() > 0 ){
    Update updatedlistChild;
   } 
  
  }

 I tested this in test instance which was working I see below error message when testing in production. Please suggest me how to fix this issue. 


caused by: System.DmlException: Update failed. First exception on row 0 with id 5503400000100IYDwAAO; first error: FIELD_INTEGRITY_EXCEPTION, If you set an end date, you must also set a start date.: Start Date: [StartDate]

Thanks
Sudhir
Pradip Kr. ShuklaPradip Kr. Shukla
Hi Sudhir,

On the Production environment, there must be some kind of validation/validation in the trigger which causing this issue plz check and rectify.

Hope this will help you.

Thanks
GMASJGMASJ
Thanks for you reply Pradip , I checked even disabled the valdiation for testing this looks something issue with code can you suggest to modify 
Pradip Kr. ShuklaPradip Kr. Shukla
May be some other triggers working on after/before update of account, as I can not see End & Start Dates here in your code piece.
GMASJGMASJ
Yes Pradip your right there is another trigger with same event getting fired looks like this is a old trigger on account do yu have any suggest how to fix this issue. 

 Shall I create a helper class and call trigger inside below trigger. 
 
trigger account_territory_lookup on Account (after insert, after update) 
{

 if(checkRecursive_AccountTerritory.runOnce())
  {
  
List<Territory_Lookup__c> TL = null;
Integer gzip;
String gstate;
String gcountry;
List<Account> leds = new List<Account>();
String onlynumber;
String str;
String numericString;

  for(Account l :  Trigger.new)
  {
  gcountry = l.BillingCountry;
  gstate   = l.BillingState;
  if ( l.BillingPostalCode != null)
   {
  str = l.BillingPostalCode;
  numericString = '';
  integer strLength = str.length();
  for(integer i =0;i<str.length();i++){
    String s= str.mid(i,1);
    if(s.isNumeric()){
        numericString +=s;
        gzip = Integer.Valueof(numericString); 
    }
   }
  
  }
}
  system.debug('Entered Country :' + gcountry);
  system.debug('Entered State :' + gstate);
  system.debug('Entered Zip :' + gzip);
 
Try
{
   if ( gcountry != null && gstate != null && gzip != null )
   {
     TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c
                          FROM Territory_Lookup__c
                          WHERE
                          Country__c = :gcountry and
                          State__c = :gstate and
                          Zip_Start__c <= :gzip and Zip_End__c >= :gzip limit 1];

      system.debug('All Have value');
   }
   else if ( gcountry != null && gstate != null )
   {
      TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c
            FROM Territory_Lookup__c
            WHERE
             Country__c = :gcountry and
              State__c = :gstate limit 1];

    system.debug('Country and State Have Value');
   }
   else if ( gcountry != null && gzip != null )
   {
      TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c
            FROM Territory_Lookup__c
            WHERE
            Country__c = :gcountry and
            Zip_Start__c <= :gzip and Zip_End__c >= :gzip limit 1];

    system.debug('Country and Zip Have Value');
   }
   else if ( gcountry != null )
   {
      TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c
            FROM Territory_Lookup__c
            WHERE
            Country__c = :gcountry limit 1];

    system.debug('Country Have Value');
   }
   else
   {
    system.debug('No value');
   }

if ( TL.size() > 0)
{
system.debug('Territory Country :' + TL[0].Country__c);
system.debug('Territory State :' + TL[0].State__c);
system.debug('Territory Zip Start :' + TL[0].Zip_Start__c);
system.debug('Territory Zip End :' + TL[0].Zip_End__c);

 for(Account uld :  Trigger.new){
   Account  uleds = new Account( Id = uld.id,Territory_Lookup__c = TL[0].ID);
   leds.add(uleds);
  }
   update leds;
}
else
{
    /* Country and State Block */
  if ( gcountry != null && gzip != null )
  {
   TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c
         FROM Territory_Lookup__c
         WHERE
         Country__c = :gcountry and
         State__c = :gstate limit 1];
  }
    system.debug('Nothing Found State');
    if ( TL.size() > 0)
    {
     system.debug('Nothing Territory Country :' + TL[0].Country__c);
     system.debug('Nothing Territory State :' + TL[0].State__c);

      for(Account uld :  Trigger.new){
      Account uleds = new Account( Id = uld.id,Territory_Lookup__c = TL[0].ID);
      leds.add(uleds);
        }
      update leds;

    }
    else
    {
    TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c
          FROM Territory_Lookup__c
          WHERE
          Country__c = :gcountry limit 1];
     system.debug('Nothing Territory Country :' + TL[0].Country__c);

     for(Account uld : Trigger.new){
      Account uleds = new Account( Id = uld.id,Territory_Lookup__c = TL[0].ID);
      leds.add(uleds);
        }
      update leds;
     }

    /* Country and Zip Block*/
if ( gcountry != null && gstate != null )
  {
   TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c
         FROM Territory_Lookup__c
         WHERE
         Country__c = :gcountry and
         Zip_Start__c <= :gzip and Zip_End__c >= :gzip limit 1 ];
  }
    system.debug('Nothing Found Zip');
    if ( TL.size() > 0)
    {
     system.debug('Nothing Territory Country :' + TL[0].Country__c);
     system.debug('Nothing Territory State :' + TL[0].State__c);

      for(Account uld :  Trigger.new){
      Account uleds = new Account( Id = uld.id,Territory_Lookup__c = TL[0].ID);
      leds.add(uleds);
        }
      update leds;

    }
    else
    {
    TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c
          FROM Territory_Lookup__c
          WHERE
          Country__c = :gcountry limit 1];
     system.debug('Nothing Territory Country :' + TL[0].Country__c);

     for(Account uld :  Trigger.new){
      Account  uleds = new Account( Id = uld.id,Territory_Lookup__c = TL[0].ID);
      leds.add(uleds);
        }
      update leds;
     }
}

}

catch (Exception e) {
   system.debug('Exception');

  
 }  
}
}


Thanks

Sudhir