+ Start a Discussion
Robert WynterRobert Wynter 

check if Affiliation record already exists

This could inserts  a related affiliation record to contact on upload. I need to add some kind of if statement to have it skip if the Affiliation already exists.
 
//Updated by Robert Wynter for Agent Affiliation
        private hed__Affiliation__c createAgentUpsertAffilFromInteraction(Interaction__c interaction) {

        hed__Affiliation__c newAffil1 = new hed__Affiliation__c();
  
        intMappingService.applyDataToSObject(interaction, newAffil1);
  
         newAffil1.Upsert_Agent_Key__c = interaction.Contact__c + interaction.Agent_Key__c;
        
        newAffil1.hed__Status__c='Current';
        newAffil1.hed__Role__c='Applicant';
        newAffil1.hed__Account__c= interaction.Agent_Key__c;
        newAffil1.hed__Primary__c=True;
        newAffil1.hed__Contact__c=interaction.Contact__c;            
        upsert newAffil1;
       
        system.debug(newAffil1.hed__Account__c= accID);
        system.debug(newAffil1.hed__Contact__c=conID);

     
        return newAffil1;
    }

 
Robert WynterRobert Wynter
I needed to tweak the INT_InteractionProcessor.cls to include adding an addition Affiliation to the Contact when the record includes an Organization Agent. The code below works for the first record upload. but because the Contact would be on the flat file upload multiple times. It fails on the second upsert.  I need a way to have it check if the Affiliation does not exist then add else skip.
Robert WynterRobert Wynter
figure it out. I forgot the Bind Variable colon(:) in my SOQL query. my only concern now is if it's Bulkified compliant. When it's in production We will be mass uploading over 100,000 records.
/*
We want to inject Apex directly into the SOQL query itself!
What we want to do is create a bind variable. A “bind variable” is simply the term for an Apex variable used inside a SOQL query.
Salesforce knows you’re using a bind variable when you precede your Apex variable with a colon (:)
1. we set the string variables for Contact and Agent Account ID's
2. Then, in our SOQL query, we used a bind variable to find every Affiliation Account in our database that has the same Contact with Agent to see if it exists!
3. if it does NOT exists, we insert the agent Affiliation
*/
        private hed__Affiliation__c createAgentUpsertAffilFromInteraction(Interaction__c interaction) {
   
       
		//Set String Variables
        String checkConID =interaction.Contact__c;
        String checkAccID =interaction.Agent_Key__c;

        //Use SOQL query with Bind Variables to check for Existing Agent Account on Contact
        List<hed__Affiliation__c> AllAccounts= [select id, name 
                                FROM hed__Affiliation__c  
                                WHERE hed__Account__c= :checkAccID AND hed__Contact__c= :checkConID]; 
        
		//Set a list Affiliation for upsert

         hed__Affiliation__c newAffil1 = new hed__Affiliation__c();
         intMappingService.applyDataToSObject(interaction, newAffil1); 
      
        if(AllAccounts.size() ==  0)
        { 
     

         newAffil1.Upsert_Agent_Key__c = interaction.Contact__c + interaction.Agent_Key__c;
        newAffil1.hed__Status__c='Current';
        newAffil1.hed__Role__c='Applicant';
        newAffil1.hed__Account__c=  interaction.Agent_Key__c;
        newAffil1.hed__Primary__c=True;
        newAffil1.hed__Contact__c=interaction.Contact__c;   

        system.debug(AllAccounts.size()); 
        system.debug(newAffil1.hed__Account__c); 
        system.debug(newAffil1.hed__Contact__c);   

        upsert newAffil1;
       }//EOR_IF  
       
     
        return newAffil1;
   
    }