Can someone help with my if, else statement?


  • I want to run a block of code but only if my lookup field has data; I have the following line:

    EntityReference a = (EntityReference)entity.Attributes["new_lead"];

    if (a != null){

    //Do the code


    It's not working - anyone can give some advise?

    Wednesday, January 08, 2014 11:13 PM

All replies

  • Hello,

    Try to use following code snippet:

    EntityReference a = entity.GetAttributeValue<EntityReference>("new_lead");

    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Wednesday, January 08, 2014 11:28 PM
  • you can check the null condition before the EntityReference cast:

    if (entity.Contains("new_lead") && entity["new_lead"] != null)
       EntityReference a = (EntityReference)entity["new_lead"];
       // Do the code

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Wednesday, January 08, 2014 11:41 PM