You need to sign in to do that
Don't have an account?
sermetege
Changing value of field
What is wrong with my code? After i execute it, annualvalue dont change...
public class ChangeValue{ public ChangeValue(){ List<Contact> cts = [SELECT ID, AccountID from Contact Where Name = 'test']; //Get list of account IDs Set<ID> accountids = new Set<ID>(); for(Contact ct: cts) if(ct.AccountID!=null){ accountids.add(ct.AccountID);} if(accountids.size()>0) { List<Account> accounts = [Select ID, AnnualRevenue from Account where ID in :accountids]; for(Account accountfound: accounts) if(accountfound.AnnualRevenue != 0) accountfound.AnnualRevenue = 500; update accounts; } } }
Have you checked the debug log? Are there any Accounts returned in the query? Do the accounts AnnualRevenue value match the IF clause?
It is a currency field you can check like this
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
For what it's worth I copied your class verbatim (and changed only the Name constraint in the Contact query) and it worked fine for me.
A simpler check than previously suggested is to look at the LastModified timestamp on the account(s) you expect to have been updated.
You are updating the accounts regardless of whether the value of AnnualRevenue needs to change (i.e. is not 0) so the LastModified timestamp should always change, assuming any accounts are selected of course.
I've tested my soql on workbench and it returns account. My debug log :
Your code runs fine in my environment as well.
Hmm, weird thought. How are you testing your code? Specifically how are you calling it? Not from a test class, yes?
Yes i used anonymous execution... How should i call?
anonymous execution is fine but are you anonymously executing the code directly or anonymously executing a test class?
I was surprised that your code ran perfectly, without modification, in my sandbox. My side-thought thinking was that if you are running a test class the data is returned to its state before the test which of course would make it appear that the Amount is not being updated—this would also explain the lack of an error message.
Regards
MellowRen
I directly run the code...
So much for that idea then. As I said it works fine in my sandbox so the code itself is fine.
Next thing I would check are your triggers. Do you have an update trigger for Account? One could be interfering and blocking the save.
Regards
MellowRen