0
votes

I've following code snippet that is grabbing all active accounts from CRM and then updating one of the hidden field for each account. There are around 45,000 active accounts resides in CRM and that is why I am using paging and grabbing 5000 records from each page. It start updating records from page 1 and grab first 5000 records but hang on 3100th account and don't move further. I am here for seeking your kind advise/suggestions to do the needful. TIA!!!

string temp1 = string.Empty;
QueryExpression qe = new QueryExpression
{
    EntityName = "account",
  ColumnSet = new ColumnSet("name", "new_latestpolicydate","new_latestpolicydate_hidden"),
    Criteria = new FilterExpression
    {
        Conditions = { 
            new ConditionExpression("statecode",ConditionOperator.Equal,0)
        }
    }
};

qe.PageInfo = new PagingInfo();
qe.PageInfo.Count = 5000;
qe.PageInfo.PageNumber = 1;
qe.PageInfo.PagingCookie = null;

while (true)
{
    EntityCollection ec = service.RetrieveMultiple(qe);

Console.WriteLine("Page no:\t{0}\nTotal Active Accounts:\t{1}",qe.PageInfo.PageNumber, ec.Entities.Count);
        foreach (Entity item in ec.Entities)
        {
            if (item.Attributes.Contains("new_latestpolicydate"))
            {

                DateTime date;
                date = Convert.ToDateTime(item.Attributes["new_latestpolicydate"]);
                temp1 = date.Date.ToString("d");
                item.Attributes["new_latestpolicydate_hidden"] = temp1;
            }
            if (temp1 != string.Empty)
            {
                service.Update(item);
                Console.Write("\r{0} are accounts updated", i++);
            }
        }
        if (ec.MoreRecords)
        {
            qe.PageInfo.PageNumber++;
            qe.PageInfo.PagingCookie = ec.PagingCookie;
        }
        else
        { break; }
    }
1

1 Answers

0
votes

Your code looks good. I have 2 suggestions:

  1. Try to rewrite code without trying to update record immediately. Try to retrieve all records and only after update all data.
  2. For such tasks I can suggest to use SSIS - it's much more easier. KingswaySoft adapter is free for such kind of operations.