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)

Plugin - Check if attribute is NULL or EMPTY

(0) ShareShare
ReportReport
Posted on by

Hello,

I have this plugin code for CRM 4 which checks if field is null or empty:

public static bool CrmNullOrEmpty<T>(T attribute) where T : class

{
bool isNull = true;

if (attribute!= null)
{
if (attribute is  CrmDateTime )
{
CrmDateTime crmDateTime = attribute as CrmDateTime;
isNull = crmDateTime.IsNull || crmDateTime.IsNullSpecified;
}
else if (attribute is Picklist)
{
Picklist sPicklist = attribute as Picklist;
isNull = sPicklist.IsNull || sPicklist.IsNullSpecified;
}
else if (attribute is Key)
{
Key key= attribute as Key;
isNull = key.Value == Guid.Empty;
}
.....//all other types

}

return isNull;

}

Plugin


EntityReference primarycontact = (EntityReference)entity["primarycontactid"];

if (!CrmNullOrEmpty(primarycontact))
  //do something


How would you do this in CRM 2011?


Thank you.



*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Surmeet Singh Profile Picture
    3,075 on at

    Hi  MIHAILO,

    In CRM 2011, you just have to use

    if(entity.Contains("primarycontactid")==false)

    This piece of code is sufficient to detect that field is empty in case of false.

  • Community Member Profile Picture
    on at

    Hi Surmeet,

    Thank you  for your reply.

    I am familiar with that approach, but I am trying to find a way to check if attribute is NULL without using Entity.

    The reason is, I have lots of function where I only pass attributes, not the whole entities.

    Thank you

    Mihailo

  • Verified answer
    Surmeet Singh Profile Picture
    3,075 on at

    Hi Mihailo,

    You can then use

    string n=entity.GetAttributeValue<string>("fieldname");

    if(n==null)

    {

    //your operation

    }

  • Soma Shekar Profile Picture
    461 on at

    if(entity.attributes.contains("primarycontactid"))

    {

    //do something

    }

  • Community Member Profile Picture
    on at

    This is probably the way to do it.

    Thank you

    Mihailo

  • JackofallTrades Profile Picture
    110 on at

    This helped me to check if (entity.Contains("new_account_id") == false).  if (entity.Contains("new_account_id") != null) does not work.

    Thank you Mr Singh.

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Jack  

    Entity accountentity = service.Retrieve("account", accountId, new ColumnSet(true));

                   if (accountentity.Attributes.Contains("new_account_id")&& accountentity.Attributes["new_account_id"]!=null)

          {    

          }

    Thanks

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