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
kirubakaran viswanathankirubakaran viswanathan 

How to write trigger to insert or update contact record without external id

whenever new application comes in from students, need to check whether student contact is already there or not, if yes then update the contact record with any new field value and create application related record to contact.
if no contact is present, then create new contact record and create a application related record.

We have application id as a external id, which is tagged to application related record. No external id is mapped to Contact.

Wrote some logic, but it is working for insert operation, but it is failing for update the contact record.
Can anyone suggest some workaround? Thanks.

public with sharing class ContactSLMCreateHelper {
    
    public static void createSLM(List<TY_Imports__c> Imports){     
        List<Admission__c> insertSLMList = new List<Admission__c>();
        Set<String> contactIds = new Set<String>();
        Map<String, Contact> contactsToInsert = new Map<String, Contact>();
        Map<String, Contact> contactsToUpdate = new Map<String, Contact>();
        List<Contact> con=[select Id,FirstName,LastName from Contact ];
        // Contact copied form TY import class
        for(TY_Imports__c TY : Imports) 
        {
            for(integer i=0; i < con.size();i++)
            {
                  if (con[i].FirstName ==TY.First_Name__c  && con[i].LastName==TY.Last_or_Family_Name__c) {
         
                     system.debug('updating the record');
                    Contact c = new Contact();
                       if (TY.Last_or_Family_Name__c!= null) {
                     c.LastName = TY.Last_or_Family_Name__c;
                    }     
                    else { 
                     c.LastName='No Last Name';
                         }  
                c.FirstName= TY.First_Name__c;
                c.GU_ID__c= TY.GUID__c;
                c.Ethnicity__c= TY.Ethnicity__c;
                contactsToupdate.put(TY.Applicant_Client_ID__c , c);
                }
               
         
                else{
                    system.debug('creating new record');
                    contact c = new Contact();
                    if (TY.Last_or_Family_Name__c!= null) {
                     c.LastName = TY.Last_or_Family_Name__c;
                    }     
                    else { 
                     c.LastName='No Last Name';
                         }  
                c.FirstName= TY.First_Name__c;
                c.GU_ID__c= TY.GUID__c;
                c.Ethnicity__c= TY.Ethnicity__c;
                contactsToInsert.put(TY.Applicant_Client_ID__c , c);
                    
                }
            }}
         System.debug('Testing for contact.') ;  
        if ( contactsToupdate.values()!= null )
        {
            System.debug('About to update Contact ' + contactsToupdate.values());
            update contactsToupdate.values(); 
            System.debug('AFTER update of contact Records');
        }
          if(contactsToinsert.values()!= null){
              System.debug('About to insert Contact ' + contactsToInsert.values());
             insert contactsToInsert.values(); 
              System.debug('AFTER insert of contact Records');
            }
        RecordType SLMRecordType;
        List<RecordType> SLMRecordTypes = [ select Id, Name, DeveloperName from RecordType where SObjectType = 'Admission__c' ];
        
        if ( SLMRecordTypes != null && SLMRecordTypes .size() > 0 )
        {
            SLMRecordType = SLMRecordTypes [0];        
        }
        else
        {
            SLMRecordType = new RecordType(id='01240000000UcwV');
        }
        
        for(TY_Imports__c TY : Imports) {
            if (TY.Applicant_Client_ID__c != NULL) {
                Admission__c adm = new Admission__c();
        
                adm.Applicant_Client_ID__c = TY.Applicant_Client_ID__c;
                adm.Age_at_Time_of_Enrollment__c = TY.Age_at_Time_of_Enrollment__c;
                adm.RecordTypeId = SLMRecordType.Id;
                adm.TY_Application_Term__c= TY.Application_Term__c;
                adm.TY_Admitted_Date__c= TY.TY_Admitted_Date__c;
                adm.TY_Enrolled_Date__c= TY.TY_Enrolled_Date__c;
                adm.Decision_Date__c = TY.Decision_Date__c;
                adm.Enrollment_Decision__c = TY.Enrollment_Decision__c;
                adm.Months_of_work_NEW__c= TY.Months_of_work_NEW__c;
                if (contactsToInsert.containsKey(TY.Applicant_Client_ID__c)) {
                adm.ContactLookUp__c= contactsToInsert.get(TY.Applicant_Client_ID__c).id;
                }
            insertSLMList.add(adm);
                 }   
            }
        System.debug('Testing for SML.') ;  
        if ( insertSLMList!= null )
        {
           if ( !Test.isRunningTest() )
            {
               System.debug('About to insert SLM. ' + insertSLMList);
               upsert insertSLMList Applicant_Client_ID__c ;  
               System.debug('AFTER insert of SLM Records');
            }
            else
            {
              try
              {
                upsert insertSLMList Applicant_Client_ID__c ; 
               }
              catch(Exception e)
              {
                System.debug(LoggingLevel.ERROR, '---> caught exception Upserting SLMList: ' + e.getMessage() );
               }
             }
           }
        }
    }
doravmondoravmon
What's the exception you got for update? Code looks fine so far...