0

I'm still very new to Apex.

What is the best way to return Account ID to my Object2__c? I have two Objects, Object2__c and Account object. These objects have a 1:1 relationship. What I'm trying to do is update or insert into Account object based on an external ID. Using Upsert would be the best but it doesn't solve 100% of the issues.

Long story short, my users also create accounts in the Account object before the external ID is created. by created leads into Accounts. I have to do a check on the Account object for the first matching Account Name and return the Account ID to Object2__c. Object2__c is just a stagging object

How would I go about doing this? I've been playing around with the codes below but I'm not getting anywhere fast.

list<account> myAccounts = new list<account>();
myAccounts = [Select ID, Name from Account limit 10];

Map<String, Account> businessMap = new Map<String, Account>();

for ( Account a : myAccounts ){
businessMap.put(a.ID, a.Name);
}

for ( ID aID : businessMap.keySet() ){
system.debug(businessMap.get(aID));
}
  • in what context is this code running - a trigger? and what kind of trigger (before/after insert/update) ? – cropredy Dec 16 '14 at 23:07
2

So if I understand you correctly, you:

  1. On Insert of an account, find any accounts:
    • With the same name
    • Where the External ID Field is blank
  2. For any that are found, create a new object2__c record

if correct you can start with a trigger on account after insert:

for trigger CreateStageObject on Account(After Insert){

      Account[] stageAccounts;

      Set<String> acctNames = New Set<String>();

      //Get Account Names
      for(Account a : trigger.new){
           acctNames.add(a.name);
      }

      //Cycle through accounts meeting criteria using indexed fields
     //NOTE - you may have to remove the NOT ID IN part and add it in the loop. I am not sure if that makes it non-selective or not and I cannot seem to find the query optimizer anymore..
     for(Account a : [Select ID, Name From Account Where Name IN :acctNames AND (NOT ID IN :trigger.new)]){
         if(string.isBlank(a.EXTERNALIDFIELD))
              stageAccounts.add(a);
     }

     //Now build stage objects
     Object2__c[] objTBI = New Object2__c[]{};
     for(Account a : stageAccounts) 
          objTBI.add(New Object2__c(Name='Your Name', ACCOUNTFIELD = a.id);


     database.insert(objTBI,false);

}
| improve this answer | |
  • Hi Eric, thank you for replying. This will not be based on insert. I'll have a scheduled job that execute this class at a specified time. – user7123 Dec 17 '14 at 1:28
  • So, once you find the matching Account name without External ID, it should retrieve the Account ID and update the record in the Object2__c. The field name will be "Acct_ID__c". – user7123 Dec 17 '14 at 1:44
  • @user7123 - If you update it accordingly yet. And you can convert it to a class and pass in the records instead of the trigger.new stuff – Eric Dec 17 '14 at 4:54
  • HI Eric, Can you help to troubleshoot what I have below. The error is in the last For loop. – user7123 Dec 19 '14 at 3:00
  • @user7123 - Troubleshoot what? – Eric Dec 19 '14 at 3:01

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.