Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Is it possible to request cancel in a custom workflow?

Posted on by Microsoft Employee

Hello all!

Is it possible to request cancel in a custom workflow using C#?

Please look at the example below.

namespace test_workflow
{
    public class JustTesing: CodeActivity
        {
            protected override void Execute(CodeActivityContext executionContext)
            {

                if(boolean)
                {

                   //do stuff

                 }
                else
                 {

                    // I would like to cancel the workflow here so in the workflow history it will appear as canceled

                  }

            }
     }
}


Thank you!

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Is it possible to request cancel in a custom workflow?

    Simply added a boolean output as you suggested and it worked. Didn't change anything else.

    Thank you

  • Suggested answer
    Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: Is it possible to request cancel in a custom workflow?

    The error message  "The given key was not present in the directory" is talking about an error elsewhere in your code - it won't be related to that (x> y) bit. Could you paste the complete code so we can try to identify where you are asking for something without checking if it is there.  

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Is it possible to request cancel in a custom workflow?

    Hmm.. I had the following and the workflow says failed with error message " The given key was not present in the directory" when the condition in if statement was not met.

    namespace test_workflow

    {

       public class JustTesing: CodeActivity

           {

               protected override void Execute(CodeActivityContext executionContext)

               {

                   try {

                           //retrieved some fields here and computed x and y, everything are solid

                        if( x > y)

                        {

                          //do stuff

                         }

                    }

                    catch(exception e){

                             throw new InvalidPluginExecutionException(e.Message);

                    }

               }

        }

    }

  • Verified answer
    Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: Is it possible to request cancel in a custom workflow?

    You cannot cancel a workflow directly from the custom workflow you called so the best way to achieve what you want would be to add a boolean output parameter to your custom workflow and set it  Yes if the workflow was successfully and No if you want the workflow to cancel itself.

    Then check the value of that output parameter in the next step of the workflow and if No is returned Stop the workflow with a status of cancelled.

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans