Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Not able to update the attribute while looping through entity collection

(0) ShareShare
ReportReport
Posted on by 435

Hi All, 

I have written a service to update the attributes while looping through the entity collection . 
can you please tell me the issue.

static void Main(string[] args)
{
var crmService = CRMHelperClass.GetCRMService();


QueryExpression qe = new QueryExpression();
qe.EntityName = "opportunity";
qe.ColumnSet = new ColumnSet();
// qe.ColumnSet.Columns.Add("xxx_estdateoffirstinvoice");
qe.ColumnSet.AllColumns = true;
// qe.ColumnSet.Columns.Add("xxx_calculatedrevenue");


// Condition where task attribute equals account id.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "statecode";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(0);
qe.Criteria.AddCondition(condition);

EntityCollection ec = crmService.RetrieveMultiple(qe);

foreach (Entity r in ec.Entities)
{
if (r.Attributes.Contains("xxx_estdateoffirstinvoice") && r.Attributes.Contains("xxx_calculatedrevenue"))
{
if (Convert.ToDateTime(r.Attributes["xxx_estdateoffirstinvoice"]) <= DateTime.Now)
{
r.Attributes["new_calculatedrevenue"] = "0";

crmService.Update(r);   -- getting error here 
}
}


};

}
}
}

static void Main(string[] args)
{
var crmService = CRMHelperClass.GetCRMService();


QueryExpression qe = new QueryExpression();
qe.EntityName = "opportunity";
qe.ColumnSet = new ColumnSet();
// qe.ColumnSet.Columns.Add("xxx_estdateoffirstinvoice");
qe.ColumnSet.AllColumns = true;
// qe.ColumnSet.Columns.Add("xxx_calculatedrevenue");


// Condition where task attribute equals account id.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "statecode";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(0);
qe.Criteria.AddCondition(condition);

EntityCollection ec = crmService.RetrieveMultiple(qe);

foreach (Entity r in ec.Entities)
{
if (r.Attributes.Contains("xxx_estdateoffirstinvoice") && r.Attributes.Contains("xxx_calculatedrevenue"))
{
if (Convert.ToDateTime(r.Attributes["xxx_estdateoffirstinvoice"]) <= DateTime.Now)
{
r.Attributes["xxx_calculatedrevenue"] = "0";

crmService.Update(r);
}
}


};

}
}
}

static void Main(string[] args)
{
var crmService = CRMHelperClass.GetCRMService();


QueryExpression qe = new QueryExpression();
qe.EntityName = "opportunity";
qe.ColumnSet = new ColumnSet();
// qe.ColumnSet.Columns.Add("angus_estdateoffirstinvoice");
qe.ColumnSet.AllColumns = true;
// qe.ColumnSet.Columns.Add("new_calculatedrevenue");


// Condition where task attribute equals account id.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "statecode";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(0);
qe.Criteria.AddCondition(condition);

EntityCollection ec = crmService.RetrieveMultiple(qe);

foreach (Entity r in ec.Entities)
{
if (r.Attributes.Contains("angus_estdateoffirstinvoice") && r.Attributes.Contains("new_calculatedrevenue"))
{
if (Convert.ToDateTime(r.Attributes["angus_estdateoffirstinvoice"]) <= DateTime.Now)
{
r.Attributes["new_calculatedrevenue"] = "0";

crmService.Update(r);
}
}


};

}
}
}

static void Main(string[] args)
{
var crmService = CRMHelperClass.GetCRMService();


QueryExpression qe = new QueryExpression();
qe.EntityName = "opportunity";
qe.ColumnSet = new ColumnSet();
// qe.ColumnSet.Columns.Add("angus_estdateoffirstinvoice");
qe.ColumnSet.AllColumns = true;
// qe.ColumnSet.Columns.Add("new_calculatedrevenue");


// Condition where task attribute equals account id.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "statecode";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(0);
qe.Criteria.AddCondition(condition);

EntityCollection ec = crmService.RetrieveMultiple(qe);

foreach (Entity r in ec.Entities)
{
if (r.Attributes.Contains("angus_estdateoffirstinvoice") && r.Attributes.Contains("new_calculatedrevenue"))
{
if (Convert.ToDateTime(r.Attributes["angus_estdateoffirstinvoice"]) <= DateTime.Now)
{
r.Attributes["new_calculatedrevenue"] = "0";

crmService.Update(r);
}
}


};

}
}
}4810.crmerror.PNG

*This post is locked for comments

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,401 on at
    RE: Not able to update the attribute while looping through entity collection

    Hi

    You have lots of Main methods in your first post.

    So, Please copy the one you are using currently and copy the full error.

  • Andreas Cieslik Profile Picture
    Andreas Cieslik 9,267 on at
    RE: Not able to update the attribute while looping through entity collection

    Hi WindyMill,

    can you please provide us the detailed error message?

    Cheers,

    Andreas

  • windyMill Profile Picture
    windyMill 435 on at
    RE: Not able to update the attribute while looping through entity collection

    Hi Gopalan,

    By doing so the privillege error gone . But another error is coming

    An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in CrmServiceRevenue

    Can you please suggest on this ?

    Thanks

  • Andreas Cieslik Profile Picture
    Andreas Cieslik 9,267 on at
    RE: Not able to update the attribute while looping through entity collection

    Hi WindyMill,

    please click on "Copy exception detail to the clipboard..." when the error raises and provide us the content.

    This might help us find the main reason behind this read permission error.

    Thanks!

    Andreas

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Not able to update the attribute while looping through entity collection

    I don't think that kind of update by itself can cause a problem (no matter which fields are included). Besides, it's "read" permission that's missing. In general, you should do the update differently:

    var entity = new Entity(..);

    entity.Id = ..

    entity[".."] = <new value>;

    service.Update(entity);

    But it is not, necessarily, the problem. Somewhere somehow there is a missing read permission.. "Update" itself won't cause that error - more likely, you have another workflow/plugin that encounters that issue. There can still be more details to that exception if you click "copy exception details to the clipboard" link on your screenshot

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Not able to update the attribute while looping through entity collection

    As per the previous post, the fundamental problem is that you're retrieving all columns. This means that the Entity instance will include system attributes that are not ValidForUpdate (e.g. the createdby attribute), so when you call the Update it will fail. It's your responsibility to only include the attributes that you want to update in the Entity instance that you pass to the Update method

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,401 on at
    RE: Not able to update the attribute while looping through entity collection

    Hi

    The error indicates missing privilege. I am not sure, as you are saying that you are in System Administrator role.

    Do not retrieve all the columns (will take more time).

    Retrieve only the needed columns (as in the following line and remove the line qe.ColumnSet.AllColumns = true;).

    qe.ColumnSet = new ColumnSet(new string[]{ "angus_estdateoffirstinvoice", "new_calculatedrevenue", "angus_estdateoffirstinvoice" });

    As you are having all the columns (with values) in the retrieved record, you would be updating all of them (with the same value, but this would trigger processes unnecessarily).

    Try again with the above changes (hope new_calculatedrevenue is single line of text data type):

    r.Attributes["new_calculatedrevenue"] = "0";

  • windyMill Profile Picture
    windyMill 435 on at
    RE: Not able to update the attribute while looping through entity collection

    Hi Andreas,

    User has System Admin role and it has all rights.

    Thanks

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Not able to update the attribute while looping through entity collection

    Hi,

     could you paste exception details here (copy exception detail to the clipboard, paste them here?)

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: Not able to update the attribute while looping through entity collection

    Hi Windy Mill,

    Please refer the link below to find resolution for this issue.

    support.microsoft.com/.../after-upgrading-to-microsoft-dynamics-crm-2011,-an-error-occurs-the-logged-on-user-does-not-have-the-appropriate-security-permissions-to-view-these-records-or-perform-the-specific-action

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans