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
Ritik DwivediRitik Dwivedi 

i have custom object State and two field inside it StatesName and StatesCode . i want to write apex code in class that i write statename and return i get state code with help of soql . please solve my query ?

ShirishaShirisha (Salesforce Developers) 
Hi Ritik,

Greetings!

This can be achieved by creating the two fields as Picklists and created the dependent picklist as Statescode.Otherwise,you can update the field using the trigger based on the StateName field before insert function.

Sample code as follows:
 
trigger AccountTrigger on Account (before insert) {
for (Account a : Trigger.new) {
if(a.StateName.contains('United States')) {
a.StateCode='USA';
}
}
}

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Ritik DwivediRitik Dwivedi
Hi Shrisha 
Actually i want write soql  in apex class for the custom object 'State ' . it contains two custom fields 'StateName ' and 'StateCode' . when i write stateCode in exnonymous window then In return i get StateName  .

for example i wrire 'UP' 
then  i will  get 'Uttar Pradesh '.
thank you