// Update the Vendor's ID, Member's ID, and Member's Physical State into the Member Vendor Record.
for (Member_Vendor_del__c u : trigger.new){
if (u.Vendor_ID__c == null)
u.Vendor_ID__c = u.Vendor__c;
u.Member_Account_ID__c = u.Member__c;
}
}My issue is with getting Member_State__c to populate with the Physical State(API Field Name: BillingState) from the related member account. I have createdthe SOQL query below to return the string text value on the account but I keep getting the error "Illegal assignment from List to String"// Update the Member's Physical State onto the Member Vendor Record.
for (Member_Vendor_del__c u : trigger.new)
if (u.Member_State__c == null)
u.Member_State__c = [Select BillingState
From Account
Where Account.Name = :u.Member__c];}I can add .Id to the query and the error goes away but then the trigger wants to return a Salesforce ID rather than the 2 letter abbreviation for the State.Question ⌗1: What can I do to my SOQL query to return the text value rather than an ID?Question ⌗2: Can I add the SOQL query to the first part of the trigger? Something like:trigger VendorRepUpdateTrigger on Member_Vendor_del__c (before update) {
// Update the Vendor's ID, Member's ID, and Member's Physical State into the Member Vendor Record.
for (Member_Vendor_del__c u : trigger.new){
if (u.Vendor_ID__c == null)
u.Vendor_ID__c = u.Vendor__c;
u.Member_Account_ID__c = u.Member__c;
u.Member_State__c = [Select Id, Account.BillingState
From Account
where Account_ID__c = :u.Member__c].Id;
}
}
Hey Gabe!The first error, "Illegal assignment from List to String," i sbeing thrown because a SOQL query returns a list, and you are attempting to assign the returned List of Accounts into a String variable. When you are expecting only 1 row to be returned, you can add the "LIMIT 1" modifier to the end of your query, which will then return a single Account record instead of a List:
[SELECT BillingState FROM Account WHERE Name = :u.Member__c LIMIT 1]
With that modification, you can then call out the BillingState field, just as you did with the Id field:
// if u.Member__c is the Name of the Account:
u.Member_State__c = [SELECT BillingState FROM Account WHERE Name = :u.Member__c LIMIT 1].BillingState;
// if u.Member__c is the Id of the Account:
u.Member_State__c = [SELECT BillingState FROM Account WHERE Id = :u.Member__c LIMIT 1].BillingState;
This will assign just the the String field "BillingState" into your u.Member_State__c field.
So the final trigger would be:
trigger VendorRepUpdateTrigger on Member_Vendor_del__c (before update)
{
// Update the Vendor's ID, Member's ID, and Member's Physical State into the Member Vendor Record.
for (Member_Vendor_del__c u : Trigger.new)
{
if (u.Vendor_ID__c == null)
{
u.Vendor_ID__c = u.Vendor__c;
u.Member_Account_ID__c = u.Member__c;
u.Member_State__c = [SELECT BillingState
FROM Account
WHERE Name = :u.Member__c
LIMIT 1].BillingState;
}
}
}
Let me know if this helps!
- Joe