Hi,
As I am new to plugin , I have got this error. My scenario is to create the values by retrieving from the parent entity (custom) "Acc" to child (custom) entity "Con". When Acc entity's form values gets saved, this plugin should get triggered and those field values should be automatically created in the Con entity as well. I have used the data types such as Integer, String, Optionset and decimal in the both the entities matching them.
when I saved,I got the error log, and when I debugged it using Profiling, I got the error as "Incorrect System.Decimal". I guessed that the error should be in the Decimal datatype, which I have used in the three fields such as new_UGPercentage,new_HSCPercentage and new_SSLCPercentage in the Acc entity.
Please help me on this. Note: This plugin for Create function.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk; // Namespaces to be mentioned along with System.Runtime.Serialization by browsing in the assembly box
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel;
namespace Simple_create
{
public class Class1 : IPlugin
{
// Variable declaration
int age; //Integer declaration
string name; //String declaration
string address;
int phonenumber;
OptionSetValue sex = new OptionSetValue(); // variable declaration syntax for optionset datatype
OptionSetValue degree = new OptionSetValue();
decimal hscpercentage; // decimal declaration
decimal ugpercentage = 0;
decimal sslcpercentage;
public void Execute(IServiceProvider serviceprovider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext)); // context is the place where all the fields and columnset is present with all the values
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);
if (context.Depth > 1) // to avoid infinite loop
return;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) //registered data
{
Entity entityrecord = (Entity)context.InputParameters["Target"]; //entity -> variable name
// Entity entityrecord = service.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(true));
if (entityrecord.LogicalName != "new_acc") //to be given against for which the plugin gonna be registered and check condition
return;
//Various types of Datatype check to verify it contains some data or not
if (entityrecord.Attributes.Contains("new_age") && entityrecord.Attributes["new_age"] != null)
{
age = (Int32)(entityrecord.Attributes["new_age"]); //Integer
}
if (entityrecord.Attributes.Contains("new_name") && entityrecord.Attributes["new_name"] != null)
{
name = (string)entityrecord.Attributes["new_name"]; //String
}
if (entityrecord.Attributes.Contains("new_address") && entityrecord.Attributes["new_address"] != null)
{
address = (string)(entityrecord.Attributes["new_address"]); //String
}
if (entityrecord.Attributes.Contains("new_degree") && entityrecord.Attributes["new_degree"] != null)
{
degree = (OptionSetValue)(entityrecord.Attributes["new_degree"]); //Optionset
}
if (entityrecord.Attributes.Contains("new_hscpercentage") && entityrecord.Attributes["new_hscpercentage"] != null)
{
hscpercentage = Convert.ToDecimal(entityrecord.Attributes["new_hscpercentage"]); //decimal
}
if (entityrecord.Attributes.Contains("new_ugpercentage") && entityrecord.Attributes["new_ugpercentage"] != null)
{
ugpercentage = Convert.ToDecimal(entityrecord.Attributes["new_ugpercentage"]); //decimal
}
if (entityrecord.Attributes.Contains("new_sslcpercentage") && entityrecord.Attributes["new_sslcpercentage"] != null)
{
sslcpercentage = Convert.ToDecimal(entityrecord.Attributes["new_sslcpercentage"]); //decimal
}
if (entityrecord.Attributes.Contains("new_sex") && entityrecord.Attributes["new_sex"] != null)
{
sex = (OptionSetValue)(entityrecord.Attributes["new_sex"]); //Optionset
}
if (entityrecord.Attributes.Contains("new_phonenumber") && entityrecord.Attributes["new_phonenumber"] != null)
{
phonenumber = (Int32)(entityrecord.Attributes["new_phonenumber"]); //Integer
}
// Setting the value of Parent entity ACC to child entity CON.
Entity xxx = new Entity(); // entity obj creation
xxx.LogicalName = "new_con";
xxx["new_name"] = name; // where the fields mentioned here are the destination entity (child entity) "Con"
xxx["new_yourage"] = age;
xxx["new_homeaddress"] = address;
xxx["new_degreetype"] = degree;
xxx["new_hscpercent"] = hscpercentage;
xxx["new_ugpercent"] = ugpercentage;
xxx["new_sslcpercent"] = sslcpercentage;
xxx["new_yoursex"] = sex;
xxx["new_contact"] = phonenumber;
Guid xid = service.Create(xxx);
}
//OptionSetValue - optionsetvalue
//lookup - EntityReference decimal Decimal boolean - bool
//float - ParseFloat money - Money
}
}
}
*This post is locked for comments
I have the same question (0)