Hi ,
I am trying to set the value to blank field by a another field
Field1 (blank) (currency data type)
Field2 (some records are blank) (data type text but contain decimal )
Requirement Field1 = Field2
Code
/*******************************************************************************
var crmService = CRMHelperClass.GetCRMService();
QueryExpression qe = new QueryExpression();
qe.EntityName = "opportunity";
qe.ColumnSet = new ColumnSet();
//qe.ColumnSet.AllColumns = true;
// qe.ColumnSet.Columns.Add("new_field1");
// qe.ColumnSet.Columns.Add("new_field2");
// qe.ColumnSet = new ColumnSet(new string[] { "new_field1", "new_field2" });
// 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) // I am not getting new_field2 , is it due to its blank value
{
if (r.Contains("new_field2"))
{
string k1 = r.Attributes["new_field2"].ToString();
decimal k2 = Convert.ToDecimal(k1);
if (k2 > 0)
{
Entity updated = new Entity();
updated.Id = r.Id;
updated.LogicalName = r.LogicalName;
updated.Attributes["new_field2"] = new Money(k2);
crmService.Update(updated); // Not getting error
}
/*****************************************************************************************
Any suggestion
*This post is locked for comments
Hi
Check new_field1,new_field2 has values. If there is no data in this fields then you will not get this fields in retrieve result set.
there was some error in code i corrected but also not working
var crmService = CRMHelperClass.GetCRMService();
QueryExpression qe = new QueryExpression();
qe.EntityName = "opportunity";
qe.ColumnSet = new ColumnSet();
qe.ColumnSet = new ColumnSet(new string[] { "new_field1", "new_field2" });
// 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) // I am not getting new_field2 , is it due to its blank value
{
if (r.Contains("new_field1")) // here it was field2
{
string k1 = r.Attributes["new_field2"].ToString();
decimal k2 = Convert.ToDecimal(k1);
if (k2 > 0)
{
Entity updated = new Entity();
updated.Id = r.Id;
updated.LogicalName = r.LogicalName;
updated.Attributes["new_field2"] = new Money(k2);
crmService.Update(updated); // Not getting error
}
Hi
The reason is, You have not specified the columns to be retrieve. Uncomment the below line in your code and try to run your code. If you have value in "new_field2" field then definitely you will get it.
//qe.ColumnSet.AllColumns = true;
Yes new_field1 is dummy field. But is the necessary that If i want to set value to a blank field then I need to get it in EntityCollection
Answer is here:
community.dynamics.com/.../237886
you need to check if attribute exits. if not, then set decimal value field to null or zero, whatever you prefer to show.
in code sample above I do not see that you use new_field1 attribute. is this by purpose?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156