Hello,
I am not sure why I am getting this error, below is my code for the plugin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
namespace ActiveWarranty
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity Case = (Entity)context.InputParameters["Target"];
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
string serialNumber = Case.Attributes["zst_txtsn"].ToString();
Entity Warranty = new Entity("zst_warranty");
QueryExpression query = new QueryExpression("zst_warranty");
query.ColumnSet = new ColumnSet(new string[] { "zst_name" });
query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);
EntityCollection collection = service.RetrieveMultiple(query);
if (collection.Entities.Count > 0)
{
Case["zst_activewarranty"] = true;
}
else if (collection.Entities.Count == 0)
{
Case["zst_activewarranty"] = false;
}
service.Update(Case);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
}
catch (Exception ex)
{
tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
Use this updated code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
namespace ActiveWarranty
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity Case = (Entity)context.InputParameters["Target"];
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
if (Case.Attributes.Contains("zst_txtsn"))
{
string serialNumber = Case.Attributes["zst_txtsn"].ToString();
Entity Warranty = new Entity("zst_warranty");
QueryExpression query = new QueryExpression("zst_warranty");
query.ColumnSet = new ColumnSet(new string[] { "zst_name" });
query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);
EntityCollection collection = service.RetrieveMultiple(query);
if (collection.Entities.Count > 0)
{
Case["zst_activewarranty"] = true;
}
else if (collection.Entities.Count == 0)
{
Case["zst_activewarranty"] = false;
}
service.Update(Case);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
}
catch (Exception ex)
{
tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
Hope this will work,
Hi,
I have updated your code on other forum question posted by you. Please check that and let me know.
Hello,
the problem seems to be connected to GUID:
FaultException`1: Condition for attribute 'zst_warranty.zst_serialno': expected argument(s) of type 'System.Guid' but received 'System.DBNull'.
Hello ,
You should follow above suggested answers, apart from that I would suggest try to debug your plugin and write trace log you can find root cause easily for this kind of issue.
Hi,
You should always check if attribute contains in entity object. see below.
Replace this line of code
string serialNumber = Case.Attributes["zst_txtsn"].ToString();
with below
string serialNumber = String.Empty;
if(Case.Attributes.Contains("zst_txtsn"))
{
serialNumber =Case.Attributes["zst_txtsn"].ToString();
}
Then check if serialNumber is not null or empty.
Hi,
Can you double check if those fields name are correct?
This type of error usually happens when we try to access the collection and we're passing a non-existing key. For example,
Case["zst_activewarranty"] = true;
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,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156