1

The requirement is basically to run a query to update a number on a user custom field. The problem is that this code is being run on a trigger which has the after insert and after update events and the INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY error is being thrown.

I already tried using without sharing on the class (which is a trigger handler).

5
  • Did you enable access to that custom field for portal user? – kurunve Jul 18 '17 at 17:59
  • You could move to something async, where there are several options which allow you to specify the context user. – Adrian Larson Jul 18 '17 at 18:01
  • @kurunve The portal user shouldn't have access to the user object, I believe. But I think that's worth a try. – Renato Oliveira Jul 18 '17 at 18:06
  • @AdrianLarson could you please provide on how to specify the context user on async calls? – Renato Oliveira Jul 18 '17 at 18:06
  • 1
    Well you can log in as someone else to schedule jobs, so that part's easy. Platform Event subscriber triggers also would probably work here if you want to go down that road. – Adrian Larson Jul 18 '17 at 18:07
0

My recommendation would be that you move to a scheduled rollup calculation. You can then impersonate whatever user you wish to schedule the job, preferably some sort of generic admin user. That way, the rollup will always be calculated by someone who should have all the appropriate permissions.

If you want it sooner, then you could try Platform Events. Here you might configure a long text field which contains newline delimited Id values (creatively named Ids__c), and then have a subscriber trigger calculate the rollups. This approach seems to have a similar time scale as @future invocations in my experience so far.

From your current trigger, your publish logic would look something like:

Set<Id> ids = new Set<Id>();
// populate somehow
EventBus.publish(new MyEvent__e(
    Ids__c = String.join(new List<Id>(ids), '\n')
    //, any other state you need to save
));

Then your subscriber logic would look something like:

Set<Id> ids = new Set<Id>();
for (MyEvent__e event : events)
    ids.addAll((List<Id>)event.Ids__c.split('\n'));

List<User> records = [
    SELECT (SELECT Id FROM SomeChildren__r)
    FROM User WHERE Id IN :ids
];
// calculate rollups
update records;
1
  • I'm afraid that scheduled rollup isn't an option, because I really need this information right after the custom object's trigger is run. "Right after" might not be the proper expression, but it really is a short time (enough to make a scheduled process inviable, unless what you meant was to schedule the process to run after the trigger runs). – Renato Oliveira Jul 18 '17 at 19:12

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.