0

I have a unique requirement in communities but I will explain it simple. I wanted to change the ownership of account to internal user as soon as it is created by a community user and grant READ access to the user who created it so that community user can see it. I have got a working solution using a scheduled batch process but wanted it in real time using a Trigger. I tried different options but always endup with some kind exception.

This is what I am doing in the trigger.

  1. Change ownership to internal user. Just setting account.Owner to internal user id.
  2. Create a new Accountshare with RowCause Manual using Apex to the user who created it.

I got INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id:

I even tried to delete existing share and create new one but could not succeed.

Would appreciate any insight on how to implement this in trigger?

2 Answers 2

1

This exception might happen when you try creating accountshare for an account to an user that already exists. In case you are using a before trigger to do this, changing the owner field will work fine. But when you try to create new share records that might cause a problem. In the salesforce database accountshare record already exists with an access level = 'All', for the community user because he is the owner. Now you have changed the owner in the before trigger, but the change has not yet been committed to the database, post which only recalculation of the sharing tables will take place.(i.e. the old accountshare record giving the community user Owner level access would be deleted, and the new accountshare record will be created for the internal user with Owner level access).

So, the part 1 - where you change the owner of the record is fine. The part 2 - where you create read level accountshare record for the original community user needs to be performed in a future method or done separately in an after trigger.

Hope this helps.

4
  • I got you are saying and that is the reason I used a batch job to update and create new shares which works fine but they wanted in real-time. It seems it can't be done synchronously and we need to use a batch job or a feature job. Thanks for your insight.
    – sdondeti
    Commented Dec 9, 2015 at 17:33
  • You could consider an after trigger. Since this runs after the data commit it the system might have recalculated the sharing tables.
    – Prajith
    Commented Dec 11, 2015 at 8:01
  • Tried after trigger but same exception. It seems sharing rules are calculated after the commit.
    – sdondeti
    Commented Dec 12, 2015 at 15:45
  • Okay, then as you said we dont seem to have a real time solution. Sorry I couldn't help.
    – Prajith
    Commented Dec 14, 2015 at 6:29
0

Your community user profile need to have the ability to change record ownership.

2
  • I think changing ownership is not the issue. I can see the DML statement executed successfully. The main issue is with sharing.
    – sdondeti
    Commented Dec 9, 2015 at 16:39
  • Then, try setting the Account Share prior to changing ownership. Maybe the exception is thrown because de user have no access on the account if it's not the owner. Commented Dec 9, 2015 at 17:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .