Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Custom Workflow that re-assigns Owner has suddenly stopped working

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have a workflow that reassigns the owner based on a field called "QuoteWerks Prepared by". 0535.WF1.PNG

The "KED365" step is a custom workflow activity, and uses the code below:

namespace KED365.Workflows
{
using System;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using System.Linq;

public sealed class GetUserByFullName : WorkFlowActivityBase
{
[Input("User Full Name")]
public InArgument<string> UserFullName { get; set; }

[Output("Prepared By")]
[ReferenceTarget("systemuser")]
public OutArgument<EntityReference> PreparedBy { get; set; }


[Output("IsSuccess")]
public OutArgument<bool> IsSuccess { get; set; }

[Output("Message")]
public OutArgument<string> Message { get; set; }

protected override void Execute(CodeActivityContext activityContext, IWorkflowContext workflowContext, IOrganizationService CrmService, ITracingService trace)
{
try
{
string userName = UserFullName.Get(activityContext);

if (string.IsNullOrWhiteSpace(userName))
{
IsSuccess.Set(activityContext, false);
Message.Set(activityContext, "User's Full Name is not provided");
return;
}

var QEsystemuser = new QueryExpression("systemuser");
QEsystemuser.ColumnSet.AddColumns("fullname");
QEsystemuser.Criteria.AddCondition("fullname", ConditionOperator.Equal, userName);

var results = CrmService.RetrieveMultiple(QEsystemuser);

if (results == null || !results.Entities.Any())
{
IsSuccess.Set(activityContext, false);
Message.Set(activityContext, "User with " + userName + " not found") ;
return;
}

if (results.Entities.Count > 1)
{
IsSuccess.Set(activityContext, false);
Message.Set(activityContext, "Multiple users found with same name : " + userName);
return;
}


IsSuccess.Set(activityContext, true);
PreparedBy.Set(activityContext, results.Entities.Single().ToEntityReference());
}
catch (Exception ex)
{
IsSuccess.Set(activityContext, false);
Message.Set(activityContext, "An error occurred trying to find user : " + ex.Message);
}
}
}
}

--------------

The "Set properties" portion of this step sets the field to "QuoteWerks Sales Rep".

Next, the record gets assigned to the user that was returned in the previous step.

However, the workflow has suddenly stopped working.  I receive the error below: 

The Owner was not provided.

Plugin Trace:
[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.AssignEntity]
[Microsoft.Xrm.Sdk.Workflow (9.0.0.0): Microsoft.Xrm.Sdk.Workflow.Activities.AssignEntity]
Error Message:
Unhandled exception:
Exception type: System.ArgumentException
Message: The Owner was not provided
-- End stack trace --
Exception type: Microsoft.Crm.CrmArgumentException
Message: The Owner was not provided
   at Microsoft.Crm.Workflow.Services.AssignActivityService.Execute(ActivityContext executionContext, AssignEntity assignEntity)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
-- End stack trace --
The workflow worked fine until about a week ago.  Any help fixing this would be greatly appreciated.  
Thank you!

*This post is locked for comments

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,050 on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    Hi

    Please make sure you follow the steps below to install the developer toolkit

    01. Download the SDK and estract to a folder if you do not already have done so

    https://www.microsoft.com/en-us/download/details.aspx?id=50032

    02. Download and install VS 2015 (Community edition if you do not have license for premium versions)

    [View:https://visualstudio.microsoft.com/vs/older-downloads/:750:50]

    03. Once VS 2015 is installed, install the developer toolkit

    04.Once you installed make sure you set the PluginRegtool and bin folder path as shown below (These are SDK folders)

    5460.ScreenClip-_5B00_192_5D00_.png

    https://community.dynamics.com/365/b/dynamics365fordevelopers/archive/2017/03/07/install-dynamics-365-developer-toolkit-with-visual-studio-2017

    Once you completed all the above steps, if you open the project, it should open fine.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    Thank you so much for the reply.  When I download the source code and try to open it, I receive the following error:

    InstallFailed.PNG

    Is there a certain version of Visual Studio I need to be able to open this project?

    Also, when I try to install the CRMtoolkit, I get the following error:

    InstallFailed.PNG

    It says "Not installable on any currently installed products", but I have Visual Studio 2019 so I feel it should be able to install.  Any idea why this is happening?

    Thanks!

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,050 on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    1.  How can I get rid of the error below, which still occurs when I put your code in a Visual Studio file?  I'm assuming I'm missing a certain NuGet package.  What NuGet package do I need to get rid of it?

    Install the developer toolkit from the following link if you do not already have this

    marketplace.visualstudio.com/items

    And then you can download the full source code from the following link to see the packages referenced

    github.com/.../GetUserByName

    2.  Is it possible to lookup the User based on an email address rather than the user's name?  If yes, how would I do that?

    You could pass the email address as the input argument to the CWA and change your query as shown below to get it working for email-based search

                    var QEsystemuser = new QueryExpression("systemuser");

                   QEsystemuser.ColumnSet.AddColumns("fullname");

                   QEsystemuser.Criteria.AddCondition("internalemailaddress", ConditionOperator.Equal, emailaddress); //email address passed to the CWA

                   var results = CrmService.RetrieveMultiple(QEsystemuser);

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    Thank you so much Kokulan!  That solution worked.  I do have a couple more quick questions if you don't mind.

    1.  How can I get rid of the error below, which still occurs when I put your code in a Visual Studio file?  I'm assuming I'm missing a certain NuGet package.  What NuGet package do I need to get rid of it?

    Severity Code Description Project File Line Suppression State

    Error CS0103 The name 'CrmService' does not exist in the current context OpportunityWorkflowLib C:\Users\tgiard\Downloads\OpportunityWorkflowLib\OpportunityWorkflowLib\SalesRepActivity.cs 51 Active

    2.  Is it possible to lookup the User based on an email address rather than the user's name?  If yes, how would I do that?

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,050 on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    Hi

    Yes most likely nuget packages, I will push the code when I get time but for now, you could try and download the latest solution file

    https://github.com/Kokulan365/GetUserByName/releases

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    I tried building this in Visual Studio but received the following 5 errors:

    Error CS0234 The type or namespace name 'Query' does not exist in the namespace 'Microsoft.Xrm.Sdk' (are you missing an assembly reference?) OpportunityWorkflowLib C:\Users\tgiard\Downloads\OpportunityWorkflowLib\OpportunityWorkflowLib\SalesRepActivity.cs 2 Active

    Severity Code Description Project File Line Suppression State

    Error CS0246 The type or namespace name 'EntityReference' could not be found (are you missing a using directive or an assembly reference?) OpportunityWorkflowLib C:\Users\tgiard\Downloads\OpportunityWorkflowLib\OpportunityWorkflowLib\SalesRepActivity.cs 20 Active

    Severity Code Description Project File Line Suppression State

    Error CS0246 The type or namespace name 'QueryExpression' could not be found (are you missing a using directive or an assembly reference?) OpportunityWorkflowLib C:\Users\tgiard\Downloads\OpportunityWorkflowLib\OpportunityWorkflowLib\SalesRepActivity.cs 46 Active

    Severity Code Description Project File Line Suppression State

    Error CS0103 The name 'ConditionOperator' does not exist in the current context OpportunityWorkflowLib C:\Users\tgiard\Downloads\OpportunityWorkflowLib\OpportunityWorkflowLib\SalesRepActivity.cs 48 Active

    Severity Code Description Project File Line Suppression State

    Error CS0103 The name 'CrmService' does not exist in the current context OpportunityWorkflowLib C:\Users\tgiard\Downloads\OpportunityWorkflowLib\OpportunityWorkflowLib\SalesRepActivity.cs 51 Active

    Am I missing something?  I'm wondering if there are more NuGet Packages I need that I do not have.

    Or are you able to provide me with a zip file with this code?

    Thank you.

  • Verified answer
    Kokulan Profile Picture
    Kokulan 18,050 on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    Hi

    You could change your Custom Workflow Activity to work with both FirstName LastName and LastName, FirstName. Please see the code example below

    namespace KED365.Workflows

    {

       using System;

       using System.Activities;

       using System.ServiceModel;

       using Microsoft.Xrm.Sdk;

       using Microsoft.Xrm.Sdk.Workflow;

       using Microsoft.Xrm.Sdk.Query;

       using System.Linq;

       public sealed class GetUserByFullName : WorkFlowActivityBase

       {

           [Input("User Full Name")]

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

           [Output("Prepared By")]

           [ReferenceTarget("systemuser")]

           public OutArgument<EntityReference> PreparedBy { get; set; }

           [Output("IsSuccess")]

           public OutArgument<bool> IsSuccess { get; set; }

           [Output("Message")]

           public OutArgument<string> Message { get; set; }

           protected override void Execute(CodeActivityContext activityContext, IWorkflowContext workflowContext, IOrganizationService CrmService, ITracingService trace)

           {

               try

               {

                   string userName = UserFullName.Get(activityContext);

                   if (string.IsNullOrWhiteSpace(userName))

                   {

                       IsSuccess.Set(activityContext, false);

                       Message.Set(activityContext, "User's Full Name  is not provided");

                       return;

                   }

                   var QEsystemuser = new QueryExpression("systemuser");

                   QEsystemuser.ColumnSet.AddColumns("fullname");

                   var QEsystemuser_Criteria_0 = new FilterExpression();

                   QEsystemuser.Criteria.AddFilter(QEsystemuser_Criteria_0);

                   QEsystemuser_Criteria_0.FilterOperator = LogicalOperator.Or;

                   QEsystemuser_Criteria_0.AddCondition("fullname", ConditionOperator.Equal, userName);

                   QEsystemuser_Criteria_0.AddCondition("fullname", ConditionOperator.Equal, ChangeNameOrder(userName));

                   var results = CrmService.RetrieveMultiple(QEsystemuser);

                   if (results == null || !results.Entities.Any())

                   {

                       IsSuccess.Set(activityContext, false);

                       Message.Set(activityContext, "User with " + userName + " not found") ;

                       return;

                   }

                   if (results.Entities.Count > 1)

                   {

                       IsSuccess.Set(activityContext, false);

                       Message.Set(activityContext, "Multiple users found with same name : " + userName);

                       return;

                   }

                   IsSuccess.Set(activityContext, true);

                   PreparedBy.Set(activityContext, results.Entities.Single().ToEntityReference());

               }

               catch (Exception ex)

               {

                   IsSuccess.Set(activityContext, false);

                   Message.Set(activityContext, "An error occurred trying to find user : " + ex.Message);

               }

           }

           private string ChangeNameOrder(string name)

           {

               string[] nameParts = name.Split(' ');

               return nameParts[1] + ", " + nameParts[0];  // If you do not need the comma you can remove

           }

       }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    Thanks for the suggestion.  I created the note and it was returning nothing, as no user was found.  I noticed that when I manually change the prepared by field from "FirstName LastName" to "LastName, FirstName" formatting, the message outparameter gets populated as it should and the Owner gets updated as it should, so the formatting of the name seems to be the issue.

    The "PreparedBy" field gets autopopulated by a field in QuoteWerks.  Any idea how I can change the formatting from "FirstName LastName" to "LastName, FirstName" within CRM?  I tried creating a 2nd field that is calculated and based on the PreparedBy field, but I can't seem to parse part of the PraparedBy field to display the name as "LastName, FirstName".

  • Guido Preite Profile Picture
    Guido Preite 54,069 Super User 2024 Season 1 on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    you can use a "Create" step, selected Annotation as entity and inside the body (or the title) you put the value of the outparameter Message

    do this step before the assign opportunity, otherwise it will not be created (or better remove the assign step for a moment)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Custom Workflow that re-assigns Owner has suddenly stopped working

    Guido,

    How would I create a note that puts the note body of the content of the message outparameter?

    Thanks!

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans