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
nikkeynikkey 

Fetch the records for the picklist value as 10.

 

Hi Developers , 

 

Can any one help me on this requirement .

 

i have a requirement , in an Account object i have 10000 records and picklist field with the values as 10,20,30,40 ,50.i would like to fetch the records for the picklist value as 10.how do u query on this?

 

Thanks In Advance

Best Answer chosen by Admin (Salesforce Developers) 
Subhash GarhwalSubhash Garhwal

Hi,

 

Hitesh is right, but your trigger will update all account records, so i think you also need to check one more condition in your for loop

 

for(Account acc : Trigger.new) {

 

if(acc.PicklistField__c == '10') {

 

 //Your logic

}

}

 

In that case it update records if PicklistField__c field value is 10

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you

 

 

All Answers

hitesh90hitesh90

Hi nik,

 

Your SOQL query on account object is like below.

 

SOQL query:

select id from account where YourPicklist__c = '10'

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

Subhash GarhwalSubhash Garhwal

Hi Nikkey,

 

You can write your query like

 

List<Account> accounts = [Select Id, Name, picklist field From Account Where picklist field = '10' ];

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you

nikkeynikkey

Hi Subhash and Hitesh ,

 

thanks for ur rply .

 

i have written the code it got saved but whn i create a record it throws an ERROR as: 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger fetchpicklistvalue caused an unexpected exception, contact your administrator: fetchpicklistvalue: execution of BeforeInsert caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.fetchpicklistvalue: line 13, column 1.

 

 trigger fetchpicklistvalue on account(before insert,before update)

{
//if(trigger.isinsert || trigger.isupdate)
list<account> alist = new list<account>();
list<account> acc = [select id,name,PicklistField__c from account where PicklistField__c='10'];
for(account a : trigger.new)
{
a.name='xyz';
alist.add(a);
}
if(alist.size()>0 && alist.size()!=null)
{
update alist;
}
}

 

Request to kindly check and do the needful.

 

Thanks in Advance

hitesh90hitesh90

Hi nik,

In before trigger you don't need to write DML statement in your trigger.

I have updated your code see below trigger.

 

Apex Trigger:

trigger fetchpicklistvalue on account(before insert,before update){
    //if(trigger.isinsert || trigger.isupdate)
    list<account> alist = new list<account>();
    list<account> acc = [select id,name,PicklistField__c from account where PicklistField__c='10'];
    for(account a : trigger.new){
        a.name='xyz';       
    }    
}

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

Subhash GarhwalSubhash Garhwal

Hi,

 

Hitesh is right, but your trigger will update all account records, so i think you also need to check one more condition in your for loop

 

for(Account acc : Trigger.new) {

 

if(acc.PicklistField__c == '10') {

 

 //Your logic

}

}

 

In that case it update records if PicklistField__c field value is 10

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you

 

 

This was selected as the best answer
nikkeynikkey

hi hitesh and subhash ,

 

 

Thanks for ur rply.

i appreciate ur help.