Above, you can see what i want to do. I wrote an html web resource to select child firms. After selection, the selected firms goes to the multiline text fields. When user presses the save (Kaydet) button, the form saves the firms one by one to the database table. I mean if 3 firm were selected, 3 rows should be added to the dadatabes table. The fatura no, fatura tarihi etc. are the same. And also the tutar value should be divided by the number of selected firms. The divided tutar should be saved to the dataase table row for each selected firms. Besides the MaÄŸaza field on the form is a lookup field and the marka field is an option set field.
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;
using System.ServiceModel;
using Microsoft.Xrm;
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)
{
// Sandbox içerisinde calisan plug-in’ler TracingService’den yararlanabilirler.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
//Context’i elde ediyoruz.
IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));
// CRM Servisi elde ediyoruz
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity SelectedFirms = new Entity("new_destek_yerelreklam_detay");
// InputParameters’dan gelen verileri aliyoruz
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Target ile entity’e erisiyoruz.
Entity entity = (Entity)context.InputParameters["Target"];
// Beklediginiz entity geldi mi diye kontrol ediyoruz.
if (entity.LogicalName != "new_destek_yerelreklam_detay") return;
if (entity.Contains("new_magaza")) return;
if(entity.LogicalName == "new_destek_yerelreklam_detay")
{
try
{
if (context.MessageName == "Create")
{
#region seçilen magazaların id lerini al
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();
}
Guid[] magazaId = Array.ConvertAll(id, Guid.Parse);
#endregion
#region markalar
//The brands (mark numbers) of the firms
string markalar = entity.Contains("new_markalar") ? entity["new_markalar"].ToString() : "";//string from the multitext
string[] marka = markalar.Split(',');
int[] mrk = new int[marka.Length];
for (int j = 0; j < marka.Length; j++)
{
marka[j] = marka[j].Trim();//add each mark number to string array
}
//string array to integer array for mark numbers
int[] myInts = Array.ConvertAll(marka, int.Parse);
#endregion
#region Gelen deÄŸerleri ata
//new_magaza alanı yoksa oluştur
if (entity.Attributes.Contains("new_magaza") == false)
{
Guid mu = new Guid();
mu = Guid.Empty;
for (int i = 0; i < id.Length; i++)//It loops in new_multiline textare according to thenumber of firmsr
{
decimal tutar = (entity["new_tutar"] as Money).Value;//money value
tutar = tutar / id.Length;
string faturano = (string)entity.Attributes["new_faturano"];
DateTime faturatarihi = (DateTime)entity.Attributes["new_faturatarihi"];
OptionSetValue donem = (OptionSetValue)entity.Attributes["new_donem"];
OptionSetValue calismaTuru = (OptionSetValue)entity.Attributes["new_calismaturu"];
OptionSetValue myOptionSet = new OptionSetValue();
myOptionSet.Value = myInts[i];
entity.Attributes["new_marka"] = myOptionSet;
OptionSetValue Firmamarkasi = (OptionSetValue)entity.Attributes["new_marka"];
SelectedFirms.Attributes["new_marka"] = Firmamarkasi;//the integer mark number of the firm
/* SelectedFirms.Attributes["new_magaza"] = entity.Attributes["new_magaza"];*/// Guid id of firm productId
SelectedFirms.Attributes["new_magaza"] = magazaId[i];//the guid of the selected firm.
SelectedFirms["new_tutar"] = new Money(tutar);//money
SelectedFirms.Attributes["new_faturano"] = faturano;//text
SelectedFirms.Attributes["new_faturatarihi"] = faturatarihi;//datetime
SelectedFirms.Attributes["new_donem"] = donem;//Option set
SelectedFirms.Attributes["new_calismaturu"] = calismaTuru;//Option set
SelectedFirms.Attributes["statecode"] = 0;//integer
SelectedFirms.Attributes["new_ids"] = "";//the ids ffrom checked firms from the html web resource should be null. In database table this field should be null
SelectedFirms.Attributes["new_markalar"] = "";//the mark numbers comes from the html web resource
SelectedFirms.Attributes["new_multiline"] = "";//the child firm names comes from the html web resource
//the ids,markalar and multilen textfield should be saved as null to the database
service.Create(SelectedFirms);
}
}
#endregion
}
}
catch (Exception ex) { tracingService.Trace("MyPlugin: {0}", ex.ToString()); throw; }
}
}
}
}
}