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
Trif Cristian 7Trif Cristian 7 

Helpp

Hello, im a newbie and i need some help. I have the next logic in my apex class, i just copied from internet so i dont know exactly whats happening, im not convinced. So, here it is:

Account a =[SELECT name, phone, fax, industry where name = 'Dell'];
if(a.Industry != 'Industry')
a.Industry = 'Industry';
update a;

So basically it saying that fetch name, phone, fax, industry from an existing account with the name Dell. And here on if im confused. If industry field not equal to industry then update that field to industry? Did i say wrong? On if then else statement why we dont have curly brackets? and where is the else statement?
Best Answer chosen by Trif Cristian 7
EldonEldon
Hi Trif,

You only have one code inside your if condition thats why curly brackets are missed and your logic is correct though code has a small error. Corrected code is given below. (Id is queried in all soql queries you dont need to give it separately).
 
Account a =[SELECT name, phone, fax, industry from account where name = 'Dell'];
if(a.Industry != 'Industry')
   a.Industry = 'Industry';
update a;



Let me know if you need more clarifications.

Mark it as best answer if it solved your doubts.

Regards

All Answers

Dilip_VDilip_V
Christian,

To update a record we must need record Id

You missed Id in your query.
 
Account a =[SELECT Id,name, phone, fax, industry where name = 'Dell'];
if(a.Industry != 'Industry')
a.Industry = 'Industry';
update a;

Let me know if you have any issues.

Mark it as best answer if it helps.

Thanks.
EldonEldon
Hi Trif,

You only have one code inside your if condition thats why curly brackets are missed and your logic is correct though code has a small error. Corrected code is given below. (Id is queried in all soql queries you dont need to give it separately).
 
Account a =[SELECT name, phone, fax, industry from account where name = 'Dell'];
if(a.Industry != 'Industry')
   a.Industry = 'Industry';
update a;



Let me know if you need more clarifications.

Mark it as best answer if it solved your doubts.

Regards
This was selected as the best answer