using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace makeFieldRed
{
public class checkForMoreRecords : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains(/Target/) && context.InputParameters[/Target/] is Entity)
{
if (context.PrimaryEntityName == /mahi_domain/)
{
Entity entity = (Entity)context.InputParameters[/Target/];
Entity preImage = context.PreEntityImages[/mahi_student/];//Getting preImage field value
//Entity preImage = (Entity)context.InputParameters[/preImage/];
Guid projectGuid = (Guid)preImage.Attributes[/mahi_student/]; //Getting guid of the preImage lookup
// getting domain records with student lookup
QueryExpression queryExpression = new QueryExpression(/mahi_domain/);
queryExpression.ColumnSet = new ColumnSet(/mahi_student/);
queryExpression.Criteria.AddCondition(new ConditionExpression(/mahi_student/, ConditionOperator.Equal, projectGuid));
EntityCollection domainRecords = service.RetrieveMultiple(queryExpression);
if(domainRecords != null)
{
Entity updateStudent = new Entity(/mahi_student/);
updateStudent.Id = projectGuid;
updateStudent[/mahi_domainsopp/] = /Plugin - Opp Exists/;
service.Update(updateStudent);
} else
{
Entity updateStudent = new Entity(/mahi_student/);
updateStudent.Id = projectGuid;
updateStudent[/mahi_domainsopp/] = /Plugin - Opp need to be created/;
service.Update(updateStudent);
}
}
}
}
}
}
Would be really helpful if anyone could share the correct syntax to retrieve the pre-image of a GUID field.
Or if there's a better way to go about approaching the problem.
Thanks!