Hi RajarajanS,
Thank you for your reply.
I already tested it with a user and I got the same error. I need to explain that I do not have any problem when I assign it to the team in the PostOperation of Lead creation.
My problem is in the PreOperation, when I just change the ownerid field to an EntityReference team. I just copy my both codes to explain it better:
1) The following code is the main code that I need. The step would be "create lead PreOperation". It doesn't work.
try
{
var context = new CrmServiceContext(OrganizationService);
var lead = Entity.ToEntity<Lead>();
if (PluginContext.MessageName.ToLower() == "create")
{
if (PluginContext.Depth <= 1)
{
if (lead.ptvits_Country != null)
{
var countryEntity = OrganizationService.Retrieve(lead.ptvits_Country.LogicalName,
lead.ptvits_Country.Id, new ColumnSet(true));
var country = countryEntity.ToEntity<ptvits_country>();
if (country != null)
lead.Address1_Country = country.ptvits_name;
}
var teamId = new Guid("09AC211F-AD95-EA11-A811-000D3A23C48F"); // AL team in Lead assignment environment
lead.OwnerId = new EntityReference("team", teamId);
}
}
}
2) The following code is my second solution that is not ideal for me, because it will insert my data in two steps. The step here would be "create lead PostOperation. It actually works, and you see I am using the same team id.
try
{
var context = new CrmServiceContext(OrganizationService);
var lead = Entity.ToEntity<Lead>();
if (PluginContext.MessageName.ToLower() == "create")
{
if (PluginContext.Depth <= 1)
{
if (lead.ptvits_Country != null)
{
var countryEntity = OrganizationService.Retrieve(lead.ptvits_Country.LogicalName,
lead.ptvits_Country.Id, new ColumnSet(true));
var country = countryEntity.ToEntity<ptvits_country>();
if (country != null)
{
lead.Address1_Country = country.ptvits_name;
context.Attach(lead);
context.UpdateObject(lead);
context.SaveChanges();
}
}
var teamId = new Guid("09AC211F-AD95-EA11-A811-000D3A23C48F"); // AL team in Lead assignment environment
var assignRequest = new AssignRequest()
{
Assignee = new EntityReference
{
LogicalName = Team.EntityLogicalName,
Id = teamId
},
Target = new EntityReference(Lead.EntityLogicalName, lead.Id)
};
OrganizationService.Execute(assignRequest);
}
}
}
Now, I want to know what's wrong with the no.1 code? I need it to work.