Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

OptionSetValue(Convert.ToInt32()) Not Updating CRM 2016

Posted on by

I am trying to update CRM 2016 via an ASP.Net MVC 4 portal using late binding. I have an Option Set field that i am trying to update using the OptionSetValue(Convert.ToInt32()) but the field does not update.  Below are code snippets of my model and controller.

 

ViewModel:

public class NominatorOrganizationViewModel

{       

   [Display(Name = "Are you submitting this nomination on behalf of an organization?")]       

   public YesOrNoSelectionOrganization OnBehalfOfOrganizationYesOrNo { get; set; }

 

   [Display(Name = "If yes, Nominating Organization:")]

   public string txtOrganization { get; set; }

 

   [DataType(DataType.MultilineText)]

   [Display(Name = "If yes, describe your position in the organization:")]

   public string txtOrganizationPosition { get; set; }

}

Enum values for Yes/No

public enum YesOrNoSelectionOrganization

{

   Nothing = 0,

   Yes = 1,

   No = 2

}

Controller

This code will not work to set the CRM field value

oasys_nomination["oasys_memberofnominatingorganization"] = new OptionSetValue(Convert.ToInt32(NominationVM.NominatorOrganization.OnBehalfOfOrganizationYesOrNo));

 

To work around setting the field, I can use the below code but I don’t think it’s good coding practice and there should be a perfectly good explanation as to why the above code it’s working, I just can’t figure it out.  The code above is similar to other option sets being set throughout the portal but the 1 particular piece of code mentioned above isn’t updating (no errors, just not updating).

 

if (NominationVM.NominatorOrganization.OnBehalfOfOrganizationYesOrNo == YesOrNoSelectionOrganization.Yes)

{

       oasys_nomination["oasys_memberofnominatingorganization"] = new OptionSetValue(2);

}

else if (NominationVM.NominatorOrganization.OnBehalfOfOrganizationYesOrNo == YesOrNoSelectionOrganization.No)

{

        oasys_nomination["oasys_memberofnominatingorganization"] = new OptionSetValue(1);

}

else

{

        oasys_nomination["oasys_memberofnominatingorganization"] = null;

Any ideas?

*This post is locked for comments

  • Verified answer
    ChrysDW Profile Picture
    ChrysDW on at
    RE: OptionSetValue(Convert.ToInt32()) Not Updating CRM 2016

    Sometimes, it just takes walking away to realize what the problem is. Thank you everyone for the quick replies but it was very simple and I was overlooking the obvious.  My problem was that I was expecting one result but wasn't checking on the other result. Meaning, my enum YesOrNoSelectionOrganization was configured wrong, it was in the wrong order to what we have in CRM. CRM is configured as No = 1 and Yes = 2.

    //Wrong Configuration

    public enum YesOrNoSelectionOrganization

    {

      Nothing = 0,

      Yes = 1,

      No = 2

    }

    //CorrectConfiguration

    public enum YesOrNoSelectionOrganization

    {

      Nothing = 0,

      No = 1,

      Yes = 2

    }

    Thank you again for the quick replies,

    Chrys

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: OptionSetValue(Convert.ToInt32()) Not Updating CRM 2016

    Hi

    oasys_nomination["oasys_memberofnominatingorganization"] = new OptionSetValue(Convert.ToInt32(NominationVM.NominatorOrganization.OnBehalfOfOrganizationYesOrNo));

    Looks correct, for me.

    Did you test the following sets a value to optionset?

    oasys_nomination["oasys_memberofnominatingorganization"] = new OptionSetValue(1);

    and

    oasys_nomination["oasys_memberofnominatingorganization"] = new OptionSetValue(2);

    Please check the value of NominationVM.NominatorOrganization.OnBehalfOfOrganizationYesOrNo, it could be 0, so not setting any value for optionset.

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: OptionSetValue(Convert.ToInt32()) Not Updating CRM 2016

    Hi

    Correction-

    I did not notice that YesOrNoSelectionOrganization is enum.

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans