-2

this is apex sharing code, but the problem is this when I'm creating acc, entering parentId of acc which I have created as demo user, error

public class ManuallyShareAccount {
    public static void manualShare(List<Account> newList) {
        Set<Id> accSet = new Set<Id>();
        List<Account> relatedAccount = new List<Account>();
        for (Account acc: [SELECT ParentId FROM Account WHERE Id IN: newList]) {
            accSet.add(acc.ParentId);
            System.debug(acc.ParentId);
        }

        relatedAccount = [SELECT 
                            Id, 
                            ParentId, 
                            OwnerId 
                        FROM 
                            Account
                        WHERE 
                            (ParentId IN: accSet AND ParentId != Null) AND 
                            ID NOT IN: newList 
                        ];

        System.debug('relatedAccount' + relatedAccount);
        List<AccountShare> shareList = new List<AccountShare>();

        for (Account acc: newList) {
            for (Account acc1: relatedAccount) {
                AccountShare accshare = new AccountShare();
                if (acc1.ParentId == acc.ParentId) {
                    accShare.AccountId = acc.id;
                    accShare.UserOrGroupId = acc1.OwnerId;
                    accShare.RowCause = Schema.AccountShare.RowCause.Manual;
                    accShare.AccountAccessLevel = 'Read';
                    accShare.OpportunityAccessLevel = 'Read';
                    shareList.add(accShare);
                }
            }
        }
        if (shareList.size() > 0)
            insert shareList;
    }

}

Error which I am getting :

Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 1; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []: Class.ManuallyShareAccount.manualShare: line 35, column 1

  • Can you please provide the error which you are getting? – Naval Sharma Dec 28 '18 at 12:19
  • Could you show how you are executing this code? Are you running it via Anonymous Apex or in a unit test context? – David Reed Jan 1 at 1:08
  • No I'm not executing it on Anonymous window, I made a proper Trigger handler class and I'm calling it on isAfter and isInsert trigger – Kamakshi Sharma Jan 1 at 16:28
0

Looks like your trigger is being executed in the current user context.

If you are calling this class from the trigger then you should declare it "Without Sharing".

If you are not declaring the class Without Sharing then trigger will execute the code in current user context and that means you have not sufficient rights to share that record with other users. You either must be an owner of the record or have Modify All Permission at the profile level when OWD is set to Private/Read-Only

If you manually check the record then you will not see the Share button on the detail page. It's all documented here.

In order to share a record you should be either an owner of the record or should have Modify All permission.

  • Actually, I'm a system admin, also I'm not calling this class from the trigger, but I've made a trigger Handler class, and I'm calling the handler class method inside the trigger, which is after Insert – Kamakshi Sharma Dec 29 '18 at 4:57
  • Hello, I've used with sharing and without sharing but this doesn't worked either – Kamakshi Sharma Dec 29 '18 at 5:13
  • @KamakshiSharma - Do you have Modify All permission on Account object? – Naval Sharma Dec 29 '18 at 7:17
  • yes as I'm a system admin, I have it also I checked it again – Kamakshi Sharma Dec 31 '18 at 5:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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