Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
I am trying to update the following 3 custom fields under my Member_Vendor_del__c custom object:

Vendor_ID__c

Member_Account_ID__c

Member_State__c

I have created the code below to update the Vendor_ID__c and Member_Account_ID__c

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;

  }

}

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;

 }

}
2 answers
  1. Sep 29, 2017, 7:01 PM
    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
Loading
0/9000