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 :
Customer experience | Sales, Customer Insights,...
Answered

i have created plugin to test if this field contain yes value it display message and if it contain no its display message.

(0) ShareShare
ReportReport
Posted on by

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace isvalid.Plugins
{
public class productopp : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.It Contains the information Current Loged in User like user id etc.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);

ITracingService TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

//InputParameters Contains all the information of Message request send by the user cliend site
// Target is checking two thing is this Targeting(have primary entity or not) any entiy or not and what evere we are target is entity or not
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
//Obtain the target entity from the input parameters
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "opportunityproduct")
return;
try
{
string str = entity.FormattedValues["new_isvalid"].ToString();
if (str == "Yes")
{
throw new InvalidPluginExecutionException("The Product is valid.");

}
else
{
throw new InvalidPluginExecutionException("The Product is Invalid.");
}
}
catch (Exception ex)
{
TracingService.Trace("MyPlugin:{0}", ex.ToString());
}

}
}
}
}

  • Mahendar Pal Profile Picture
    on at
    RE: i have created plugin to test if this field contain yes value it display message and if it contain no its display message.

    Hi,

    What is the data type of your field it it's two option set you can simply do like below

    boolean response =entity.GetAttributeValue<boolean>("new_isvalid");

    if(response )

    {

    throw new InvalidPluginExecutionException("The Product is valid.");

    }

    else

    {

    throw new InvalidPluginExecutionException("The Product is Invalid.");

    }

    }

  • Suggested answer
    Bipin D365 Profile Picture
    on at
    RE: i have created plugin to test if this field contain yes value it display message and if it contain no its display message.

    HI,

    You can achive this requirement with simple workflow.

    Why did you go for Plugin custom code. With no code you can easily achieve. See below workflow.

    CQ67.PNG

    Click on Set Properties to provide your error message.

    Trigger your workflow on Create of Opportunity Line entity.

    Please mark my answer verified if i were helpful

  • unbeatable Profile Picture
    on at
    RE: i have created plugin to test if this field contain yes value it display message and if it contain no its display message.

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    namespace isvalid.Plugins

    {

       public class productopp : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               // Obtain the execution context from the service provider.It Contains the information Current Loged in User like user id etc.

               IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);

               ITracingService TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               //InputParameters  Contains all the information of Message request send by the user cliend site  

               // Target is checking two thing is this Targeting(have primary entity or not)   any entiy or not and what evere we are target is entity or not

               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

               {

                   //Obtain the target entity from the input parameters

                   Entity entity = (Entity)context.InputParameters["Target"];

                   if (entity.LogicalName != "opportunityproduct")

                       return;

                   try

                   {

                       boolean resp = entity.GetAttributeValue<boolean>("new_isvalid");

                       if (resp)

                       {

                           throw new InvalidPluginExecutionException("The Product is valid.");

                       }

                       else

                       {

                           throw new InvalidPluginExecutionException("The Product is Invalid.");

                       }

                   }

                   catch (Exception ex)

                   {

                       TracingService.Trace("MyPlugin:{0}", ex.ToString());

                   }

               }

           }

       }

    }

    i m getting error on if(resp)pastedimage1604044815142v1.png

  • Verified answer
    Bipin D365 Profile Picture
    on at
    RE: i have created plugin to test if this field contain yes value it display message and if it contain no its display message.

    HI,

    Update your code as below.

    bool resp = entity.GetAttributeValue<bool>("new_isvalid");

    It will work.

    Please mark my answer verified if i were helpful

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans