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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    you can check for minimum date

    if(datefield != DateTime.MinValue)

    {

     //write your code.

    }

    DateTime.MinValue =  1/1/0001 12:00a

    please check

    msdn.microsoft.com/.../system.datetime.minvalue(v=vs.110).aspx

  • Community Member Profile Picture
    on at

    Hi Mahadeo,

    Thank you for the reply.

    After thorough testing,

    Results: Did not work

    I tried this:

    pre-condition:

     datefield value: 09/20/2016

    Condition:

      if (datefield == DateTime.MinValue);       //  update only if null

    or

      if (datefield.Equals(DateTime.MinValue) ) // as suggested by the article

    in both cases the datefield got updated even if it had some value other than MinValue

    Any other suggestions?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Actually you don't need to compare that field with any kind of values. When field is updated with null inside plugin you can use following check assuming that target is your entity you got from context:

    var entity = context.InputParameters["Target"] as Entity;

    if (entity.Contains("new_fieldnamehere") && entity["new_fieldnamehere"] == null)

    {

    //put your logic of handling null values here

    }

  • Suggested answer
    Community Member Profile Picture
    on at

    If you are using late bound then you need to check in Entity contains key, like:

    if(entity.Contais("datefield") && entity.GetAttributeValue<Datetime?>("datefield")!=null){

    }

  • Community Member Profile Picture
    on at

    Hi Guido,

    Here is the test result:

    before:

     DateField value: null

    code:

       DateTime? DateField = entity.GetAttributeValue<DateTime?>("new_datefield");

       if (DateField == null)

          entity["new_datefield"] = new DateTime(2017,3,1);

    result: Fail - datefield value changed to 1/3/2017

    Thank You,

    next step: going to try other suggestions as well

  • Community Member Profile Picture
    on at

    Hello Andrii,

    Thank you for your reply.

    Here is the test below:

    // throw new InvalidPluginExecutionException("DateDead: "       +entity.GetAttributeValue<DateTime>("new_datedead"));   // returns 1/1/0001 12:00:00a

    Entity entity = (Entity)context.InputParameters["Target"];          

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

       entity["new_datedead"] = DateTime.Now;    // does not get executed

    }

    results: Fail - does not get passed the if statement

    also tried:

    if (entity.Attributes.ContainsKey ("new_datedead"))     // did not pass either

    The weird part is I can read and update but cannot find the attribute

    Thanks

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • Community Member Profile Picture
    on at

    Ok that makes sense.

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

    thank You

  • Community Member Profile Picture
    on at

    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"

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans