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
force shahidforce shahid 

System.ListException: Duplicate id in list Error while imorting CSV file

Hi Friends ,

I am importing a csv file using data import wizard. After Completion of import i got the below error.
System.ListException: Duplicate id in list

It shows System.ListException: Duplicate id in list: 00Qn0000006FfywEAC.

When I check the id in my objects. i found a record . I check the CSV file with this record. I found phone number field is name in some other records. That'y It shows error Duplicate Id.
Only Phone number is Same , remaining fields are different. How to over come thsi.

My code is :

public class DataImport_Class {
    public static void beforeInsert (List<Campaign_Entry__c> newrecord){
Integer count1;
    String custObjEmailDomain;
    List <Lead> inlead = new List <Lead> ();
    List <Lead> uplead = new List <Lead> ();
    List <Lead> uplead1 = new List <Lead> ();
    List <Lead> uplead2 = new List <Lead> ();
    List <Campaign> campCrt = new List <Campaign> ();
    List <Campaign> camprecds = [select name, (select Name, Status, ContactId,LeadId,Phone, MobilePhone, 
                                     CompanyOrAccount from CampaignMembers ) from Campaign];
    List <CampaignMember> campMemUpd = new List <CampaignMember>();
    List <CampaignMember> campMemcre1 = new List <CampaignMember>();
    List <CampaignMember> campMemcre2 = new List <CampaignMember>();
    List <CampaignMember> campMemcre = new List <CampaignMember>();
    List <Account> accs = [select Email_Domain__c, (select LastName,Email, otherphone, phone, Contact_Status__c,Title,
                                  MailingStreet,MailingCity,MailingState,MailingPostalCode
                                                    from Contacts) from Account];
    List <Lead> leads = [select lastname, MobilePhone, Email, Company,phone, Title ,Street,City,State,PostalCode,Status from Lead];
    List <CampaignMember> campMem = [select Name, Status, ContactId,LeadId,Phone, MobilePhone, 
                                     CompanyOrAccount from CampaignMember];
       for (Campaign_Entry__c customObjRecord: newrecord) { // Iterate over the custom object records
           if(customObjRecord.Email_Address__c!=null && customObjRecord.Email_Address__c !=''){   
                custObjEmailDomain = ((customObjRecord.Email_Address__c).split('@')[1]).split( '\\.')[0];
           }
        System.debug(customObjRecord);
        Boolean accCrtFlag = true;
if (accCrtFlag) {
            Boolean leadCrtFlag = true;
            for (Lead l: leads) {
                if (leadCrtFlag && (customObjRecord.Email_Address__c == l.email && customObjRecord.Email_Address__c !=
                        '' && customObjRecord.Email_Address__c != null) && customObjRecord.Office_Phone__c != l.phone ) // If Email ids are same
                {
                    leadCrtFlag = false;
                    Lead l1 = l;
                    
                    if (customObjRecord.Name != null && customObjRecord.Name != '')
                        l1.LastName = customObjRecord.Name;
                    if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                         l1.FirstName = customObjRecord.First_Name__c;
                    if (customObjRecord.Business_Card_Company__c != null && customObjRecord.Business_Card_Company__c !='')
                        l1.Company = customObjRecord.Business_Card_Company__c;
                    if (customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)
                        l1.MobilePhone = customObjRecord.Office_Phone__c; // Update new phone Field value into other phone Field
                    if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                        l1.Title=customObjRecord.Business_Card_Title__c;
                    if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                        l1.Street=customObjRecord.Primary_Address_Street__c;
                    if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                        l1.City=customObjRecord.Primary_Address_City__c;
                    if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                        l1.State=customObjRecord.Primary_Address_State__c;
                    if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                        l1.Country=customObjRecord.Primary_Address_Country__c;
                    if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                        l1.PostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                      Boolean campCrtFlag = true;
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.LeadId==l1.Id){
                                        campCrtFlag=false;
                                    }
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, LeadId = l1.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                            }
                        }
                    uplead.add(l1); // Update the existing record in Leads
                } else if (customObjRecord.Email_Address__c != l.email  && (customObjRecord.Office_Phone__c == l.phone &&
                        customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)) // If phone numbers are same
                {
                    leadCrtFlag = false;
                    lead l2 = l;
                    if (customObjRecord.Name != null && customObjRecord.Name != '') // Update LastName field when new Name field value is not empty .
                        l2.LastName = customObjRecord.Name;
                    if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                            l2.FirstName = customObjRecord.First_Name__c;
                    if (customObjRecord.Email_Address__c != null && customObjRecord.Email_Address__c != '')
                        l2.Email = customObjRecord.Email_Address__c;
                    if (customObjRecord.Business_Card_Company__c != null && customObjRecord.Business_Card_Company__c !='')
                        l2.Company = customObjRecord.Business_Card_Company__c;
                    if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                        l2.Title=customObjRecord.Business_Card_Title__c;
                    if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                        l2.Street=customObjRecord.Primary_Address_Street__c;
                    if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                        l2.City=customObjRecord.Primary_Address_City__c;
                    if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                        l2.State=customObjRecord.Primary_Address_State__c;
                    if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                        l2.Country=customObjRecord.Primary_Address_Country__c;
                    if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                        l2.PostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                     Boolean campCrtFlag = true;
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.LeadId==l2.Id){
                                        campCrtFlag=false;
                                }
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, LeadId = l2.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                            }
                        }
                    uplead.add(l2); // Update the existing record in Leads
                } else if ((customObjRecord.Email_Address__c == l.email && customObjRecord.Email_Address__c != '' &&
                        customObjRecord.Email_Address__c != null) && (customObjRecord.Office_Phone__c == l.phone &&
                        customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)) //  phone numbers & Email Ids are same
                {
                    leadCrtFlag = false;
                    lead l3 = l;
                    if (customObjRecord.Name != null && customObjRecord.Name != '') // Update LastName field when new Name field value is not empty .
                        l3.LastName = customObjRecord.Name;
                    if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                         l3.FirstName = customObjRecord.First_Name__c;
                    if (customObjRecord.Business_Card_Company__c != null && customObjRecord.Business_Card_Company__c !='')
                        l3.Company = customObjRecord.Business_Card_Company__c;
                    if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                        l3.Title=customObjRecord.Business_Card_Title__c;
                    if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                        l3.Street=customObjRecord.Primary_Address_Street__c;
                    if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                        l3.City=customObjRecord.Primary_Address_City__c;
                    if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                        l3.State=customObjRecord.Primary_Address_State__c;
                    if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                        l3.Country=customObjRecord.Primary_Address_Country__c;
                    if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                        l3.PostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                    
                    Boolean campCrtFlag = true;
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.LeadId==l3.Id){
                                        campCrtFlag=false;
                                    }
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, LeadId = l3.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                            }
                        }
                    uplead.add(l3); // Update the existing record in Contacts
                }
            }
            
            if (leadCrtFlag) // Email & Phone both are different
                {
                    System.debug('lead: Both are Different');
                    count1=01;
                    lead l4 = new lead();
                    l4.LastName = customObjRecord.Name;
                    l4.FirstName = customobjrecord.First_Name__c;
                    l4.MobilePhone = customObjRecord.Other_Phone__c;
                    l4.Email = customObjRecord.Email_Address__c;
                    l4.Phone = customObjRecord.Office_Phone__c;
                    l4.Company = customObjRecord.Business_Card_Company__c;
                    l4.Title=customObjRecord.Business_Card_Title__c;
                    l4.Street=customObjRecord.Primary_Address_Street__c;
                    l4.City=customObjRecord.Primary_Address_City__c;
                    l4.State=customObjRecord.Primary_Address_State__c;
                    l4.Country=customObjRecord.Primary_Address_Country__c;
                    l4.PostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                    l4.External_Lead_Id__c='Ext'+count1+customObjRecord.Office_Phone__c;
                    System.debug('Ext'+count1+customObjRecord.Office_Phone__c);
                    inlead.add(l4); // Create new Lead
                    for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                    String extname='Ext'+count1+customObjRecord.Office_Phone__c;
                                    Lead lnew=new Lead(External_Lead_Id__c=extname);
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, Lead = lnew,
                                                                           Status=customobjrecord.Member_Status__c);
                                    campMemcre.add(cm);
                                
                            }
                        }
                    count1++;
                }
            
        }
    }

        System.debug('New Contacts'+incon);
    if (inlead.size() > 0){
        Database.saveResult[] rslt=Database.insert(inlead, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
        
    if (uplead.size() > 0){
        Database.saveResult[] rslt=Database.update(uplead, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
    if (campMemUpd.size() > 0){
        Database.saveResult[] rslt=Database.update(campMemUpd, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
    
    }
}


 
Santhosh SSanthosh S
Hi shahid,

Before updating the list you need to check the list is having any duplicate record.

You can do this 2 ways.
1. Use Maps <Id,Object> instead of list and check if id is already exist before putting the object into map. During update use MapUpdate.values().
Refer : https://developer.salesforce.com/forums/?id=906F00000008yxqIAA
2. Keep one set<id> var and before adding the list check if set is not contains the value of the list record id then add it to updateList.add() list other wise don't add to list beacuse its already exist.

if your question is answered, please choose best answer. 

 
force shahidforce shahid
Hi Santosh,
Thanks for your help. But I don't have knowledge in Collections.I don;t know how to use. If you don't mine can you provide the code plz.
Santhosh SSanthosh S
Hi Shahid,

Try some thing like this.


public class DataImport_Class {
    public static void beforeInsert (List<Campaign_Entry__c> newrecord){
Integer count1;
    String custObjEmailDomain;
    Set <Id> leadIds = new Set <Id> ();
    List <Lead> inlead = new List <Lead> ();
    List <Lead> uplead = new List <Lead> ();
    List <Lead> uplead1 = new List <Lead> ();
    List <Lead> uplead2 = new List <Lead> ();
    List <Campaign> campCrt = new List <Campaign> ();
    List <Campaign> camprecds = [select name, (select Name, Status, ContactId,LeadId,Phone, MobilePhone, 
                                     CompanyOrAccount from CampaignMembers ) from Campaign];
    List <CampaignMember> campMemUpd = new List <CampaignMember>();
    List <CampaignMember> campMemcre1 = new List <CampaignMember>();
    List <CampaignMember> campMemcre2 = new List <CampaignMember>();
    List <CampaignMember> campMemcre = new List <CampaignMember>();
    List <Account> accs = [select Email_Domain__c, (select LastName,Email, otherphone, phone, Contact_Status__c,Title,
                                  MailingStreet,MailingCity,MailingState,MailingPostalCode
                                                    from Contacts) from Account];
    List <Lead> leads = [select Id,lastname, MobilePhone, Email, Company,phone, Title ,Street,City,State,PostalCode,Status from Lead];
    List <CampaignMember> campMem = [select Name, Status, ContactId,LeadId,Phone, MobilePhone, 
                                     CompanyOrAccount from CampaignMember];
       for (Campaign_Entry__c customObjRecord: newrecord) { // Iterate over the custom object records
           if(customObjRecord.Email_Address__c!=null && customObjRecord.Email_Address__c !=''){   
                custObjEmailDomain = ((customObjRecord.Email_Address__c).split('@')[1]).split( '\\.')[0];
           }
        System.debug(customObjRecord);
        Boolean accCrtFlag = true;
if (accCrtFlag) {
            Boolean leadCrtFlag = true;
            for (Lead l: leads) {
                if (leadCrtFlag && (customObjRecord.Email_Address__c == l.email && customObjRecord.Email_Address__c !=
                        '' && customObjRecord.Email_Address__c != null) && customObjRecord.Office_Phone__c != l.phone ) // If Email ids are same
                {
                    leadCrtFlag = false;
                    Lead l1 = l;
                    
                    if (customObjRecord.Name != null && customObjRecord.Name != '')
                        l1.LastName = customObjRecord.Name;
                    if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                         l1.FirstName = customObjRecord.First_Name__c;
                    if (customObjRecord.Business_Card_Company__c != null && customObjRecord.Business_Card_Company__c !='')
                        l1.Company = customObjRecord.Business_Card_Company__c;
                    if (customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)
                        l1.MobilePhone = customObjRecord.Office_Phone__c; // Update new phone Field value into other phone Field
                    if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                        l1.Title=customObjRecord.Business_Card_Title__c;
                    if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                        l1.Street=customObjRecord.Primary_Address_Street__c;
                    if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                        l1.City=customObjRecord.Primary_Address_City__c;
                    if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                        l1.State=customObjRecord.Primary_Address_State__c;
                    if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                        l1.Country=customObjRecord.Primary_Address_Country__c;
                    if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                        l1.PostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                      Boolean campCrtFlag = true;
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.LeadId==l1.Id){
                                        campCrtFlag=false;
                                    }
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, LeadId = l1.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                            }
                        }
                    if(!leadIds.contains(l1.id)){
                        leadIds.add(l1.id);
                        uplead.add(l1); // Update the existing record in Leads
                    }
                } else if (customObjRecord.Email_Address__c != l.email  && (customObjRecord.Office_Phone__c == l.phone &&
                        customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)) // If phone numbers are same
                {
                    leadCrtFlag = false;
                    lead l2 = l;
                    if (customObjRecord.Name != null && customObjRecord.Name != '') // Update LastName field when new Name field value is not empty .
                        l2.LastName = customObjRecord.Name;
                    if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                            l2.FirstName = customObjRecord.First_Name__c;
                    if (customObjRecord.Email_Address__c != null && customObjRecord.Email_Address__c != '')
                        l2.Email = customObjRecord.Email_Address__c;
                    if (customObjRecord.Business_Card_Company__c != null && customObjRecord.Business_Card_Company__c !='')
                        l2.Company = customObjRecord.Business_Card_Company__c;
                    if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                        l2.Title=customObjRecord.Business_Card_Title__c;
                    if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                        l2.Street=customObjRecord.Primary_Address_Street__c;
                    if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                        l2.City=customObjRecord.Primary_Address_City__c;
                    if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                        l2.State=customObjRecord.Primary_Address_State__c;
                    if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                        l2.Country=customObjRecord.Primary_Address_Country__c;
                    if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                        l2.PostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                     Boolean campCrtFlag = true;
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.LeadId==l2.Id){
                                        campCrtFlag=false;
                                }
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, LeadId = l2.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                            }
                        }

                    if(!leadIds.contains(l2.id)){
                        leadIds.add(l2.id);
                        uplead.add(l2); // Update the existing record in Leads
                    }
                } else if ((customObjRecord.Email_Address__c == l.email && customObjRecord.Email_Address__c != '' &&
                        customObjRecord.Email_Address__c != null) && (customObjRecord.Office_Phone__c == l.phone &&
                        customObjRecord.Office_Phone__c != '' && customObjRecord.Office_Phone__c != null)) //  phone numbers & Email Ids are same
                {
                    leadCrtFlag = false;
                    lead l3 = l;
                    if (customObjRecord.Name != null && customObjRecord.Name != '') // Update LastName field when new Name field value is not empty .
                        l3.LastName = customObjRecord.Name;
                    if (customObjRecord.First_Name__c != null && customObjRecord.First_Name__c != '')
                         l3.FirstName = customObjRecord.First_Name__c;
                    if (customObjRecord.Business_Card_Company__c != null && customObjRecord.Business_Card_Company__c !='')
                        l3.Company = customObjRecord.Business_Card_Company__c;
                    if (customObjRecord.Business_Card_Title__c != null && customObjRecord.Business_Card_Title__c !='')
                        l3.Title=customObjRecord.Business_Card_Title__c;
                    if (customObjRecord.Primary_Address_Street__c != null && customObjRecord.Primary_Address_Street__c !='')
                        l3.Street=customObjRecord.Primary_Address_Street__c;
                    if (customObjRecord.Primary_Address_City__c != null && customObjRecord.Primary_Address_City__c !='')
                        l3.City=customObjRecord.Primary_Address_City__c;
                    if (customObjRecord.Primary_Address_State__c != null && customObjRecord.Primary_Address_State__c !='')
                        l3.State=customObjRecord.Primary_Address_State__c;
                    if (customObjRecord.Primary_Address_Country__c != null && customObjRecord.Primary_Address_Country__c !='')
                        l3.Country=customObjRecord.Primary_Address_Country__c;
                    if (customObjRecord.Primary_Address_Postal_Code__c != null && customObjRecord.Primary_Address_Postal_Code__c !='')
                        l3.PostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                    
                    Boolean campCrtFlag = true;
                        for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                for(CampaignMember cm: camp.CampaignMembers){
                                    if(cm.LeadId==l3.Id){
                                        campCrtFlag=false;
                                    }
                                }
                                if(campCrtFlag){
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, LeadId = l3.Id,
                                                                           status = customObjRecord.Member_Status__c);
                                    campMemcre.add(cm);
                                }
                            }
                        }
                    
                    if(!leadIds.contains(l3.id)){
                        leadIds.add(l3.id);
                        uplead.add(l3); // Update the existing record in Contacts
                    }                    
                }
            }
            
            if (leadCrtFlag) // Email & Phone both are different
                {
                    System.debug('lead: Both are Different');
                    count1=01;
                    lead l4 = new lead();
                    l4.LastName = customObjRecord.Name;
                    l4.FirstName = customobjrecord.First_Name__c;
                    l4.MobilePhone = customObjRecord.Other_Phone__c;
                    l4.Email = customObjRecord.Email_Address__c;
                    l4.Phone = customObjRecord.Office_Phone__c;
                    l4.Company = customObjRecord.Business_Card_Company__c;
                    l4.Title=customObjRecord.Business_Card_Title__c;
                    l4.Street=customObjRecord.Primary_Address_Street__c;
                    l4.City=customObjRecord.Primary_Address_City__c;
                    l4.State=customObjRecord.Primary_Address_State__c;
                    l4.Country=customObjRecord.Primary_Address_Country__c;
                    l4.PostalCode=customObjRecord.Primary_Address_Postal_Code__c;
                    l4.External_Lead_Id__c='Ext'+count1+customObjRecord.Office_Phone__c;
                    System.debug('Ext'+count1+customObjRecord.Office_Phone__c);
                    inlead.add(l4); // Create new Lead
                    for (Campaign camp: camprecds){
                            if(camp.Name==customObjRecord.Campaign__c){
                                    String extname='Ext'+count1+customObjRecord.Office_Phone__c;
                                    Lead lnew=new Lead(External_Lead_Id__c=extname);
                                    CampaignMember cm = new CampaignMember(CampaignId = camp.Id, Lead = lnew,
                                                                           Status=customobjrecord.Member_Status__c);
                                    campMemcre.add(cm);
                                
                            }
                        }
                    count1++;
                }
            
        }
    }

        System.debug('New Contacts'+incon);
    if (inlead.size() > 0){
        Database.saveResult[] rslt=Database.insert(inlead, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
        
    if (uplead.size() > 0){
        Database.saveResult[] rslt=Database.update(uplead, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
    if (campMemUpd.size() > 0){
        Database.saveResult[] rslt=Database.update(campMemUpd, false);
        for(Database.saveResult r: rslt){
            if(r.isSuccess()){
                System.debug(r.getId());
            }
            else{
                System.debug(r.getErrors());
            }
        }
    }
    
    }
}
force shahidforce shahid
Hi Santosh,
Thank you so much.
It's Working. But there is small problem . When I import CSV file with 912 records into custom object. total 912 records are uploaded into custom object,but in lead object there is only 824 records are inserted . Remaining are missing. Why these records are issing.

I check this same CSV file in my developer org. Here in
custom object : 912 records uploaded
Lead Object : 889 records are inserted.

Why differences happend in these 2 org. What about missing records. If you have any idea , please inform me.

Thanks advance.
Santhosh SSanthosh S
Hi Shahid,

The reason may be multiple you need to analysis any error has ocurrered or any dependacy on lead. Try using data loader and see the error file to analysis the issue .

 
force shahidforce shahid
Hi Santosh,
Good Morning. K I will check this issue.
I need one more help. I want to split the email domain.
Actually i did in my code . but it works on only 0th element.
Ex : shahid@gmail.com

when email is shahid@gmail.edu.in.com, Then how to split this one .
From this Email i want gmail.edu.in value.
i don't know how to write this.
My previous spliting code is :

 if(customObjRecord.Email_Address__c!=null && customObjRecord.Email_Address__c !=''){   
                custObjEmailDomain = ((customObjRecord.Email_Address__c).split('@')[1]).split( '\\.')[0];
           }

Thanks in Advance