Hi,
I wrote a plugin to save more than one firm in one form. When user selects 3 firms , in databae 3 rows added according to the firm information. But some fields should be sam such as fatura no, fatura tarihi etc. Only firm (mağaza) ids , marka (brand number) should be different; the other infos are same.
If i want to save one firm, it will be easy to save.(teh original form was made to save one firm and infos about it). But the companuy wants me to to in this form to save more than one firm with ists info. Since i wrote the plugin. It gives me error: Specified cast is not valid.
Her is the eror log:
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: ERROR: message:Specified cast is not valid.Detail:
<OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
<ErrorCode>-2147220891</ErrorCode>
<ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>OperationStatus</d2p1:key>
<d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:string">0</d2p1:value>
</KeyValuePairOfstringanyType>
<KeyValuePairOfstringanyType>
<d2p1:key>SubErrorCode</d2p1:key>
<d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>ERROR: message:Specified cast is not valid.</Message>
<Timestamp>2016-10-17T11:41:54.5480695Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>
[CheckboxMagazaKaydet: CheckboxMagazaKaydet.MyPlugin]
[6bc53568-5b94-e611-80d8-0050569f563c: CheckboxMagazaKaydet.MyPlugin: Create of new_destek_yerelreklam_detay]
</TraceText>
</OrganizationServiceFault>
Here is my form:
and here is my plugin code:
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 CheckboxMagazaKaydet
{
// Bu plugin yerle rekalm desteğinde birden fazla mağaza seçildiğinde mağazaları ve verilerini kaydeder.
public class MyPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if ("Create,Update".Contains(context.MessageName) == false) return;
if (!context.InputParameters.Contains("Target") || (context.InputParameters["Target"] is Entity) == false) return;
Entity entity = (Entity)context.InputParameters["Target"];
try
{
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);
#region Yerel Reklam Desteği
if (entity.LogicalName == "new_destek_yerelreklam_detay")
{
//The brands of the firms
string markalar = entity.Contains("new_marka") ? entity["new_marka"].ToString() : "";
string[] marka = markalar.Split(',');
for (int j = 0; j < marka.Length; j++)
{
marka[j] = marka[j].Trim();
}
//The ids of the firms
string ids = entity.Contains("new_ids") ? entity["new_ids"].ToString() : "";
string[] id = ids.Split(',');
for (int k = 0; k < id.Length; k++)
{
id[k] = id[k].Trim();
}
string magazalar = entity.Contains("new_multiline") ? entity["new_multiline"].ToString() : "";
string[] magaza = magazalar.Split(',');
for (int i = 0; i < magaza.Length; i++)//It loops in new_multiline textare according to thenumber of firms
{
magaza[i] = magaza[i].Trim();
entity["new_magaza"] = id[i];
Decimal tutar = (Decimal)entity.Attributes["new_tutar"];
tutar = tutar / (i);
int faturano = (int)entity.Attributes["new_faturano"];
DateTime faturatarihi = (DateTime)entity.Attributes["new_faturatarihi"];
string donem = (string)entity.Attributes["new_donem"];
entity["new_marka"] = marka[i];
entity.Attributes["new_tutar"] = tutar;
entity.Attributes["new_faturano"] = faturano;
entity.Attributes["new_faturatarihi"] = faturatarihi;
entity.Attributes["new_donem"] = donem;
service.Create(entity);
}
}
#endregion
}
catch (Exception ex)
{
Exception e = ex.InnerException != null ? ex.InnerException : ex;
throw new InvalidPluginExecutionException(string.Format("ERROR: message:{0}", e.Message));
}