1

I want to achieve the same functionality as that of account team in project(Custom object)  using trigger . For this I have made a new custom object as projectteam and added lookup of user as well as project. and also tried a code but not getting if it is correct or not

This is my code:

trigger projectteam on Projectteam__c (after insert) {
        if(trigger.IsInsert){

        Projectteam__c project=new Projectteam__c();

        List<Projectteam__share> projectshares=new List<Projectteam__share>();
        for(Projectteam__c proj:trigger.new){
            Projectteam__share projRecord=new Projectteam__share();
            projRecord.ParentId=proj.Related_Project__c;
            projRecord.UserOrGroupId =proj.Team_member__c;
            projRecord.AccessLevel = 'read';
            projRecord.RowCause = 'Manual';
           projectshares.add(projRecord);
        }
            insert projectshares;
    }
}

and I am getting this error : Review all error messages below to correct your data.

Apex trigger projectteam caused an unexpected exception, contact your administrator: projectteam: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Parent ID: id value of incorrect type: a032v00002tCFh8AAG: [ParentId]: Trigger.projectteam: line 13, column 1

can anyone suggest what should i do?

2
  • The type of projRecord.ParentId isn't the same for proj.Related_Project__c
    – anouar_ma
    Nov 22, 2019 at 11:04
  • The sharing model is private
    – maria
    Nov 22, 2019 at 11:18

1 Answer 1

2

The problem here is you want to share the project object record with Users. So ideally, Instead of inserting Projectteam__share record, you should insert Project__share object record.

in your case, You are inserting Projectteam__share object record. So the parentid here expect an id from Projectteam__c object record but you are giving Project__c record id. Hence it result in FIELD_INTEGRITY_EXCEPTION, Parent ID: id value of incorrect type.

The correct code for you will be:-

trigger projectteam on Projectteam__c (after insert) {
        if(trigger.IsInsert){

        List<Project__share> projectshares=new List<Project__share>();
        for(Projectteam__c proj:trigger.new){
            Project__share projRecord=new Project__share();
            projRecord.ParentId=proj.Related_Project__c;
            projRecord.UserOrGroupId =proj.Team_member__c;
            projRecord.AccessLevel = 'read';
            projRecord.RowCause = 'Manual';
           projectshares.add(projRecord);
        }
            insert projectshares;
    }
}

Read more here:- Sharing a Record Using Apex

2
  • Yes, The code i have uploaded will trigger on project team record insertion and will insert an Project__share record which share the project record with Users. Nov 22, 2019 at 11:19
  • Yes Its working good thank you for your help
    – maria
    Nov 22, 2019 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .