Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Cast exception in Updation of option set value

Posted on by Microsoft Employee

Scenario: I am retriving all the records from an entity with a condition and tring to update a value of my option set to another value, if my condition satisfies.
For this I wrote the below code and I am getting an error "system.invalidcastexception.Microsoft Dynamics CRM has experienced an error". Can any tellme where I am exactly missing?


private void CheckIfInworkActions(ICrmContext context,Guid userId)
        {
        
            QueryExpression exe1 = new QueryExpression(avd_masteraction.EntityLogicalName);

            exe1.ColumnSet.AddColumn(XrmAttributes.MasterAction.Status);
          
            exe1.Criteria.AddCondition(XrmAttributes.StatusCode, ConditionOperator.Equal, 100000004);
          
            var coll1 = context.OrganizationService.RetrieveMultiple(exe1);

            foreach (Entity e in coll1.Entities)
          
            {
                if (coll1 != null && coll1.Entities != null && coll1.Entities.Count > 0)
               
                {
                    OptionSetValue myOptionSet = new OptionSetValue();
                    myOptionSet.Value = 100000011;                            

                    e.Attributes[XrmAttributes.StatusCode] = myOptionSet.Value;
                  
                    context.OrganizationService.Update(e);

                }
               

            }
           
        }

*This post is locked for comments

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Cast exception in Updation of option set value

    Hi,

    I think you solved this issue in community.dynamics.com/.../212922

    right?

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Cast exception in Updation of option set value

    run this plugin in asynchronous or synchronous mode either option ....for updating status reason it works fine  

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Cast exception in Updation of option set value

    Hi,

    Exactly in which line you are getting error?

    You are talking about Status Code. Is this Default field or custom field?

    If it is default field then you can't assign 100000011 Value to it right?

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

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans