Hi, Thank you for the response.
I am running the following code and getting this error:
Exception Message: An exception System.FormatException was thrown while trying to convert input value ' + ownerid + ' to attribute 'teammembership.systemuserid'. Expected type of attribute value: System.Guid. Exception raised: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
I am not sure how to pass the ownerid. i tried the way you have suggested. i got the similar error. so i tried to pass the ownerid into the fetchxml as + ownerid +. Still getting errors. Any suggestion on how to pass the owner id into fetchXML.
public class OwnershipUpdateToTeam : IPlugin
{
// When a new contact is created from CRM, automatically assign the ownership of the contact to the team of the user who is creating the contact
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var entity = context.InputParameters["Target"] as Entity;
if (entity.LogicalName != "contact" || !entity.Contains("ownerid"))
return;
//serivice
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//EntityReference owner = entity.GetAttributeValue<EntityReference>("ownerid");
Guid ownerid = entity.GetAttributeValue<EntityReference>("ownerid").Id;
//Get user's team
var fetchXml = $@"
<fetch >
<entity name='team'>
<attribute name='teamid' />
<attribute name='teamtype' />
<order attribute='createdon' descending='false' />
<filter type='and'>
<condition attribute='teamtype' operator='eq' value='0' />
</filter>
<link-entity name='teammembership' from='teamid' to='teamid' visible='false' intersect='true'>
<filter >
<condition attribute='systemuserid' operator='eq' value=' + ownerid + '/>
</filter>
</link-entity>
</entity>
</fetch>";
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));
if (result.Entities.Count > 0 )
{
//get the team and assign the team to owner field
foreach (var c in result.Entities)
{
var team = c.Attributes["teamid"];
var tempContact = new Entity("contact", entity.Id);
tempContact["ownerid"] = team;
service.Update(tempContact);
break;
}
}
}
}