Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Get Current AsyncOperationId in CodeActivity

Posted on by Microsoft Employee

Hello guys,

I am currently working on some new CodeActivities for CRM 2011 and I got stuck with one particular problem.
The new Activity is supposed to execute a given Subworkflow every X Seconds for forever. Problem is, when we want to terminate the Workflow its status will stay on "Cancelling" and the activity will continue to execute the Subworkflow.

To counter that I wanted to add a check for the current status but I cant find a way to get my current AsyncOperationId.

My code currently looks like this, but I found out the "WorkflowInstanceId" does not match the AsyncOperationId (as I hoped) and I cant find anything regarding this isssue.

var currentStatusQuery = new QueryByAttribute()
{
    EntityName = AsyncOperation.EntityLogicalName,
    ColumnSet = new ColumnSet("statecode", "statuscode")
};
currentStatusQuery.AddAttributeValue("asyncoperationid", this.Context.WorkflowInstanceId);

if ((this.Service.RetrieveMultiple(currentStatusQuery).Entities?.FirstOrDefault() as AsyncOperation)?.StatusCode?.Value == 22)
{
    //... end this activity
}


Looking forward to any help.

Greetings

Simon Schulte

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get Current AsyncOperationId in CodeActivity

    Hey,

    the problem we are trying to solve with this Activity is the one that CRM does not allow to start a recurring Task but we have some synchronization and reporting-tasks that have to be executed regularly. (Like every hour or every month)

    Today we have a workaround in place where we got a datetime-field on an entity on which the workflow will execute on update and wait for the time to arrive. After it is done it will set the datetime-field to the next value.

    But there are some downsides to this, for example if you need to execute the workflow for more than 8 times an hour crm will throw an exception.

    Of course we could develop external applications that will start the workflow in time using the web-api, but we would like to have a solution inside of crm so our customers do not have to care for extra applications.

    If there is another solution to this particular problem or anything, I would love to hear it.

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Get Current AsyncOperationId in CodeActivity

    Hi,

    Your design is really bad, you should not run CustomActivities forever. In your case, you should develop custom console application that will run every X seconds (why so often?) running whatever you want. Currently you are trying to find a solution for a bad design that will make it even worse design. Why have you chosen CustomAcitivites and workflows for your task? From the description it's clear that they are not suitable to do the job.

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