web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Create New Student Record

(0) ShareShare
ReportReport
Posted on by 5

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");
}
}
}

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Create New Student Record

    Hello,

    Check the following 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("stu_name") : string.Empty;
    
    				if (StudentName == "Anna") 
    				{
    					throw new InvalidPluginExecutionException("This entry is not allowed");
    				}
    			}
    		}
    	}
    }

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,409 Super User 2025 Season 2 on at
    Create New Student Record
    Hello user,
     
    What is the trigger point of your plugin? if you have trigger point as the given boolean field then it will create a loop.
    You can use depth to go out of loop. Please refer below link.

    Thank you,
    Amit Katariya

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans