Загружаю данные используя Integration Services из внешнего источника данных (база Access) в MS CRM 2011
код добавляет новых контрагентов в CRM, а в случае, если контрагент уже такой есть, то обновляет его
пока нарисовался следующий код:
X++:
/* Microsoft SQL Server Integration Services Script Component
* Write scripts using Microsoft Visual C# 2008.
* ScriptMain is the entry point class of the script.*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using SC_09d18353d43c4f97ba6222b40aa3ff97.csproj.CRMSDK1;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
// Объяляем Crm Service
public CrmService myservice = null;
public override void PreExecute()
{
base.PreExecute();
// Настраиваем Crm Service
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "formula-t";
myservice = new CrmService();
myservice.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx";
myservice.CrmAuthenticationTokenValue = token;
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
}
public override void PostExecute()
{
base.PostExecute();
/*
Add your code here for postprocessing or remove if not needed
You can set read/write variables here, for example:
Variables.MyIntVar = 100
*/
}
public override void 0_ProcessInputRow(0Buffer Row)
{
// Create the ColumnSet that indicates the properties to be retrieved.
ColumnSet cols = new ColumnSet();
// Set the properties of the ColumnSet.
cols.Attributes = new string[] { "accountid"}; //выходной столбец
// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();
// Set the condition for the retrieval to be when the contact's address' city is Sammamish.
condition.AttributeName = "accountnumber"; //искать будем по коду справочника
condition.Operator = ConditionOperator.Like;
condition.Values = new string[] { Row. };
// Create the FilterExpression.
FilterExpression filter = new FilterExpression();
// Set the properties of the filter.
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] { condition };
// Create the QueryExpression object.
QueryExpression query = new QueryExpression();
// Set the properties of the QueryExpression object.
query.EntityName = EntityName.account.ToString();
query.ColumnSet = cols;
query.Criteria = filter;
// Retrieve the contacts.
BusinessEntityCollection FilterAccounts = myservice.RetrieveMultiple(query);
if (FilterAccounts.BusinessEntities.Length == 0) //если записи не найдены
{
account account1 = new account();
account1.accountnumber = Row.;
if (!Row._IsNull) {account1.name = Row.;}
myservice.Create(account1);
}
else
{
foreach (account Acount1 in FilterAccounts.BusinessEntities)//Если запись с таким кодом уже существует, то посмотрим - а может ее надо обновить ?
{
account account2 = new account();
account2.accountid = Acount1.accountid;
int Upd = 0; //будем апдейтить только если есть что изменять//хотя наверняка есть более лучшие способы узнать о изменениях
if (account2.name == Row.) { account2.name = Row.; Upd = 1; }// ура есть что изменять
if (Upd == 1)
{
myservice.Update(account2);
}
}
}
}
}
но этот код мне кажется крайне неоптимальным
как его улучшить ?