I have looked around a lot of MS Dynamics CRM blogs and SO questions and tried all the solutions but they haven't worked.
The problem I am facing is as follows: I am trying to loop through an excel file with company names and company type. I then find company with matching names in CRM and set values for some custom fields depending on the company type in excel. When I do this though code for a single company it works fine however when I try to run a loop for a large number of companies I am constantly getting:
SecLib::CrmCheckPrivilege failed. Returned hr = -2147220943 on UserId: xxxxxx and PrivilegeType: Read.
The user in question has all privilleges and is an admin user with Read/Write CAL. Also like I pointed out I am able to do the updates for a single record. But when run as a loop that very same record throws an error.
I have been unable to find a solution for this so any help would be much appreciated. I am using MS Dynamics CRM online.
Here's my code:
while (!parser.EndOfData)
{
counter++;
if (counter >= 20)
{
try
{
counter = 0;
context.SaveChanges();
}
catch (Exception exc)
{
while (exc != null)
{
System.Diagnostics.Debug.WriteLine("ERROR: " + exc.Message);
exc = exc.InnerException;
}
continue;
}
}
//Processing row
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
//TODO: Process field
company.Add(fields[0]);
category.Add(fields[1]);
var currAccs = context.CreateQuery<Account>().Where(x => x.Name.Contains(fields[0])).ToList();
if (currAccs != null)
{
foreach (var currAcc in currAccs)
{
System.Diagnostics.Debug.WriteLine("Processing: " + currAcc.Name);
currAcc.cir_MediaOwner = false;
currAcc.cir_Agency = false;
currAcc.cir_AdvertiserBrand = false;
if (fields[1].Contains("MO"))
{
currAcc.cir_MediaOwner = true;
}
if (fields[1].Contains("A"))
{
currAcc.cir_Agency = true;
}
if (fields[1].Contains("B"))
{
currAcc.cir_AdvertiserBrand = true;
}
try
{
context.UpdateObject(currAcc);
}
catch (Exception exc)
{
while (exc != null)
{
System.Diagnostics.Debug.WriteLine("ERROR: " + exc.Message);
exc = exc.InnerException;
}
continue;
}
}
}
break;
}
}