0

I have a static Account and a Static Contact which works other than the fact I don't have a fix for if exist skip upsert.

I will be placing this code inside another but I'm having a hard time working my head around how to use the List from a custom object called Interaction__c within the other class in order to get the Account and Contact ID 's to replace the static ID's

Sorry, I'm new to Apex. Let me explain further about the object

Interactions__c:

On Data Loader all records are uploaded to this object then these objects are mapped to all the other Objects including Standard Objects. The Account and Contact info Including the ID's get added for that Contact on each interaction. The other cls uses the List to process the records. If it helps here's a link for that cl call INT_InteractionProcessor.cls

Also, I do get an error when I try to add the Interaction code in my separate cls.

Line          Column             Error Description
───────────── ───────────────────────
6             43                Variable does not exist: interactionsToProcess

Apex:

public class AffiliationAgent {


 public void processInteractions(List<Interaction__c> newInteractions) {

    for (Interaction__c interaction : interactionsToProcess)
     {

                     if (!String.isEmpty(interaction.Contact__c)) 
                     {

                            //Account acc = new Account(id=interaction.Agent_Key__c);
                            Account acc = new Account(id='001m000000qsO9tAAE');
                            ID accID = acc.ID;

                            Contact newCont = new Contact(Id = interaction.Contact__c);
                           // Contact newCont = new Contact(Id = '003m000001AGByaAAH');
                            // the sObject will be 
                            // populated with an ID.
                            // Get this ID.
                            ID conID = newCont.ID;

                            // Add a Affiliation to this Contact.
                            hed__Affiliation__c Affil = new hed__Affiliation__c(interaction.Agent_Key__c,
                                hed__Status__c='Current',
                                hed__Role__c='Applicant',
                                hed__Account__c= accID,
                                hed__Primary__c=True,
                                hed__Contact__c=conID);
                            upsert Affil;
                        }
    }

}

}

====================================================

I think I'm making it too complicated. the following works. So if I could get a List or map to select the current Interaction record that is being processed. I could then, I think, simply replace the static ID with interaction.Contact__c and interaction.Agent_Key__c

public class AffiliationAgent {

public static void AffiliationAgent() {


  //Account acc = new Account(id=interaction.Agent_Key__c);
  Account acc = new Account(id='001m000000qsO9tAAE');
  ID accID = acc.ID;
  //list<Account> allAgent = [select id, Name from Account WHERE RecordType.Name = 'Agent']; 
  //map<id,Account> thisAgentAccount = new map<id,Account>([select id, Name from Account WHERE RecordType.Name = 'Agent']);


//List<Contact> contactsToUpdate = new List<Contact>();
//Contact newCont = new Contact(Id = interaction.Contact__c);
Contact newCont = new Contact(Id = '003m000001AGByaAAH');
// the sObject will be 
// populated with an ID.
// Get this ID.
ID conID = newCont.ID;

// Add a Affiliation to this Contact.
hed__Affiliation__c Affil = new hed__Affiliation__c(
    hed__Status__c='Current',
    hed__Role__c='Applicant',
    hed__Account__c= accID,
    hed__Primary__c=True,
    hed__Contact__c=conID);
upsert Affil;
}

}

====================================

The following works. Now I just need to figure out how to check if Affiliation already exist then to skip the affiliation insert

    //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;
}
  • Are you getting any error or can you check expected result in debug log that what are you getting, upsert statement should be outside the for loop. – cloudZigZag Apr 4 at 4:15
  • 1
    Given an Interaction__c - how do you know what account and contact to relate the hed__Affiliation__c? You'll need to use edit and explain a bit more – cropredy Apr 4 at 4:45
  • I got it to work, now I need to figure out how to somehow check if the affiliation already exists to skip the insert. – Robert Wynter Apr 4 at 18:24
  • I've got this checking if exists using static Account and Contact ID's but as soon as I replace with variables the SOQL fails String checkConID =interaction.Contact__c; String checkAccID =interaction.Agent_Key__c; //List<hed__Affiliation__c> AllAccounts= new List<hed__Affiliation__c>([select id, name from hed__Affiliation__c WHERE hed__Account__c='001m000000qsO9tAAE' AND hed__Contact__c= '003m000001AEeJhAAL']); List<hed__Affiliation__c> AllAccounts= Database.query('select id, name from hed__Affiliation__c WHERE hed__Account__c=' + checkAccID +' AND hed__Contact__c= '+ checkConID); – Robert Wynter Apr 4 at 21:44
0

solved. I forgot the colon(:) in the bind variable.

/*

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;

}
  • my only concern now is if it's Bulkified compliant. When it's in production We will be mass uploading over 100,000 records – Robert Wynter Apr 5 at 12:41

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.