Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1571
  • Last Modified:

Salesforce - reference field lookup syntax

Newbie Question -
 I am trying to write a trigger which creates a new task when the Contract is saved.
In the task I need to populate the Sales_Name__c field with the value from the contract field Sales_Rep_c.
I am getting a compile error indicating an invalid foreign key relationship for c.Sales_Rep_c.
Here is the line generating the error;

Sales_Name__c = [SELECT Name From User Where Id = : c.Sales_Rep_c.id];


If I look at the properties of the Contract field Sales_rep_c it is a reference(custom) with foreign key Sales_rep_r
The Contract field Sales_Rep_c is populated with the correct id value for the user name.
0
Ed_CLP
Asked:
Ed_CLP
1 Solution
 
techhealthCommented:
It should be like this:
Sales_Name__c = [SELECT Name From User Where Id = : c.Sales_Rep__r.id];
or
Sales_Name__c = [SELECT Name From User Where Id = : c.Sales_Rep_c];

The former is a way of retrieving through the relationship; the second is simply using the reference field, which stores the Id itself.  The second is prob. more efficient.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now