+ Start a Discussion
pdostapdosta 

Using Apex Trigger to Share with Customer Portal User

Hello I am trying to use an Apex trigger to create Sharing on a custome object with a Customer Portal User referenced on the object.  Unfortunately I can only get to the Contact ID, but I want to retrieve the Contact's Customer Portal User ID, can anyone help?  Here is my code currently:

 

trigger BusinessApexShare on Participant_ID_Request__c (before update) { if(trigger.isUpdate) { List<Participant_ID_Request__Share> pidshr = new List<Participant_ID_Request__Share>(); Participant_ID_Request__Share bshr; for(Participant_ID_Request__c pid : trigger.new) { bshr = new Participant_ID_Request__Share(); bshr.ParentId = pid.Id; bshr.UserOrGroupId = pid.Business_Approver__c; bshr.AccessLevel = 'edit'; bshr.RowCause = Schema.Participant_ID_Request__Share.RowCause.ID_REQUEST_Business_Share__c; pidshr.add(bshr); } Database.SaveResult[] lsr = Database.insert(pidshr,false); Integer i=0; for(Database.SaveResult sr : lsr){ if(!sr.isSuccess()){ Database.Error err = sr.getErrors()[0]; if(!(err.getStatusCode() == StatusCode.FIELD_INTEGRITY_EXCEPTION && err.getMessage().contains('AccessLevel'))){ trigger.newMap.get(pidshr[i].ParentId). addError('Unable to grant sharing access due to the following exception: ' + err.getMessage()); } }i++; } } }

 When I try to save the record, I get the following error: "User/Group ID: id value of incorrect type"

 

 

rubixtiousrubixtious

You will need to query for the portal contacts underlying userid and create a Map.

 

List<ID> contactIds = new List<ID>(); Map<ID,ID> contactToUserMap = new Map<ID,ID>(); for(Participant_ID_Request__c p:trigger.new){ contactIds.add(p.Business_Approver__c); } for(User u:[select id, contactid from User where contactid IN :contactIds]){ contactToUserMap.put(u.contactId, id); }

 

 

Not sure if this is the most efficient way but it should work.