0

Account Owner Lookup field needs to be updated based on the Region field of the user. Admin Will run the anonymous apex, If the users region and Account's Field Dealer Code Values match the Account Ownership will be updated to the user of the same region.

This is the code i could think of..

public without sharing class noSharing {
        public static void changeOwner(Account acc, User us)
        {
            If(acc.Area_Code__c==us.Region__c)
            {  
             acc.OwnerId = us.Id;
             update acc;
            }
         }
}

Getting an error like "Static Can Only be used on methods of top level type"

Can anyone please help?

2

As the error states, you cannot put a static method on an inner class. Because of the underlying behavior of how execute anonymous works, all classes inside the execute anonymous scripts are inner classes. In other words, when you write a script like this:

public class x { }

The compiled version of what's run looks like this:

public class $anon {
  public class x { }
}

Which makes x an inner class. In execute anonymous, you would need to make an instance of the class and call it that way:

public without sharing class noSharing {
    public void changeOwner(Account acc, User us)
    {
        If(acc.Area_Code__c==us.Region__c)
        {  
         acc.OwnerId = us.Id;
         update acc;
        }
     }
}
noSharing ns = new noSharing();
...
ns.changeOwner(acc, us);

Please note that your code likely has other problems, such as bulkification issues, so you may need to adjust your code to account for this. Most likely, you'll need to write a proper batchable class in order to complete your desired goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.