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
Do you have any update here? Either provide the update or close the thread by marking the answer.
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.
if (!entity.Contains("productid"))
not sign before entity is correct and your following code checks the field or its value?
That is what code does.
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"
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.
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.");
}
}
}
}
and productid (data type is lookup)
please try to fix this
Try to replace line
string product = entity.GetAttributeValue<string>("productid").ToString();
with line
var product = entity.GetAttributeValue<EntityReference>("productid").Id;
while running this code it showing an invalid argument
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
}
}
}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156