function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
h8r41dh8r41d 

Is there a way to override ownerid when inserting activity data for a portal session?

I have a client who has some code that automatically adds a notes task when a case is closed. It works fine except when a portal user closes their case, then I get this error:

 

First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Portal user can't own an activity: []

 

Is there a way to get around this issue and still insert an activity record when running as a PU? Can I override the user session limitations and add records as a system admin in the class?

Starz26Starz26

You can display that object as a vf page and create a custom save method which assigns the owner id of your choosing.

xpl0idxpl0id

You can create a trigger on Task and Event objects:

 

trigger ActivityOwner on Task (before insert) {
    
    Set<String> portalProfile = new Set<String>();
    for(Profile p : [SELECT ID FROM Profile WHERE Name like '%Portal%'])
    {
        portalProfile.add(p.ID);
    }
    
    for(Task t : Trigger.new)
    {
        if(portalProfile.contains(UserInfo.getProfileId()))
        {
            t.OwnerId = 'SF_USER_ID';
        }
    }
}