1

Newbie here, just starting to learn how to code via trailhead and anything I can find online, while also trying to solve org problems in the real world of salesforce CRM, so am hoping someone can help me find or provide examples online to get me on the right track!

Scenario:

  • I have a custom object = Sessions
  • I need to relate Sessions to the standard object, Accounts, so I have created an Account Lookup text field on the Sessions object
  • Sessions entries are submitted via webform, so I am using a custom field to capture the Account name = Account Submitted
  • The idea is that the Account Submitted is what the user submits and believes to be correct, and what I want to do is have a trigger or process that looks up the Account Submitted, determines if there is an exact match to an Account Name in the system, and if so, update the Account Name lookup field on the custom Sessions object to create the association between standard object parent and custom object child
  • the leftovers will be managed manually as this is not a heavily used form, several per day at most.

I was hoping to use flow or process builder, but am stumped, and haven't coded my own trigger outside of the trailhead examples, which aren't helping me figure this out.

Any and all pointers are appreciated as I'm just getting started in the world of process and triggers, but this is a current problem I need to solve.

If you've found similar answers on stackexchange, please point me to them, as the words I've chosen to search aren't turning up the results I was hoping for!

1 Answer 1

1

Lots to learn here and much of it is fundamentals. Here is a sample that would do what you want it to. If you have specific questions feel free to ask:

trigger findAccount on Sessions__c(before Insert){
    Map<String,Account[]> nameToAccounts = New Map<String,Account[]>();

    for(Sessions__c s : trigger.new){
        if(s.AccountName__c != null)
            nameToAccounts.put(s.AccountName__c,New Account[]{});
    }

    for(Account a : [Select ID, Name From Account Where Name In :nameToAccounts.keyset()]){
        nameToAccounts.get(a.Name).add(a);
    }

    for(Sessions__c s : trigger.new){
        if(s.AccountName__c != null && nameToAccounts.get(s.AccountName__c).size() == 1)
            s.Account__c = nameToAccounts.get(s.AccountName__c)[0].id;
    }

}

Basically:

  1. Populate Set (Map Key) with entered Names
  2. Populate map with accounts matching name
  3. run through sessions and if only one match populate lookup

Doing it on before Insert as the Session record can be modified directly. Lots more to say. Have you visited Trailhead

1
  • Thanks Eric, I thought I could skip ahead and get straight to the solutoin, but am back tracking and starting fresh with the trailhead and online resources to better understand from the basics - and then will give your suggested approach a try to see how I do!
    – SZCan
    May 9, 2016 at 2:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .