Skip to main content

I have a REST endpoint that create/update a custom object record (Engagement). Engagement object has an after update/after insert trigger that update/insert event record based on the data from engagement record. When after update trigger is executed we receive an error 

System.DmlException: Upsert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Error: Update failed. First exception on row 0 with id 00UHr00001MfzWgMAJ; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on object id: []

After insert trigger work as expected, and event record is created. Integration user has a minimum access profile, with edit permission to edit events in System Permissions. Additionally, access granted to all event fields. Event record related to Engagement via WhatId field. 'Modify All' access granted to Engagement record via Permission set. I tried to update only Subject and DateTime fields on event but still receive the same error. 

This is fragment of my after update trigger.

for(Engagement__c bcv : bcvs){

try{

if(bcv.Visit_Staus__c == 'Cancelled'){

bcvsToDelete.add(bcv);

}else{

//collect Events and EventRelations to be recreated and deleted originally existing event

List<Event> origEvents = [SELECT Id, StartDateTime, EndDateTime FROM Event WHERE whatId =: bcv.Id WITH USER_MODE ];

system.debug('----------------------------origEvents= ' + origEvents);

//if event exists collect all original Attendees and add them to the newly created event

if(origEvents.size() > 0){

origEvents[0].Subject = bcv.Visit_Name__c;

origEvents[0].StartDateTime = bcv.Start_Date__c;

origEvents[0].EndDateTime = bcv.End_Date__c;

origEvents[0].DurationInMinutes = integer.valueOf(calcDuration(bcv.Start_Date__c, bcv.End_Date__c));

system.debug('----------------------------origEvents= ' + origEvents);

update origEvents;

//Database.update(origEvents, true, AccessLevel.SYSTEM_MODE);

}

}

} catch(QueryException e){

bcv.addError('Error query Event: ' + e.getMessage());

} catch (Exception e){

bcv.addError('Error: ' + e.getMessage());

}

}

Does any of you faced the same issue? Or maybe you have an idea how can I fix it?

1 answer
  1. Dec 20, 2024, 9:12 PM

    Hello @Viktoriia Nyzhnyk there are maybe to things to have a look at with the line 

    List<Event> origEvents = [SELECT Id, StartDateTime, EndDateTime FROM Event WHERE whatId =: bcv.Id WITH USER_MODE ];
    1. get the query outside of the for loop
    2. as you have enforced the user mode for your select, make sure the event list you get is consistent with the data you manipulate, and please check also the validation rule that has fired too

    Eric

0/9000