locked
CRM 2011 C# Plugin: IF Statement for Bit (Two Options Field) RRS feed

  • Question

  • Hello, I am having difficulties creating an IF statement for a bit (two option) field.

    My current syntax is not working, please help!

    if (targetEntity.Contains("apd_twooptionsfield") && targetEntity.GetAttributeValue<bool>("apd_twooptionsfield") == true)
    {}
    

    In VS2010 I do not have any errors but when I run the code in CRM, it will not step into the if statement and the code does not fire.


    Mike Karls

    Monday, June 30, 2014 3:32 PM

Answers

  • I ended up using this:

    var Trigger = targetEntity.GetAttributeValue<bool?>("apd_twooptionsfield").GetValueOrDefault();
    if(Trigger == true)


    Mike Karls

    • Marked as answer by Mike Karls Tuesday, July 8, 2014 4:54 PM
    Tuesday, July 8, 2014 4:54 PM

All replies

  • the code looks ok, if it doesn't step into the if statement or the entity doesn't contain the field or the field is not true.

    try to debug it


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

    Monday, June 30, 2014 5:33 PM
  • Hi Mike,

    you have to change you code like this

    if (targetEntity.Contains("apd_twooptionsfield")

    {

          if(targetEntity.GetAttributeValue<bool>("apd_twooptionsfield") == true)
          {}

    }

    In one line if you put these 2 conditions it will fail because if will check second condition if first is false also.

    Tuesday, July 1, 2014 7:26 AM
  • I tried your code Suresh but I still cannot get it to work. If I take out the If statements the code works fine, but if I add them nothing happens. The plugin does not fire.

    I am trying to have an email be created if the field "apd_twooptionsfield" is set to True so that my email is only created when the user wants it to.

    I have confirmed I have the correct spelling of the field, that it is a bit field and that I have the field selected. Not sure what I am doing wrong....Here is an updated piece of the code

                    if (targetEntity.Contains("apd_twooptionsfield"))
                    {
                        if (targetEntity.GetAttributeValue<bool>("apd_twooptionsfield") == true)
                        {
                                               
                            Entity email = new Entity();
                            email.LogicalName = "email";
    }}
    


    Mike Karls

    Wednesday, July 2, 2014 2:54 PM
  • Can you also try:-

    bool oBool= (bool)targetEntity.Entities[0]["apd_twooptionsfield"];

    //now check

    if (oBool == true){}


    Regards Faisal

    Wednesday, July 2, 2014 3:16 PM
  • Faisal, in your piece of code I get an error when using "Entities" after targetEntity.....do I need a specific assembly reference for this?

    Mike Karls

    Wednesday, July 2, 2014 3:20 PM
  • I think you earlier code is correct. My code will work if you are retreiving entity. Just again check the schema name of field try a copy paste


    Regards Faisal

    Wednesday, July 2, 2014 3:34 PM
  • I ended up using this:

    var Trigger = targetEntity.GetAttributeValue<bool?>("apd_twooptionsfield").GetValueOrDefault();
    if(Trigger == true)


    Mike Karls

    • Marked as answer by Mike Karls Tuesday, July 8, 2014 4:54 PM
    Tuesday, July 8, 2014 4:54 PM