web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Error checking in C#

(0) ShareShare
ReportReport
Posted on by

what is the easiest way to check if a field value is null or nothing is returned in a collection? Try catch works for one or two fields, but I have like 15 that I need to use to create the sales order entity.

I want to be able to create an order entity even though there's no value and not break in the middle of the code. Is there a function I can use for each field without too much hassel?

Sample code:

This gets the account id by looking up the name of the customer.

var account = GetEntityCollection(_service, "account", "name", customerid, new ColumnSet("accountid", "name", "defaultpricelevelid"));
Guid accountId = (Guid)account[0].Id; 
salesorder["customerid"] = new EntityReference("account", new Guid(accountId.ToString()));

private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string attributeName, string attributeValue, ColumnSet cols)
{
QueryExpression query = new QueryExpression
{
EntityName = entityName,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = attributeName,
Operator = ConditionOperator.Equal,
Values = { attributeValue }
}
}
}
};
return service.RetrieveMultiple(query);
}


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    tpeschat Profile Picture
    4,930 on at
    RE: Error checking in C#

    Hi,

    you could add if conditions for each field, like

    If (entity.attributes.contains("fieldxxx"))

    {Your code}

    It would be cumbersome, but at least you prevent this try catch and rollback issue.

    Or you create a function for that if condition plus action and you work with variables.

    Hth Thomas

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Error checking in C#

    Hello,

    The best way is usage Contains method of Entity object like:

    var contact = ;//Code where you fetch contact record

    if (contact.Contains("your field name here")){

    //Your code that will run if record has field populated

    }

  • KZee Profile Picture
    on at
    RE: Error checking in C#

    You can also check if any results have been returned by GetEntityCollection function call to avoid an exception at line (Guid)account[0].Id;

    var account = GetEntityCollection(_service, "account", "name", customerid, new ColumnSet("accountid", "name", "defaultpricelevelid"));

    if (account.Entities.Any())

    {

    Guid accountId = (Guid)account[0].Id;

    salesorder["customerid"] = new EntityReference("account", new Guid(accountId.ToString()));

    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans