1

I have 3 objects Imports__c, Admin__c and Contact with Admin__c has lookup to contact. The Imports__c has an External Id field called Unique__c.

My requirement is upon the insert, update of Imports__c records I'm trying to create a Admin__c record with the same external id on Imports__c.

Now my issue is how can I pull the related contact record and update if it already exists if not then I need to create and also how to populate the Contactid on admin__c.

Here is the trigger/class where I was able to create the admin__c record using upsert.

public with sharing class ContactadminCreateHelper {

public static void createadmin(List<Imports__c> Imports){     
    List<Admin__c> insertadminList = new List<Admin__c>();
    List<Contact> insertcontactList = new List<Contact>();
    Set<String> contactIds = new Set<String>();

    for(Imports__c AY : Imports) {
        if (AY.unique__c != NULL) {
            Contact c = new Contact();
           if (AY.Last_or_Family_Name__c!= null) {
                 c.LastName = AY.Last_or_Family_Name__c;
            } else { 
                c.LastName='No Last Name';
            }  
            c.GU_ID__c= AY.GUID__c;
            c.Ethnicity__c= AY.Ethnicity__c;
            insertcontactList.add(c);
             }   
        }
    System.debug('Testing for contact.') ;  
    if ( insertcontactList != null )
    {
        System.debug('About to insert Contact ' + insertcontactList);
        insert insertcontactList ;
        System.debug('AFTER insert of contact Records');
    }

    RecordType adminRecordType;
    List<RecordType> adminRecordTypes = [ select Id, Name, DeveloperName from RecordType where SObjectType = 'Admin__c' ];

    if ( adminRecordTypes != null && adminRecordTypes .size() > 0 )
    {
        adminRecordType = adminRecordTypes [0];        
    }
    else
    {
        adminRecordType = new RecordType(id='01240000000UcwV');
    }

    for(Imports__c AY : Imports) {
        if (AY.unique != NULL) {
            Admin__c adm = new Admin__c();

            adm.unique__c = AY.unique__c;
            adm.Age_at_Time_of_Enrollment__c = AY.Age_at_Time_of_Enrollment__c;
            adm.RecordTypeId = SLMRecordType.Id;
            adm.AY_Application_Term__c= AY.Application_Term__c;
            adm.AY_Admitted_Date__c= AY.AY_Admitted_Date__c;
            adm.AY_Enrolled_Date__c= AY.AY_Enrolled_Date__c;
            adm.Decision_Date__c = AY.Decision_Date__c;
            adm.Enrollment_Decision__c = AY.Enrollment_Decision__c;
            adm.Months_of_work_NEW__c= AY.Months_of_work_NEW__c;
            //adm.ContactName__c=c.Id; this is what i have to do
            insertadmList.add(adm );
             }   
        }
    System.debug('Testing for SML.') ;  
    if ( insertSLMList!= null )
    {
        System.debug('About to insert adm. ' + insertadmList);
        upsert insertadmList unique__c ;
        System.debug('AFTER insert of adm Records');
    }

     }
  }
2

Here is a version of the code. The trick is the use of Map<String, Contact> contactsToInsert to hold the relationship between your unique ID and the contact being inserted.

Then, on the second loop, you can reuse this map to store the values in your code.

PS: I replaced your == null with String.IsEmpty() as that also checks for empty strings.

PPS: Harcoding a RecordTypeID is a bad practice. but I can't figure out what you have the new statement there.... so I left it alone

** NOTE 2/18/16 Per Chat with OP, I adjusted my code to allow existing contacts to be updated **

public with sharing class ContactadminCreateHelper {

public static void createadmin(List<Imports__c> Imports){     
  List<Admin__c> insertadminList = new List<Admin__c>();
  Set<String> contactIds = new Set<String>();

  Map<String, Contact> contactsToUpsert = new Map<String, Contact>();

  for(Imports__c AY : Imports) {
      if (! String.isEmpty(AY.unique__c)) {
          Contact c = new Contact();

          if (! String.isEmpty(AY.ContactName__c)) {
            c.Id = AY.ContactName__c;
          }

          if (AY.Last_or_Family_Name__c!= null) {
               c.LastName = AY.Last_or_Family_Name__c;
          } else { 
              c.LastName='No Last Name';
          }  
          c.GU_ID__c= AY.GUID__c;
          c.Ethnicity__c= AY.Ethnicity__c;

          contactsToUpsert.put(AY.Unique__c, c);
           }   
      }
  System.debug('Testing for contact.') ;  

  System.debug('About to insert Contact ' + contactsToUpsert.values());
  upsert contactsToUpsert.values();
  system.debug('AFTER insert of contact Records');


  RecordType adminRecordType;
  List<RecordType> adminRecordTypes = [ select Id, Name, DeveloperName from RecordType where SObjectType = 'Admin__c' ];

  if ( adminRecordTypes != null && adminRecordTypes .size() > 0 )
  {
      adminRecordType = adminRecordTypes [0];        
  }
  else
  {
      adminRecordType = new RecordType(id='01240000000UcwV');
  }

  for(Imports__c AY : Imports) {
      if (! String.isEmpty(AY.unique__c)) {
          Admin__c adm = new Admin__c();

          adm.unique__c = AY.unique__c;
          adm.Age_at_Time_of_Enrollment__c = AY.Age_at_Time_of_Enrollment__c;
          adm.RecordTypeId = SLMRecordType.Id;
          adm.AY_Application_Term__c= AY.Application_Term__c;
          adm.AY_Admitted_Date__c= AY.AY_Admitted_Date__c;
          adm.AY_Enrolled_Date__c= AY.AY_Enrolled_Date__c;
          adm.Decision_Date__c = AY.Decision_Date__c;
          adm.Enrollment_Decision__c = AY.Enrollment_Decision__c;
          adm.Months_of_work_NEW__c= AY.Months_of_work_NEW__c;
          if (contactsToUpsert.containsKey(AY.Unique__c)) {
            adm.ContactName__c= contactsToUpsert.get(AY.Unique__c).id;
          }

           insertadmList.add(adm );
           }   
      }
  System.debug('Testing for SML.') ;  
  if ( insertSLMList!= null )
  {
      System.debug('About to insert adm. ' + insertadmList);
      upsert insertadmList unique__c ;
      System.debug('AFTER insert of adm Records');
  }

   }
}
|improve this answer|||||
  • If you have some sort of "ExternalID" in your contact object, we can make this code even better. Let me know if that's the case and I can rearrange the code. – Sebastian Kessel Feb 17 '16 at 20:36
  • Thanks Sebastian : I was able to populate the contactid but the issue is there is no external id on contact which I can use from imports__c – Salesforce Learner Feb 17 '16 at 20:57
  • This code should then, hopefully, help anyway. Please don't forget to upvote (and mark as accepted) if this helped you, so others can benefit. – Sebastian Kessel Feb 17 '16 at 22:06
  • Sebastian: Since I dont have the external id on contact. Can I use a combination of Fistname+Lastname+Email to update the records if doesnt exist should be able to insert it? – Salesforce Learner Feb 18 '16 at 14:41
  • After the code I sent, what's missing? – Sebastian Kessel Feb 18 '16 at 15:20

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.