Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How do you test a date field for null value in a plug-in using c#

Posted on by Microsoft Employee

I am trying to test a date field to see if its null or has no value but no luck.

The field is blank on the form.

If I use  == null  it fails.

When I get the value of the blank date field it returns 1/1/0001 12:00a etc.

I test against the return value as follows:

datefield.ToShortDateString() == "1/1/0001";

it always returns true regardless of the value of the datefield.

I am trying to use the QueryByAttribute method but can't figure out the syntax, I haven't seen any date related examples.

Any help or suggestions would be greatly appreciated.

Thanks

*This post is locked for comments

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How do you test a date field for null value in a plug-in using c#

    For images you should use

    if (!entity.Contains("new_datedead"))

    ...

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How do you test a date field for null value in a plug-in using c#

    Sorry, it was right under my nose all the time.

    I registered the PreImage but still cannot find the datedead field,

    throws Exception: "The given key was not present in the dictionary."

    Code:

    // step is registered as Post-operation

    // Image step is registered as PreImage (name=Image, alias=Image)

    // new_datedead value is null in the database table

    // context

    Entity entity = (Entity)context.PreEntityImages["Image"];

     if ( entity["new_datedead"] == null)  // Throws the exception

     {

     }  

    but if I change to:

    if (entity.GetAttributeValue<DateTime>("new_datedead") == null)  //false, NoException

    {

    }

    else  

    {

       // this gets executed

    }

    Not sure which method should be used and which context to use to do the update of the new_datedead field, regular context or the PreImage.

    Thanks

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How do you test a date field for null value in a plug-in using c#

    Just click with right mouse on step you've already registered and you will have a menu option - register image.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How do you test a date field for null value in a plug-in using c#

    Thanks for the article.

    I was following the steps in the online CRM Book but it looks different than what I have.

    I am on CRM 2013 on-premise and could not find the Image update section it was referring to same as the article.

    Thank You

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How do you test a date field for null value in a plug-in using c#

    Registration of step and image is different. Check this article - msdn.microsoft.com/.../hh237515(v=crm.6).aspx

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How do you test a date field for null value in a plug-in using c#

    In registering the step:

    Selected Pre-operation under Event Pipeline Stage of Execution

    and

    Server was checked under Deployment

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How do you test a date field for null value in a plug-in using c#

    You need pre-image and you have to register it in plugin registration tool. I believe you haven't done that.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How do you test a date field for null value in a plug-in using c#

    I tried both Pre and Post update images as follows:

    // Entity entity = context.PreEntityImages["PreImage"];

    Entity entity = context.PostEntityImages["PostImage"];

    Returned Business Process Error: "The given key was not present in the dictionary"

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How do you test a date field for null value in a plug-in using c#

    Ok that makes sense.

    Which image should I be looking at Pre or Post update?

    thank You

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How do you test a date field for null value in a plug-in using c#

    Field will be available in Target only if you update this attribute on the form. If you haven't touched that field but you want to get its value you can access it through Images.

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans