Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Custom Workflow Activity to set Default Value on Opportunity Product

Posted on by 1,545

I'm trying to set a default value on the Opportunity Product entity via a Custom Workflow Actvity. That would be so much easier if we could run workflows on that entity...!

Not being a coder, I'm having quite a hard time. Code should be pretty simple and look something like this:

public class LockPricesOpportunityProducts:CodeActivity
{
protected override void Execute(CodeActivityContext context)
{

ITracingService tracingService = context.GetExtension<ITracingService>();
IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

Entity OpportunityProduct = new Entity("opportunityproduct");
OpportunityProduct["ispriceoverridden"] = true;

}
}

I have no error when I compile the code, no error when I register the plugin, and no error when I run the workflow in CRM.

But nothing happens. I'm just trying to set the ispriceoverridden field to True. I thought I would use a Custom Workflow because I can then run it manually and add conditions.

Any hel would be much appreciated.

*This post is locked for comments

  • yleclerc Profile Picture
    yleclerc 1,545 on at
    RE: Custom Workflow Activity to set Default Value on Opportunity Product

    Thank you both very much. Works great!

    I need to learn code...!

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Custom Workflow Activity to set Default Value on Opportunity Product

    Hi Yvan,

    The below code should work.

    ==================

    public class LockPricesOpportunityProducts : CodeActivity

       {

            protected override void Execute(CodeActivityContext context)

           {

               try

               {

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

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

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

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

                   Entity OpportunityProduct = new Entity(workflowContext.PrimaryEntityName, workflowContext.PrimaryEntityId);

                   OpportunityProduct["ispriceoverridden"] = true;

                   service.Update(OpportunityProduct);

               }

               catch (Exception pEx)

               {

                   throw new InvalidPluginExecutionException("Error in custom workflow activity!!! " + pEx.Message);

               }

           }

       }

    ==============================

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Custom Workflow Activity to set Default Value on Opportunity Product

    Hi,

    There are two missing items.

    1. opportunityproduct id is missing
      - OpportunityProduct.Id = workflowContext.PrimaryEntityId; // opportunity product record id 
    2. update statement is missing
      - service.Update(OpportunityProduct);

    Sample

    msdn.microsoft.com/.../gg509012.aspx

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans