function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KRaviKRavi 

Unable to Read values from Dropdown list in Before Insert trigger

Hi Experts, 

am trying to read values from few fields (text and dropdown fields)in a before insert Account trigger.
but to my surprise, when am trying to read values from drop down fields am getting null values while inserting a record. but inserted record has valid values in drop down lists.
but while updating a record it looks fine

below is the code, 
trigger assignLocation on Account (before Insert, before Update) {
 for (Account account : Trigger.new) {
        street.add(account.ShippingStreet);
        city.add(account.ShippingCity);
        state.add(account.ShippingState);
        zipCode.add(account.ShippingPostalCode);
        country.add(account.ShippingCountry);
     } 
}
     
     System.debug('--streetstreet --: ' + street ); // am getting desired value
     System.debug('--citycity --: ' + city ); // am getting desired  value
     System.debug('--statestate --: ' + state ); // this is drop down field, in trigger it shows null while inserting a record.
      System.debug('--zipCode --: ' + zipCode ); // am getting desired  value
     System.debug('--countrycountry --: ' + country ); // this is drop down field, in trigger it shows null while inserting a record.



I have to use BEFORE INSERT/UPDATE TRIGGER, as i have to update a field before inserting.

Thank you
Best Answer chosen by KRavi
Abhishek BansalAbhishek Bansal
Hi Ravi,

I tried your same code in my org and it works fine for me.
I dont understand why it is not working for you.

Since we dont have any root cause on this issue so i have to debug your c ode in your ORG.
So if it is possible for you than please share your org credentials or you can reach me on my mail id : abhishek.bansal@metacube.com OR skype id : abhishek.bansal2790.

Regards,
Abhishek

All Answers

David ZhuDavid Zhu
I have the problem. The dropdown value only does not work with ADDRESS composite data type. I believe it is a SFDC bug.
If creating your custom picklist fields, you won't have any problem to get dropdown value in BEFORE triggers. This could be a workaround for now. 
KRaviKRavi

How do i populate those newly created Custom picklists!!!

Won't that be complicated again
please correct me if am wrong

KRaviKRavi
Here is full trigger code,

In this trigger, am just trying to read value from standard Addresss fields, check some condition and based on that updating another field.
but when am tryingto read Shipping state and Country (standard Address field dropdowns), it shows null value. but saved Account record has valid values in it.

Trigger: Before insert / update

Try to throw some light on this road block 


Here is my full code of trigger:

trigger assignLocationToAccount on Account (before Insert, before Update) {

    List<String> street  = new List<String>();
    List<String> city    = new List<String>();
    List<String> state   = new List<String>();
    List<String> zipCode = new List<String>();
    List<String> country = new List<String>();
    Map<string,Id> LocMap = new Map<string,Id>();

 for (Account account : Trigger.new) {
        street.add(account.ShippingStreet);
        city.add(account.ShippingCity);
        state.add(account.ShippingState);
        zipCode.add(account.ShippingPostalCode);
        country.add(account.ShippingCountry);
     } 
     
     System.debug('--streetstreet --: ' + street );
     System.debug('--citycity --: ' + city );
     System.debug('--statestate --: ' + state );  //**it is dstandard Address drop down field**/ unable to read value from here , it just shows null
      System.debug('--zipCode --: ' + zipCode );
     System.debug('--countrycountry --: ' + country ); //**it is dstandard Address drop down field**/ unable to read value from here , it shows null
     
    //get Map of all records from Location object with above shipping details
    Map<ID,Site__c> sitemap =new Map<ID,Site__c>(
             [SELECT ID,Name,Street__c,City__c,State__c,Zip__c,Country__c  FROM Site__c 
             WHERE Street__c IN : street AND City__c IN : city AND State__c IN : state AND                                                                                                         Zip__c IN : zipCode AND  Country__c IN : country LIMIT 10000]);
                   
     for(Site__c site : sitemap.values()){
       LocMap.put(site.Street__c+site.City__c+site.State__c+site.Zip__c+site.Country__c,site.id);
     }          
 
 for (Account acc : Trigger.new) { 
     Id locId = LocMap.get(acc.ShippingStreet+acc.ShippingCity+acc.ShippingState+acc.ShippingPostalCode+acc.ShippingCountry);         
        System.debug('--LocIDd --: ' + LocId );
     If (acc.Type == 'Generator'){  
        acc.SvmxLocation__c= locId;
      }
                   
      }          
}


any help would be highly appreciated


Thakn you
Kishore
Abhishek BansalAbhishek Bansal
Hi Ravi,

I tried your same code in my org and it works fine for me.
I dont understand why it is not working for you.

Since we dont have any root cause on this issue so i have to debug your c ode in your ORG.
So if it is possible for you than please share your org credentials or you can reach me on my mail id : abhishek.bansal@metacube.com OR skype id : abhishek.bansal2790.

Regards,
Abhishek
This was selected as the best answer
Steven Berg 10Steven Berg 10
I am experiencing the same problem but on the Lead Address field for  State and Country.
My before insert trigger shows the values as blank.
I need to write this trigger so I can assign a specific value based on the State Value. Ex, NY or Maine value is North-East.
Does anyone know how to fix this without writing a custom picklist with all the states in the US?
Thank You
ANKUR GUPTA 63ANKUR GUPTA 63
This occurs when you have enabled country state picklist in your ORG. You will get only codes in before insert triggers.