1

We are one custom object"Data feed" where user enter their detail.Then using batch apex we are inserting these record as a contact. First I need to check whether the contact is already exist or not. If exist then I need to update the contact else need to create new contact. I can insert the contact but i got stucked when checking for existing contact. Here is the code what i have tried, using this code i can insert the contact but can't able to update the existing contact. Kindly advice me how to achieve this.

  global class ScheduleContact implements Database.Batchable<SObject> {
global Database.queryLocator start(Database.BatchableContext ctx) {

    String query = 'Select Name,Account_ID__c,Primary_Account_ID__c, Deleted__c, First_Name__c, Internal_User_ID__c,Language_Preference__c,Language_Preference_Code__c,Phone__c,Primary_Contact__c,Speaks_English__c from Data_Feeds__c';


    return Database.getQueryLocator(query);

}
global void execute(Database.BatchableContext BC, List<sObject> scope) {

    List<Data_Feeds__c> feeds = (List<Data_Feeds__c>) scope;

    Set<String> lastNames = new Set<String>();
    for (Data_Feeds__c f : feeds) {
        lastNames.add(f.Name);
    }

    Map<String, Contact> contacts = new Map<String, Contact>();
    for (Contact c : [select Id, LastName from Contact where LastName in :lastNames])
     {
        contacts.put(c.LastName, c);
    }
    for (Data_Feeds__c f : feeds) {
        Contact c;
       // Contact c = new Contact();
        if (contacts.containsKey(f.Name)) {
            c = contacts.get(f.Name);
        } else {
            c = new Contact(LastName = f.Name);
            contacts.put(f.Name, c);
        }
        c.FirstName = f.First_Name__c;
        c.AccountId = f.Primary_Account_ID__c;
        c.AccContRole__c = f.Account_ID__c;
    }
    upsert contacts.values();
/********************************Insert Account Contact Roles************************************/
    List<Contact> AccConRole = new List<Contact>();
    Set<ID> AccID = new Set<ID>(); 

    for (Contact con : contacts.values())
    {
      AccID.add(con.AccContRole__c);
     }

    Map<ID,AccountContactRole> controle = new Map<ID,  AccountContactRole>();
    for (AccountContactRole ACR :[ SELECT Id,ContactId, Role From  AccountContactRole WHERE ContactId in :AccID])
    {
        controle.put(ACR.ContactId, ACR);
    }
    for (Contact con : contacts.values())
    {
        AccountContactRole ACR;
            if(controle.containsKey(con.AccContRole__c))
            {
                ACR = controle.get(con.AccContRole__c);
            }
            else 
            {
                ACR = new AccountContactRole( ContactId = con.AccContRole__c);
                controle.put(con.AccContRole__c, ACR);
            }
            ACR.ContactId = con.AccContRole__c;
            ACR.AccountId  = con.AccountId;

    }
    upsert controle.Values();

global void finish(Database.BatchableContext BC) {

}
}
1
  • Your match to existing Contacts is based solely on the Contact.LastName?
    – Keith C
    May 19 '14 at 12:58
3

Assuming it is just the Contact.LastName that identifies a Contact and that there are no duplicates, this (untested) code will insert or update (by using upsert) as necessary:

global void execute(Database.BatchableContext BC, List<sObject> scope) {

    List<Data_Feeds__c> feeds = (List<Data_Feeds__c>) scope;

    Set<String> lastNames = new Set<String>();
    for (Data_Feeds__c f : feeds) {
        lastNames.add(f.Name);
    }

    Map<String, Contact> contacts = new Map<String, Contact>();
    for (Contact c : [
            select Id, LastName
            from Contact
            where LastName in :lastNames
            ]) {
        contacts.put(c.LastName, c);
    }
    for (Data_Feeds__c f : feeds) {
        Contact c;
        if (contacts.containsKey(f.Name)) {
            c = contacts.get(f.Name);
        } else {
            c = new Contact(LastName = f.Name);
            contacts.put(f.Name, c);
        }
        c.FirstName = f.First_Name__c;
        c.AccountId = f.Primary_Account_ID__c;
        c.AccContRole__c = f.Account_ID__c;
    }
    upsert contacts.values();
}
2
  • Thank you very much, its working now. In this i have added the same insert and update function for Contact role.but it showing some error:First error: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ContactId]: [ContactId]. I have edited my code.
    – user7292
    May 20 '14 at 8:51
  • @user7292 You might get that error is a Data_Feeds__c .Account_ID__c was ever null. Also code like WHERE ContactId in :AccID looks wrong: are you sure you are using the correct IDs everywhere?
    – Keith C
    May 20 '14 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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