7

Is it possible to change the owner of the record through APEX even though the user that executing the apex code is not the owner or system admin?

11

Yes, its possible if you write your class without sharing.

Without sharing keyword make sure that code runs in system context and so it has full access to change the ownership of record even if current user id not the owner.

Here is sample code that should give you idea.

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

Here are more details on without sharing https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_sharing.htm

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.