You need to sign in to do that
Don't have an account?
Accessing sObject Fields Through Relationships
I am trying to create a new opportunity object and populate some fields from related objects. I am having trouble getting the value of a custom field on the Account Object. Example;
for(Contract c:Trigger.new){
Opportunity newopp = new Opportuity(
Sales_Rep__c = c.Sales_Mgr__c
// Where Sales_Mgr__c resides in the Account object
New Opportunity.accountId is populated from contract.accountID and should provide a reference point to get the Sales_Mgr__c from Account object.
I have tried [Select Sales_Mgr__c From Account where accountId = : newopp.accountId Limit 1] without success
Here is what finally worked to get the user name from User via the Account (when only having a Contract as the reference);
Sales_rep__c = [Select sales_mgr__r.Name From Account Where id = :c.accountId ].sales_mgr__r.Name
Thanks to all for the suggestions
All Answers
Data from related records is not populated in Trigger.new. You will need to perform a query to retrieve it.
It looks like you attempted that but without success. Can you describe what happens when you try that?
Thanks for your reply. Here's what I have tried so far;
Sales_Rep__c = [select Sales_Mgr__c from account where id = :c.accountId].Name, // this compiles, but returns a blank value
Sales_Rep__c = [select Sales_Mgr__c from account where id = :c.accountId] // This throws a compile error: Invalid initial expression type for field Opportunity.Sales_Rep__c, expecting: String
Sales_Rep__c = [select Sales_Mgr__c, NAME from account where id = :c.accountId].Name, // this compiles, but returns a blank value
You need to select the name field in order for it to be populated.
Getting closer... Adding , NAME as you suggested is returning the account name, not the sales_mgr__c name. If I try this it returns the userId for the Sales_Mgr__c;
Sales_Rep__c = [select Sales_Mgr__c, NAME from account where id = :c.accountId].Sales_Mgr__c
Sales_Mgr_c__c is a field type custom on the Account object with relationship Sales_Mgr__r that references the User object.
Thanks for your help
Opportunity.Sales_Rep__c = [SELECT Sales_Mgr__r. = [select Sales_Mgr__c from account where id = :c.accountId].Sales_Mgr__c
Assuming Sales_Rep__c and Sales_Mgr__c are both lookups to the user object, that should work.
However, if you're doing that query in a for loop, you're setting yourself up to hit a governor limit exception.
You should not be doing queries in a for-loop and should isntead do one query, move the data into data structures that make sense, and access the data within the for-loop. Let me know if you'd like an example of that.
Sales_Rep__c is type picklist(custom) and does not reference the User object. I am trying to populate it with a value from Sales_Mgr__C which is type Reference (refers to User object).
Thanks again
So I assume Sales_Rep__c is a picklist that contains the name (in format FirstName + ' ' + LastName) of the sales rep? and you want to populate that picklist automatically with the first and last name of the Sales_Mgr__c that you have chosen on the same record?
Sorry if I'm still not getting the use case. This is a tricky one =)
Yes - you've described it very well. I'm auto-creating an opportunity from a contract. I use the accountID from contract to reference the account. Now I am trying to populate a custom picklist (Sales_rep__c) on the opportunity with a user First and Last name from the acount.Sales_Mgr__c which is a reference to the user object.
Thanks for your patience on this one!
Here is what finally worked to get the user name from User via the Account (when only having a Contract as the reference);
Sales_rep__c = [Select sales_mgr__r.Name From Account Where id = :c.accountId ].sales_mgr__r.Name
Thanks to all for the suggestions