• michaelmoreno
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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);
    }
}

 

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);
    }
}