3

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;
    }
}
F Nadeem
  • 93
  • 9
  • That's strange, you should get that read error maybe if you're trying to update fields from a related entity where your user doesn't have access. If you try updating the records without using the context (i.e. OrganizationService.Update(account) does the same happen? And if you try with ExecuteMultiple? – Jordi Jun 04 '16 at 18:40
  • Hi Jordi and thanks for your reply. If I comment out the O rganizationService.Update(account) call I dont get any excetions but my updates also dont get committed to the CRM. So things like : currAcc.cir_MediaOwner = true; don't work. – F Nadeem Jun 06 '16 at 10:19
  • Meant to say to replace the context.UpdateObject(currAcc); by a service.Update(account) where account = new Account() {Id = new Guid("the id"), cir_MediaOwner = true, ... } and so on. – Jordi Jun 06 '16 at 19:56
  • It looks like you're only trying to commit the changes every 20 records. I'm wondering if it is performing this using a `Bulk Update` in the background - does your user have permission to do bulk updates? – jasonscript Jul 21 '16 at 02:17

1 Answers1

0

It is possible that you have either a Workflow or Plugin Step related to account and create/update message, impersonated with an User without the proper privileges.

For workflows, these could have the option "execute as: the owner of the workflow" (link)

For plug-in steps, these have an option "Run in User's Context" that can be set to a fix user (link)