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
michaelmorenomichaelmoreno 

Need Help Populating Lookup Field with Trigger

Hi guys,

 

I'm new to Salesforce so go easy on me :)

 

My problem is I'm trying to populate a Lookup Field of a Case before the Case is inserted.

 

More specifically, I'm trying to populate the Case field "Account Name".

 

I'm pulling the account name from a String, however, when I run the code below, the Account Name field remains blank.

So I'm assuming that writing to a Lookup Field is different than writing to other fields.

 

Please tell me how to make this work.

 

Thanks,

 

Michael

 

trigger MapAccountAndContact on Case (before insert, before update) {
    
    String subject;
    
    for(Case myCase : Trigger.new) {
       subject = myCase.Subject;

       Boolean fromLMS = subject.startsWith('[TICKET]');
       
       if(!fromLMS)
           continue;
       
       String[] elements = subject.split('-');
       
       myCase.Account = getAccount(elements[3].trim());
       
       myCase.Contact = getContact(elements[4].split(' '));
       
       
       myCase.Subject = mycase.Account.Name + ' - ' + myCase.Contact.FirstName 
       + ' ' + myCase.Contact.LastName + ' - ' + elements[5];
       
       
       System.debug(myCase.Account.Name);
       System.debug(myCase.Contact.FirstName + ' ' +     myCase.Contact.LastName);
       System.debug(mycase.Subject);
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
BewitchedBewitched

Hi,

 

Yes it is...When you write a code for a look up field you need to pass the 'ID' instead of Name....

For eg:- Lets suppose you are using an Account Object it has name as :- 'XYZ Company' and ID is '001M0000009h7B4' . Now if you are populating this Account name for a look up field you need to pass the ID... not the name..

 

 

 

Hope this answers the question..

 

Thanks,

Tulika

 

All Answers

BewitchedBewitched

Hi,

 

Yes it is...When you write a code for a look up field you need to pass the 'ID' instead of Name....

For eg:- Lets suppose you are using an Account Object it has name as :- 'XYZ Company' and ID is '001M0000009h7B4' . Now if you are populating this Account name for a look up field you need to pass the ID... not the name..

 

 

 

Hope this answers the question..

 

Thanks,

Tulika

 

This was selected as the best answer
michaelmorenomichaelmoreno

Thank you, your suggestion worked!

 

Here is a snippet of my revised, working code:

 

     String subjectAccountName =   getSubjectAccountName(elements[3].trim());
       
       Account resultAccount = null;
       
       try {
         resultAccount = [SELECT Name, Id from Account where Name = :subjectAccountName];
         myCase.AccountId = resultAccount.Id;
         myCase.Account = resultAccount;
       }
       catch(Exception e) {
         //unknown account
       }

 

SamuelDeRyckeSamuelDeRycke

I have some additional comments: I think you need to pay attention to a few things when writing your trigger.

 

You're right in iterating over the Trigger.New, as triggers can be run in batch, you can't be certain at how many objects the trigger will receive. But this has other implications too.

 

Firstly, not doing sql querties inside your for iteration, not just because you"ll hit governer limits, but because its better practise not too. (hense the limits).

 

Secondly:

 

resultAccount = [SELECT Name, Id from Account where Name = :subjectAccountName];
myCase.AccountId = resultAccount.Id;
myCase.Account = resultAccount;

 

If, just if ever someone might have entered duplicate data, and you have 2 accounts with the same name, that query will result an array, and not an object. Or technically they may be no result at all.  Either can create an error your code is not dealing with. Keep these things in mind when developing.