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
aakiaaki 

What is the Wrong in this Code Block

list<contact> clst = new list<contact>();
for(contact con : [Select id, otherphone from contact]) {
    con.otherphone = '768906543333';
    clst.add(con);
}
update clst;

Getting SOQL 101 Error Can Any one Correct Me. I am run this code in developer console
RKSalesforceRKSalesforce
Hi Aaki,

Code look's good.
Is this full code you are running?
Please remove everyting from Anonymous window and try executing again this code. You might be getting error due to other piece of code.

PLease mark as best answer if helped.

Regards,
Ramakant 
HARSHIL U PARIKHHARSHIL U PARIKH
Try this:
 
List<Contact> clst = New List<Contact>();
        
        For(List<Contact> EveryBatchOfCons : [Select Id, otherPhone FROM Contact])
        {
            For(Contact EveryCon : EveryBatchOfCons)
            {
                EveryCon.otherPhone = '768906543333';
                clst.add(EveryCon);
            }    
        }
        If(!clst.isEmpty())
        {
            update clst;
        }
Hope this helps!
 
Glyn Anderson 3Glyn Anderson 3
It could be that your code is OK, but there are triggers on Contact that aren't bulkified, and so they exceed SOQL limits when you insert a large number of Contact records.
dharmik thummardharmik thummar
Avoid SOQL inside or within for loop. It's that simple.