Hello Community,
I am facing difficulty in sharing a record using a plugin. I will paste the function below.
The code is not returning any error and executing normally, but the records are not being shared. The user ID and the record ID are passed.
I tried to debug the code and it is working fine without any errors, but without sharing.
Can someone assist me with any error in my function or another approach for sharing?
public void ShareRec(Mission mission, List<RecordToShare> recs)
{
foreach (var userId in mission.MissionPrev.User)
{
var userReference = new EntityReference(/systemuser/, new Guid(userId));
foreach (var privilege in mission.MissionPrev.Privileges)
{
var accessMask = Models.Roles[privilege.Id];
foreach (var record in recs)
{
var recordReference = new EntityReference(record.EntityName, record.RecordId);
var grantAccessRequest = new GrantAccessRequest
{
PrincipalAccess = new PrincipalAccess
{
AccessMask = accessMask,
Principal = userReference
},
Target = recordReference
};
try
{
service.Execute(grantAccessRequest);
// Handle success or logging here if needed
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
// Handle the exception (logging, error handling, etc.)
}
}
}
}
}