I am using SSIS for data migration (create and update) into CRM 2013 database from another database.
I am facing a challenge with writing code to get the guid records in lookup fields. For example, I have two lookup fields on the form of a custom entity (gems_accountauditor). One lookup field looks into account entity and the other looks into a custom entity (gems_auditor).
I wrote the code below using entityreference for the account lookup field. Records are created when I run the SSIS package but only the first record from the source database has its account lookup field containing data. All the other records (a little over 100) have their account lookup field empty even though all those accounts exist in crm.
What am I missing in the code below? Is there a better way of having all the records getting their lookup fields to contain data?
if (!Row.listedCompany_IsNull)
{
String cNumber = Row.listedCompany.ToString();
QueryExpression qe = new QueryExpression();
ColumnSet cs = new ColumnSet();
FilterExpression fe = new FilterExpression();
ConditionExpression ce = new ConditionExpression();
cs.AddColumns("accountid");
ce.AttributeName = "gems_listedcomp";
ce.Operator = ConditionOperator.Equal;
ce.Values.Add(cNumber);
fe.AddCondition(ce);
qe.ColumnSet = cs;
qe.Criteria = fe;
qe.EntityName = "account";
EntityCollection result = organizationservice.RetrieveMultiple(qe);
foreach (Entity act in result.Entities)
{
string value = act.Attributes["accountid"].ToString();
Guid Value = new Guid(value);
EntityReference Account = new EntityReference("account", Value);
newAccountAuditor["gems_account"] = Account;
}
}
The second lookup field has it's own problem. I tweaked the code above to suit it's requirement but when I run the SSIS package, this particular field throws a runtime error ("An Unexpected error occured"). What could be the problem?
if (!Row.listedCompany_IsNull)
{
String cNumber = Row.listedCompany.ToString();
QueryExpression qe = new QueryExpression();
ColumnSet cs = new ColumnSet();
FilterExpression fe = new FilterExpression();
ConditionExpression ce = new ConditionExpression();
cs.AddColumns("accountid");
ce.AttributeName = "gems_listedcomp";
ce.Operator = ConditionOperator.Equal;
ce.Values.Add(cNumber);
fe.AddCondition(ce);
qe.ColumnSet = cs;
qe.Criteria = fe;
qe.EntityName = "account";
EntityCollection result = organizationservice.RetrieveMultiple(qe);
foreach (Entity act in result.Entities)
{
string value = act.Attributes["accountid"].ToString();
Guid Value = new Guid(value);
EntityReference Account = new EntityReference("account", Value);
newAccountAuditor["gems_account"] = Account;
}
}