2

can anybody help me here. I am not able to push a trigger into production, I am getting this error for all the test.classes. I hope someone can tell me what is wrong here. I will appreciate any help.

Error:

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateOSOTrigger: execution of AfterInsert caused by: System.ListException: Duplicate id in list: a0i40000003CpULAA0 Trigger.updateOSOTrigger: line 43, column 1: []", Failure Stack Trace: "Class.OS...

Trigger:

  trigger updateOSOTrigger on SO_Detail__c (after insert, after update, before delete) {


  Outbound_Sales_Order__c [] oso = new Outbound_Sales_Order__c[]{};  

  if(Trigger.isInsert || Trigger.isUpdate){
    for(SO_Detail__c p : Trigger.new){
        if(!String.isEmpty(p.Outbound_Sales_Order__c)){
            oso.add(new Outbound_Sales_Order__c(

              Id = p.Outbound_Sales_Order__c,
              x1_Issue__c = String.valueof(p.SOD_Issue__c), 
              x2_Issue__c = String.valueof(p.x2_Issue__c), 
              x3_Issue__c = String.valueof(p.x3_Issue__c), 
              x4_Issue__c = String.valueof(p.x4_Issue__c), 
              x5_Issue__c = String.valueof(p.x5_Issue__c), 
              x6_Issue__c = String.valueof(p.x6_Issue__c)  

            ));
      }
    }
  }

  if(Trigger.isDelete){
    for(SO_Detail__c p : Trigger.old){
        if(!String.isEmpty(p.Outbound_Sales_Order__c)){
            oso.add(new Outbound_Sales_Order__c(

              Id = p.Outbound_Sales_Order__c,

              x1_Issue__c = '', 
              x2_Issue__c = '', 
              x3_Issue__c = '', 
              x4_Issue__c = '', 
              x5_Issue__c = '', 
              x6_Issue__c = ''  

            ));
      }
    }
  }

 update oso;  
 }

Test Class

     @isTest
  public class test_updateOSOTrigger {
  static testMethod void updateOSOTrigger_Test() {
    Profile pf = [Select Id from Profile where Name = 'System Administrator'];
    User u55 = new User();
    u55.FirstName = 'Test';
    u55.LastName = 'User';
    u55.Email = 'testuser@test123456789.com';
    u55.CompanyName = 'test.com';
    u55.Title = 'Test User';
    u55.Username = 'testuser@test123456789.com';
    u55.Alias = 'testuser';
    u55.CommunityNickname = 'Test User';
    u55.TimeZoneSidKey = 'America/Mexico_City';
    u55.LocaleSidKey = 'en_US';
    u55.EmailEncodingKey = 'ISO-8859-1';
    u55.ProfileId = pf.Id;
    u55.LanguageLocaleKey = 'en_US';
   insert u55;

    system.runAs(u55){

  Test.startTest();

 Account account = new Account(Name = 'Test Account1');
   insert account;

  NRProducts__c product2 = new NRProducts__c(Account__c = account.Id, Name = '105823437');
   insert product2;


   Entity__c ent = new Entity__c(name='CarlosStore') ;
   insert ent;  


    Outbound_Sales_Order__c ososo = new Outbound_Sales_Order__c(Entity__c= ent.id ,X1_Issue__c = '2', X2_Issue__c = '2', X3_Issue__c = '2', X4_Issue__c = '2', X5_Issue__c = '2',X6_Issue__c = '2');

  insert ososo;
  update ososo;


    SO_Detail__c detail = new SO_Detail__c(Outbound_Sales_Order__c = ososo.id, NRProducts__c = product2 .id);
   insert detail;
   update detail;
   delete detail;



  System.assertequals( detail.Outbound_Sales_Order__c , ososo.id);
  System.assertequals( '2', ososo.X1_Issue__c);
  System.assertequals( '2' , ososo.X2_Issue__c);
  System.assertequals('2' , ososo.X3_Issue__c);
  System.assertequals( '2' ,  ososo.X4_Issue__c);
  System.assertequals( '2' ,  ososo.X5_Issue__c);
  System.assertequals( '2' , ososo.X6_Issue__c);

  Test.stopTest();


    }
    }
    }   

enter image description here

4
  • what does (your simplest) testmethod look like?
    – cropredy
    Jul 16, 2014 at 21:47
  • crop1645, i updated my question with the test class. but with o without the testClass I keep getting the error.
    – Carlos
    Jul 16, 2014 at 22:02
  • do you mind me asking if that duplicate salesforce id already exists somewhere? Jul 16, 2014 at 22:21
  • They don't exist, all the test.classes affected create new records on the fly. That's why this is so weird. But I am new in apex so maybe i am doing something wrong.
    – Carlos
    Jul 16, 2014 at 22:26

1 Answer 1

1

Your trigger looks quite dangerous in that if ever two or more SO_Detail__c objects that are children of the same Outbound_Sales_Order__c parent are inserted or updated or deleted at the same time then the duplicate ID error will occur. (You may not expect that this will happen right now, but some upload or bulk update in the future may well cause that condition and bring proceedings to a halt.)

A way to avoid the error is to use a map keyed by the Outbound_Sales_Order__c ID and make some decision about which child object "wins". Making this change may work-around your test failures (which in some ways is a bad thing because why the failure is happening when the test only uses one SO_Detail__c is certainly unclear to me) and will make the normal production use of the trigger more reliable.

PS

This is how the map would be used to make sure there are never duplicate IDs when the update is done:

Map<Id, Outbound_Sales_Order__c> osos = new Map<Id, Outbound_Sales_Order__c>();

...

    Outbound_Sales_Order__c oso = osos.get(p.Outbound_Sales_Order__c);
    if (oso == null) {
        oso = new Outbound_Sales_Order__c(Id = p.Outbound_Sales_Order__c);
        osos.put(p.Outbound_Sales_Order__c, oso);
    }
    oso.x1_Issue__c = String.valueof(p.SOD_Issue__c);
    ...
...

update osos.values();
5
  • Keith, you are the savior, i spent all day making this trigger and it worked in sandbox like i wanted. So according to you, in this trigger i need to get the Outbound_Sales_Order__c ID, create a map and then use it in trigger.new. Is this right? Thank you!
    – Carlos
    Jul 16, 2014 at 22:46
  • @Carlos I've added to the answer the change I'm suggesting.
    – Keith C
    Jul 16, 2014 at 23:00
  • Keith, you don't know how much appreciate your help. Thank you man!
    – Carlos
    Jul 16, 2014 at 23:10
  • Keith, sorry to bother you again, I am having problems adding your code into the existing code
    – Carlos
    Jul 16, 2014 at 23:31
  • do not worry I got it...
    – Carlos
    Jul 16, 2014 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.