+ Start a Discussion
Siva SakthiSiva Sakthi 

how to add two list in trigger

Hi,

I have tried to add two list via trigger but i got an error like System.ListException: Before Insert or Upsert list must not have two identically equal elements.  This is my code please suggest me whats is error. and how to fix that. I want to try to send sms for two contact numbers in employee object.
trigger SMSAttendance on Emp_Attendance__c (after insert) {
    List<smagicinteract__smsmagic__c> scpList = new List<smagicinteract__smsmagic__c>();
    String tplText = null;
    for (Emp_Attendance__c empatten : Trigger.New){
        if(!empatten. Employee_Present__c){                      
           Emp_Attendance__c emp= [SELECT Contact_Emp__r.Phone,sitinstra__Contact_Emp__r.OtherPhone from Emp_Attendance__c where Id = :empatten.Id limit 1];
             
            tpltext = 'Dear Emp, this is to inform you that you are ABSENT for the office today. Regards, Department of HR, ABC Pvt Ltd.';
            system.debug(tpltext);
            smagicinteract__smsMagic__c smsObj = new smagicinteract__smsMagic__c();
            smsObj.smagicinteract__PhoneNumber__c = emp.Contact_Emp__r.Phone;
            smsObj.smagicinteract__SMSText__c = tplText;             
            smsObj.smagicinteract__senderId__c = 'ABCPL';
            smsObj.smagicinteract__external_field__c = empatten.Id + 'testmessage';
            smsObj.smagicinteract__Name__c = empatten.Employee_Name__c;
            scpList.add(smsObj);
          /   Here i have a doubt - how to send a SMS for two mobile numbers in same employee. Below code i have tried but got an error that means second time add list value  /
            smsObj.smagicinteract__PhoneNumber__c = emp.Contact_Emp__r.OtherPhone;
            smsObj.smagicinteract__SMSText__c = tplText;             
            smsObj.smagicinteract__senderId__c = 'ABCPL';
            smsObj.smagicinteract__external_field__c = empatten.Id + 'testmessage1';
            smsObj.smagicinteract__Name__c = empatten.Employee_Name__c;
            scpList.add(smsObj);
            
        }
    }
    insert scpList;
}
Thanks
Siva
Raj VakatiRaj Vakati
Modify the code like below 
 
trigger SMSAttendance on Emp_Attendance__c (after insert) {
    List<smagicinteract__smsmagic__c> scpList = new List<smagicinteract__smsmagic__c>();
    String tplText = null;
    for (Emp_Attendance__c empatten : Trigger.New){
        if(!empatten. Employee_Present__c){                      
           Emp_Attendance__c emp= [SELECT Contact_Emp__r.Phone,sitinstra__Contact_Emp__r.OtherPhone from Emp_Attendance__c where Id = :empatten.Id limit 1];
             
            tpltext = 'Dear Emp, this is to inform you that you are ABSENT for the office today. Regards, Department of HR, ABC Pvt Ltd.';
            system.debug(tpltext);
            smagicinteract__smsMagic__c smsObj = new smagicinteract__smsMagic__c();
            smsObj.smagicinteract__PhoneNumber__c = emp.Contact_Emp__r.Phone;
            smsObj.smagicinteract__SMSText__c = tplText;             
            smsObj.smagicinteract__senderId__c = 'ABCPL';
            smsObj.smagicinteract__external_field__c = empatten.Id + 'testmessage';
            smsObj.smagicinteract__Name__c = empatten.Employee_Name__c;
            scpList.add(smsObj);
          /   Here i have a doubt - how to send a SMS for two mobile numbers in same employee. Below code i have tried but got an error that means second time add list value  /
            smsObjSecond.smagicinteract__PhoneNumber__c = emp.Contact_Emp__r.OtherPhone;
            smsObjSecond.smagicinteract__SMSText__c = tplText;             
            smsObjSecond.smagicinteract__senderId__c = 'ABCPL';
            smsObjSecond.smagicinteract__external_field__c = empatten.Id + 'testmessage1';
            smsObjSecond.smagicinteract__Name__c = empatten.Employee_Name__c;
            scpList.add(smsObjSecond);
            
        }
    }
    insert scpList;
}

 
Siva SakthiSiva Sakthi
Hi Raj V,

Thanks for your reply , I have checked with your updated code but its not working. Becuse 'smsObjSecond' we have not declared right its through an error. Once i have decalred like  this smagicinteract__smsMagic__c smsObjSecond = new smagicinteract__smsMagic__c(); but not yet fixed.