Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Create New Student Record

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

  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,506 Super User 2024 Season 1 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
  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 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");
    				}
    			}
    		}
    	}
    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans