+ Start a Discussion
StaciStaci 

lookup field value from one object to another

I'm creating a new Change(our custom object) record from a case.  When you click the Change Management button to do so, I have apex that runs to pull over the account name, contact name, dealer, product and support advocate information from the case.  The Account field on the Case is a lookup, the Account field on the Change is a lookup, but for some reason I can't get it to sync with each other.  This works fine, no errors, but then the Account field on the Change record doesn't get filled in, I still have to use the lookup.  I just want the account name from the case to auto fill in.  Is that possible?

Here's part of my code:

public class SampleCaseButtonController {

    Case currCase;

    public SampleCaseButtonController(ApexPages.StandardController controller) {
        currCase = [Select Account_Name__c, ContactId, Dealer__c, Product__c, Support_Advocate__c From Case Where Id = :controller.getId()];
    }
    
    public PageReference changeCase(){
    
        // Create a new Change Record
        Change__c newChange = new Change__c();
        
                
              
        // Fill in the values for the new record
      
        
        newChange.Account_Name_text__c  = currCase.Account_Name__c;

 

SwarnasankhaSwarnasankha

Hi Staci,

 

I am not sure how you are populating the Account Name in the Account_Name__c field on the Case object. When you are querying the case record, add the following 2 fields: AccountId, Account.Name

currCase = [Select Id, AccountId, Account.Name, Account_Name__c, ContactId, Dealer__c, Product__c, Support_Advocate__c From Case Where Id = :controller.getId()];

 While creating the record for the Change__c entity, map the fields as follows:

  • Account Lookup field on Change__c = currCase.AccountId
  • Account Name String field on Change__c = currCase.Account.Name

I hope this helps. In case you have any queries or need any clarification, feel free to ask.