• gmog220448
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

Hi Everybody,
I have a problem while trying to update actorid of a ProcessInstanceWorkItem record through Apex.  As part of delegating to a new user, we need to reassign the request to a new user.  This update can be done by an administrator (System Administrator) or by a manager (who can be a normal user such as Default Employee profile user, etc).  The code is working for administrator users.  But throwing the below error.
   
Error in Saving the Record: Delegate_after_trig: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 04iT0000000LcnHIAS; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] Class.myClass.tstMethod: line 22, column 1 trigger.Delegate_after_trig: line 182, column 1
   
I thought if I can run the apex code as System Administrator, I may not come across the above error.  Hence I tried the below approaches.

  1. Set "Modify All" flag on the request, which starts the approval process.  This worked fine.  But this allowed the users to see other users details.  But we don't want this behavior.  Hence this option is ruled out.
  2. Created a class "without sharing" with only this method (as the documentation mentioned that the code in this class does not follow the logged in user's sharing rules and run as System Administrator)
  •             Called this method from another class.  But this threw the same error mentioned above.
  •             Called this method from a trigger.  But this threw the same error mentioned above.


Here is the code, which I am using to update the actorid.  I hardcoded some values for my testing. The query is working fine and returning the result what I am expecting.  I tried updating OriginalActorId by logging in as normal user, it is getting updated with out any errors.

 

I found some posts with this issue on this board, but did not have much details on what they did to make it work for them.  Hence I thought of seeking your help.  I greatly appreciate if someone can help me in resolving this error.

    
public without sharing class myClass{
    public static void tstMethod() {
        //Get Processinstance workitem
        List<ProcessInstanceWorkItem> piwi_to_update = new List<ProcessInstanceWorkItem>();
        List<ProcessInstanceWorkItem> piwi = [SELECT ActorId, OriginalActorId
                            FROM ProcessInstanceWorkitem
                               WHERE ProcessInstanceId in ('04gT000000070mUIAQ')
                             AND actorid != '00530000003eDDbAAM'];
        System.debug('piwi ----------> ' + piwi);
        for (integer j=0; j<piwi.size(); j++){
            System.debug('--------------->piwi[' + j + '] ------------> ' + piwi[j]);
            piwi[j].ActorId = '00530000001mxv2';
            //piwi[j].OriginalActorId = '005T0000001UydwIAC';
            piwi_to_update.add(piwi[j]);
        }
        //Update ProcessInstance work items
        system.debug('-------------->piwi_to_update = ' + piwi_to_update);
        update piwi_to_update;
    }
}

Hi Everybody,
I have a problem while trying to update actorid of a ProcessInstanceWorkItem record through Apex.  As part of delegating to a new user, we need to reassign the request to a new user.  This update can be done by an administrator (System Administrator) or by a manager (who can be a normal user such as Default Employee profile user, etc).  The code is working for administrator users.  But throwing the below error.
   
Error in Saving the Record: Delegate_after_trig: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 04iT0000000LcnHIAS; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] Class.myClass.tstMethod: line 22, column 1 trigger.Delegate_after_trig: line 182, column 1
   
I thought if I can run the apex code as System Administrator, I may not come across the above error.  Hence I tried the below approaches.

  1. Set "Modify All" flag on the request, which starts the approval process.  This worked fine.  But this allowed the users to see other users details.  But we don't want this behavior.  Hence this option is ruled out.
  2. Created a class "without sharing" with only this method (as the documentation mentioned that the code in this class does not follow the logged in user's sharing rules and run as System Administrator)
  •             Called this method from another class.  But this threw the same error mentioned above.
  •             Called this method from a trigger.  But this threw the same error mentioned above.


Here is the code, which I am using to update the actorid.  I hardcoded some values for my testing. The query is working fine and returning the result what I am expecting.  I tried updating OriginalActorId by logging in as normal user, it is getting updated with out any errors.

 

I found some posts with this issue on this board, but did not have much details on what they did to make it work for them.  Hence I thought of seeking your help.  I greatly appreciate if someone can help me in resolving this error.

    
public without sharing class myClass{
    public static void tstMethod() {
        //Get Processinstance workitem
        List<ProcessInstanceWorkItem> piwi_to_update = new List<ProcessInstanceWorkItem>();
        List<ProcessInstanceWorkItem> piwi = [SELECT ActorId, OriginalActorId
                            FROM ProcessInstanceWorkitem
                               WHERE ProcessInstanceId in ('04gT000000070mUIAQ')
                             AND actorid != '00530000003eDDbAAM'];
        System.debug('piwi ----------> ' + piwi);
        for (integer j=0; j<piwi.size(); j++){
            System.debug('--------------->piwi[' + j + '] ------------> ' + piwi[j]);
            piwi[j].ActorId = '00530000001mxv2';
            //piwi[j].OriginalActorId = '005T0000001UydwIAC';
            piwi_to_update.add(piwi[j]);
        }
        //Update ProcessInstance work items
        system.debug('-------------->piwi_to_update = ' + piwi_to_update);
        update piwi_to_update;
    }
}