Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

Posted on by 365

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 sample.sampl1
{
public class test : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
Entity entity = (Entity)context.InputParameters["Target"];

 if (entity.LogicalName != "opportunityproduct")
return;
try{
if (entity.Attributes.Contains("productid") == false)
{
Random rndgen = new Random();
entity.Attributes.Add("productidr", rndgen.Next().ToString());
}
else
{
throw new InvalidPluginExecutionException("The product doesn't contain value.");
}

}
}
}

//and the field is a lookup field

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    Do you have any update here? Either provide the update or close the thread by marking the answer.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    What code does is literally following:

    1. Give me opportunityproduct that user/service tries to create.

    2. Check if "productid" is present in the record.

    3. If it's not present - throw an exception with provided message.

  • unbeatable Profile Picture
    unbeatable 365 on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    if (!entity.Contains("productid"))

    not sign before entity is correct and your following code checks the field or its value?

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    That is what code does.

  • unbeatable Profile Picture
    unbeatable 365 on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    I want if productid contain value it allow to save the record but if productid doesn't contain value it shows"The product doesn't contain the value"

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    I reproduced your scenario and it works for me the way it should - it shows the "The product doesn't contain value" message. Keep trying.

  • unbeatable Profile Picture
    unbeatable 365 on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    I had tried your code but there si an error message coming

    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 sample.sampl1
    {
    public class test : IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {
    IPluginExecutionContext context = (IPluginExecutionContext)
    serviceProvider.GetService(typeof(IPluginExecutionContext));
    Entity entity = (Entity)context.InputParameters["Target"];

    if (entity.LogicalName != "opportunityproduct")
    return;

    if (!entity.Contains("productid"))
    {
    throw new InvalidPluginExecutionException("The product doesn't contain value.");
    }

    }
    }
    }

    Screenshot-_2800_796_2900_.png

    and productid  (data type is lookup)

    please try to fix this

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    Try to replace line

    string product = entity.GetAttributeValue<string>("productid").ToString();

    with line

    var product = entity.GetAttributeValue<EntityReference>("productid").Id;

  • unbeatable Profile Picture
    unbeatable 365 on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception message

    while running this code it showing an invalid argument

    pastedimage1604326817644v1.png

  • unbeatable Profile Picture
    unbeatable 365 on at
    RE: plugin to trigger when filed contains value it allow to create the record if field doesn't contain value it show invalid exception 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 sample.sampl1

    {

       public class test : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               //Obtain the execution context from the service provider.

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

               if (context.MessageName != "Create")

                   return;

               //Get a reference to the Organization service.

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

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

               //Extract the tracing service for use in debugging sandboxed plug-ins

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

               #region working code

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

               {

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

                   if (entity.LogicalName != "opportunityproduct")

                   {

                       return;

                   }

                   if (context.Depth > 1)

                   {

                       return;

                   }

                   try

                   {

                       if (entity.Attributes.Contains("productid"))

                       {

                           string product = entity.GetAttributeValue<string>("productid").ToString();

                           //handle the above line of code accordingly based on dataType(String, EntityReference, Datetime etc.)

                           QueryExpression contactQuery = new QueryExpression("opportunityproduct");

                           contactQuery.ColumnSet = new ColumnSet("productid");

                           contactQuery.Criteria.AddCondition("productid", ConditionOperator.Equal, product);

                           EntityCollection contactColl = service.RetrieveMultiple(contactQuery);

                           if (contactColl.Entities.Count > 0)

                           {

                               throw new Exception("prduct is Invalid !!!");

                           }

                       }

                   }

                   catch (Exception ex)

                   {

                       throw (ex);

                   }

               }

               #endregion

           }

       }

    }

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans