Hi All, Please help me in this.
If student name is “Anna” then show error message “This entry is not allowed”. Or else Create With another name… but it runs into infinite loop.
I have registered this plugin on Create of New Student.
Message:Create
Primary Entity: Student
Stage:Post-Operation (Synchronous)
Here is my code,
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnCreateStudent
{
public class CreateStudent : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
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)
{
Entity Student = (Entity)context.InputParameters["Target"];
String StudentName = Student.Contains("stu_name") ? Student.GetAttributeValue<string>("stu_name") : string.Empty;
if (StudentName != String.Empty)
{
Entity student = new Entity("stu_student");
new Entity("stu_student")["stu_name"] = "Amaan";
new Entity("stu_student")["Amaan"] = false;
}
else
{
new Entity("stu_student")["Amaan"] = true;
}
service.Create(new Entity("stu_student"));
}
throw new InvalidPluginExecutionException("This entry is not allowed");
}
}
}