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
sermetegesermetege 

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;
        }
    }    
}

 

aj2taylo2aj2taylo2

Have you checked the debug log?  Are there any Accounts returned in the query?   Do the accounts AnnualRevenue value match the IF clause?

souvik9086souvik9086

It is a currency field you can check like this

 

if(accountfound.AnnualRevenue != $0)

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

GSBassoGSBasso

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.

sermetegesermetege

I've tested my soql on workbench and it returns account. My debug log : 

Anonymous execution was successful.

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: public class ChangeValue{
Execute Anonymous:     public ChangeValue(){
Execute Anonymous:         List<Contact> cts = [SELECT ID, AccountID from Contact Where Name = 'test'];
Execute Anonymous: 		//Get list of account IDs    
Execute Anonymous: 		Set<ID> accountids = new Set<ID>();
Execute Anonymous:         for(Contact ct: cts) if(ct.AccountID!=null){
Execute Anonymous:         	accountids.add(ct.AccountID);}
Execute Anonymous:         if(accountids.size()>0)
Execute Anonymous:         {
Execute Anonymous:             List<Account> accounts = [Select ID, AnnualRevenue 
Execute Anonymous:                                       from Account where ID in :accountids];
Execute Anonymous:             for(Account accountfound: accounts)
Execute Anonymous:                 if(accountfound.AnnualRevenue != 0)
Execute Anonymous:                 accountfound.AnnualRevenue = 500;
Execute Anonymous:             update accounts;
Execute Anonymous:         }
Execute Anonymous:     }    
Execute Anonymous: }
15:40:59.045 (45714000)|EXECUTION_STARTED
15:40:59.045 (45726000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
15:40:59.512 (46164000)|CUMULATIVE_LIMIT_USAGE
15:40:59.512|CUMULATIVE_LIMIT_USAGE_END

15:40:59.046 (46183000)|CODE_UNIT_FINISHED|execute_anonymous_apex
15:40:59.046 (46192000)|EXECUTION_FINISHED

 

MellowRenMellowRen

Your code runs fine in my environment as well.

MellowRenMellowRen

Hmm, weird thought. How are you testing your code? Specifically how are you calling it?  Not from a test class, yes?

sermetegesermetege

Yes i used anonymous execution... How should i call?

MellowRenMellowRen

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

sermetegesermetege

I directly run the code...

MellowRenMellowRen

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

sermetegesermetege
I have triggers that belongs to third party apps...