Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Unable to update an option set field in a business process workflow

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I am trying to update a custom AccountType field on the Opportunity entity to equal the AccountType on the Account entity, in these 2 scenarios: 1) whenever the related Account is changed on the Opportunity record AND 2) whenever the Account Type is changed on the related Account record.

Both AccountType fields (on Account and Opportunity) are Global Option Sets.

However, the business process workflow does not allow me to set the AccountType field in the Opportunity since it's an option set. So I added a new field of Text type and the workflow now works.

However, my business rule on the Opportunity entity (to hide/display fields according to the AccountType) now has to include hard-coded strings for each value in this next Text field because it's a text field, not an option set.

The consequence of this is that every time we add or edit a value in the global option set, we will need to update the business rule accordingly.

Questions:

1) Is there a way to update an option set field in a workflow? I found this article from May 2017 saying this is a system bug, although one of the comments mentions it was fixed.

https://community.dynamics.com/crm/f/117/t/235679

2) Would it be better to use a 1:N relationship mapping on the Account to map this field from Account to Opportunity? Or is the workflow/business rule method that I've done above the best method?

Thanks in advance.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to update an option set field in a business process workflow

    Thanks so much Aric. This is awesome that you shared your code! I haven't had a chance to look into creating this yet, but I'll definitely start with this when we get to it. Many thanks.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Unable to update an option set field in a business process workflow

    Hi Sally,

    You will need to create a custom workflow activity. You will need to pass the following parameters: Account, Account Optionset Field Name, Opportunity Account Lookup Field Name, Opportunity Optionset Field Name (You can also replace the Account Optionset Field Name with the actual value).

    Below is sample code that should get you the results that you are looking for:

    using System;

    using System.Activities;

    using System.ServiceModel;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Workflow;

    namespace XrmWorkflowTools

    {

       public class CopyOptionSet : CodeActivity

       {

           #region Input/Output Parameters

           [Input("Account")]

           [RequiredArgument]

           [ReferenceTarget("account")]

           public InArgument<EntityReference> Account { get; set; }

           [Input("Account Optionset Field Name")]

           [RequiredArgument]

           public InArgument<string> AccountFieldName { get; set; }

           [Input("Opportunity Account Lookup Field Name")]

           [RequiredArgument]

           public InArgument<string> OpportunityAccountLookupFieldName { get; set; }

           [Input("Opportunity Optionset Field Name")]

           [RequiredArgument]

           public InArgument<string> OpportunityFieldName { get; set; }

           #endregion

           protected override void Execute(CodeActivityContext executionContext)

           {

               // Extract the tracing service

               ITracingService tracingService = executionContext.GetExtension<ITracingService>();

               try

               {

                   //Create the context

                   IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

                   IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

                   IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                   EntityReference refAccount = Account.Get<EntityReference>(executionContext);

                   string accountFieldName = AccountFieldName.Get<string>(executionContext);

                   ColumnSet columns = new ColumnSet(new string[] { accountFieldName });

                   Entity account = service.Retrieve(refAccount.LogicalName, refAccount.Id, columns);

                   // Validate Field Contains Value

                   int accountPicklistValue = account.GetAttributeValue<OptionSetValue>(accountFieldName).Value;

                   string accountLookupFieldName = OpportunityAccountLookupFieldName.Get<string>(executionContext);

                   EntityCollection opportunities = RetrieveOpportunities(service, refAccount, accountLookupFieldName);

                   if (opportunities.Entities.Count > 0)

                   {

                       string opportunityFieldName = OpportunityFieldName.Get<string>(executionContext);

                       foreach(Entity opportunity in opportunities.Entities)

                       {

                           Guid opportunityId = opportunity.Id;

                           UpdateOpportunity(service, opportunityId, opportunityFieldName, accountPicklistValue);

                       }

                   }

               }

               catch (FaultException<OrganizationServiceFault> ex)

               {

                   throw new Exception("SBS.Workflow.CopyOptionSet: " + ex.Message);

               }

           }

           private EntityCollection RetrieveOpportunities(IOrganizationService service, EntityReference account, string attributeName)

           {

               QueryExpression query = new QueryExpression("opportunity")

               {

                   ColumnSet = new ColumnSet("opportunityid"),

                   Criteria = new FilterExpression(LogicalOperator.And)

                   {

                       Conditions =

                       {

                           new ConditionExpression(attributeName, ConditionOperator.Equal, account.Id)

                       }

                   }

               };

               try

               {

                   EntityCollection results = service.RetrieveMultiple(query);

                   return results;

               }

               catch (FaultException<OrganizationServiceFault> ex)

               {

                   throw new Exception("XrmWorkflowTools.CopyOptionSet.RetrieveOpportunities: " + ex.Message);

               }

           }

           private void UpdateOpportunity(IOrganizationService service, Guid opportunityId, string opportunityFieldName, int accountPicklistValue)

           {

               Entity opportunity = new Entity("opportunity");

               opportunity.Id = opportunityId;

               opportunity[opportunityFieldName] = new OptionSetValue(accountPicklistValue);

               try

               {

                   service.Update(opportunity);

               }

               catch (FaultException<OrganizationServiceFault> ex)

               {

                   throw new Exception("XrmWorkflowTools.CopyOptionSet.UpdateOpportunity: " + ex.Message);

               }

           }

       }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to update an option set field in a business process workflow

    Thanks for your help. I managed to get the workflow working now to update the Opportunity option set when the Account record changes on the Opportunity.

    However, the workflow to change the Opportunity option set when the Account option set changes is still not working.

    I created a 1:N Relationship from Account to Opportunity and included AccountType option set in the mapping, however Opportunities are never created from an Account so I'm not sure if this is needed.

    Here is a screenshot of my workflow, triggered OnChange of the AccountType option set on the Account record. However, the Opportunity entity is not displayed in the Update entity list. I want to update the Opportunity AccountType option set to equal the Account AccountType option set, for all related Opportunities.

    workflow_2D00_screenshot.PNG

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Unable to update an option set field in a business process workflow

    Hi Sally,

    Can you provide a screenshot of your workflow to see what you are trying to do? I think I understand your requirements, but just want to make sure.

    For creating new opportunities from the account record, if you have your mapping set for the Optionset between account and opportunity, the Optionset in opportunity should prepopulate.

    For making changes on the account optionset, you will have to do one of the following (since this is a 1-N relationship):

    - Write a Custom Workflow activity on the Change event of the account to modify the opportunity child records

    - Write a Plugin on the Update event of the account entity to modify the opportunity child records

    - Use a Workflow Utility that provides the capabilities of modifying multiple records (such as Ultimate Workflow Toolkit: github.com/.../UltimateWorkflowToolkit or Dynamics 365 Workflow Tools github.com/.../Dynamics-365-Workflow-Tools)

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to update an option set field in a business process workflow

    I also just created a 1:N relationship from Account to Opportunity and added the global option sets AccountType on each entity to the mapping. However, when updating the AccountType selection on the Account record OR when adding a new Opportunity record for this account, no change is made to the AccountType on the related Opportunity record. I followed the method on this page but it did not update the Opportunity record AccountType field. Any ideas?

    www.powerxrm.com/how-to-map-option-set-fields-in-crm

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans