Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Not able to set the blank field of CRM using console application

Posted on by 435

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

  • Suggested answer
    Meena Kannan Profile Picture
    Meena Kannan 230 on at
    RE: Not able to set the blank field of CRM using console application

    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.

  • windyMill Profile Picture
    windyMill 435 on at
    RE: Not able to set the blank field of CRM using console application

    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

    }

  • Suggested answer
    Meena Kannan Profile Picture
    Meena Kannan 230 on at
    RE: Not able to set the blank field of CRM using console application

    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;

  • windyMill Profile Picture
    windyMill 435 on at
    RE: Not able to set the blank field of CRM using console application

    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

  • Suggested answer
    Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 on at
    RE: Not able to set the blank field of CRM using console application

    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?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans