0

Problem Statement

I need to change the Parent of the Chatter Post (FeedItem) when a specific community user posts a question or poll type post to a Collaboration Group.

What have I done so far

I have written a trigger to solve the problem stated above, which appears quite simple, however, in the test class I am getting an error when I try to post a feed using System.runAs(CommunityMemberUser), I receive insufficient privileges error.


Trigger

trigger FeedItemTrg on FeedItem(before Insert)
{
    // get the community id
    Network[] wfCommunity = [SELECT Id, Name FROM Network WHERE Name = 'Workfront Experience' AND Status = 'Live' LIMIT 1];
    Id wfCommunityId = wfCommunity[0].Id;
    for(FeedItem fi: trigger.new)
    {
        // check if the record belongs to community, Question or Poll type, and posted to a user
        if(fi.NetworkScope == wfCommunityId && (fi.Type== 'QuestionPost' || fi.Type=='PollPost') && String.valueOf(fi.ParentId).subString(0, 3) == '005')
        {
            // change the parent to discussion group Id
            fi.ParentId = '[CollborationGroupId]';
        }

    }
}


Test class

@isTest static void insertTest()
{
    // query communities
    List<Network> wfCommunity = [SELECT Id, Name FROM Network WHERE Name = 'Workfront Experience' LIMIT 1];
    // find a member of the community
    List<NetworkMember> ntmembers = [SELECT Id, MemberId, Network.Name FROM NetworkMember WHERE NetworkId=: wfCommunity[0].Id LIMIT 1];
    List<User> usr = [SELECT Id FROM User WHERE Id=:ntmembers[0].MemberId];
    // post using member of the community
    System.runAs(usr[0])
    {
        // create a feed item post with with group parent
        FeedItem fi = new FeedItem();
        fi.Title = 'test post';
        fi.ParentId = Userinfo.getUserId();
        fi.Type = 'QuestionPost';
        fi.NetworkScope = wfCommunity[0].Id;
        insert fi;
        // test class behavior with asserts
        fi = [SELECT Id, Type, ParentId, NetworkScope FROM FeedItem WHERE Id=: fi.Id];
        System.assertEquals('QuestionPost', fi.Type);
        System.assertEquals([GROUPID], fi.ParentId);
    }

}


Error

Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

0

As per the help article

It is not currently possible to simply re-parent Chatter Feed records or move them from one record to another via an update operation. This is because the FeedItem ParentId field that stores the ID of the object type or record to which the FeedItem is related does not support an update call. To workaround this limitation, you can recreate the posts and associate them with the new parent record.

Although, I was re-parenting it in before insert event, it still did not allow me and gave weird error of Insufficient Access on cross reference field. Anyways, after doing debugging and reading above help article, I recreated the post with new Parent in the after insert event and it was successfully created with desired ParentId.

| improve this answer | |

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.