locked
2011 on-premise - Custom workflow activity output of systemuser not working RRS feed

  • Question

  • I'm trying to create a workflow that, very 1st step will create an activity and assign the owner to the person who kicked off the task.


    When I add the task to my workflow and try to use it's "Output" in the next step, it doesn't show as an option.


    Here is my code.


    Thanks,
    Jon

    publicclassGetExecutingUser : CodeActivity
    {
    [Output("OutputCurrentUser")]
    [ReferenceTarget("systemuser")]
    publicOutArgument<EntityReference> OutputCurrentUser { get; set; }
     
    protectedoverridevoid Execute(CodeActivityContext executionContext)
    {
    IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
    IOrganizationService service = executionContext.GetExtension<IOrganizationServiceFactory>().CreateOrganizationService(context.UserId);
     
    Entity currentUser = service.Retrieve("systemuser", context.UserId, new ColumnSet(true));
      OutputCurrentUser.Set(executionContext, currentUser);
    }
    }    

    Tuesday, March 6, 2012 4:04 PM

Answers

  • Greetings Goosey,

    If the workflow runs when f.e. a record is created (as in automatically) then the default user will always be the system user or the workflow owner, but if the user runs the workflow himself you would get that user as the owner.

    Ok so with that information and your results I would assume that users will not be running the workflow themself, if the workflow is run when a record is created you can use that records "created by" field to use to set the owner of the task(?) that you are creating.

    If the workflow is run when f.e. its status changes then you should be able to use the "modified by" fields user to get the correct user as the owner of the task(?) you are creating.

    The context.UserId will if I am not mistaken give you exactly the same user as leaving the records owner empty.

    After looking more closely at your code I think you might have to return a Owner object instead of user object since owner can also be a team, see if you can set the Output to use Owner instead of OutputCurrentUser.


    Halldór Jóhannsson

    • Marked as answer by Goosey314159 Tuesday, March 13, 2012 8:04 PM
    Wednesday, March 7, 2012 5:10 PM

All replies

  • the declarations look right but I think you have another problem.  To change the owner I think you're going to need to use an AssignRequest.  Are you specifically needing the sysuser from the output to use as an input into another activity? If not, I think just calling AssignRequest will accomplish wht you need

    Cordially, W.G.Ryan - www.williamgryan.mobi

    Tuesday, March 6, 2012 6:43 PM
  • Are you specifically needing the sysuser from the output to use as an input into another activity?


    Correct. I need 1 step of my workflow to to run this activity and use the output in the 2nd step, which is creating an activity and assigning it to them.

    Currently I can use my output on text(string) fields but when I select the activity owner field then my output field is no longer select-able.

    It looks like it's being filtered out, like when you highlight a date field, the right side drop down only shows fields of the primary entity that are of type date.

    I have tried output types of 'systemuser', 'owner', and Guid, nothing works. 

    Tuesday, March 6, 2012 7:00 PM
  • Hmm, the code looks right (I know, that's not much help) - let me take a stab at it and see if I can find anything.

    Cordially, W.G.Ryan - www.williamgryan.mobi

    Tuesday, March 6, 2012 7:34 PM
  • I think there is Type mismatch here, your Output variable OutputCurrentUser  is of Type EntityReference where as you are setting value as Type Entity.

    MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview | CRM2011 Lookup Attribute Mapping | CRM2011 TreeView Control (Beta)

    Wednesday, March 7, 2012 8:33 AM
  • Greetings Goosey,

    I am wondering if you can do what you need done without the custom activity.

    If your workflow is run on demand then you can leave the owner field empty and it should fill in the current user running it as owner, is that what you are trying to do?

    If your workflow runs when a record is created then you can use the created by field for that record and use that to set the owner of the record you create with the workflow.

    If not then Makarand is correct..

    This line

    Entity currentUser = service.Retrieve("systemuser", context.UserId, new ColumnSet(true));

    Should be

    EntityReference currentUser = service.Retrieve("systemuser", context.UserId, new ColumnSet(true));


    Halldór Jóhannsson


    Wednesday, March 7, 2012 11:15 AM
  • I think there is Type mismatch here, your Output variable OutputCurrentUser  is of Type EntityReference where as you are setting value as Type Entity.

    MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview | CRM2011 Lookup Attribute Mapping | CRM2011 TreeView Control (Beta)

    I've tried both and it still won't show in the dropdown for that field.
    Wednesday, March 7, 2012 3:49 PM
  • Greetings Goosey,

    I am wondering if you can do what you need done without the custom activity.

    If your workflow is run on demand then you can leave the owner field empty and it should fill in the current user running it as owner, is that what you are trying to do?

    If your workflow runs when a record is created then you can use the created by field for that record and use that to set the owner of the record you create with the workflow.

    If not then Makarand is correct..

    This line

    Entity currentUser = service.Retrieve("systemuser", context.UserId, new ColumnSet(true));

    Should be

    EntityReference currentUser = service.Retrieve("systemuser", context.UserId, new ColumnSet(true));


    Halldór Jóhannsson


    Leaving it blank defaulted to the workflow owner, but not the current user.

    I've also tried the EntityReference path

    new EntityReference("systemuser", context.UserId)
    

    No go either. :(

    Wednesday, March 7, 2012 3:52 PM
  • Greetings Goosey,

    If the workflow runs when f.e. a record is created (as in automatically) then the default user will always be the system user or the workflow owner, but if the user runs the workflow himself you would get that user as the owner.

    Ok so with that information and your results I would assume that users will not be running the workflow themself, if the workflow is run when a record is created you can use that records "created by" field to use to set the owner of the task(?) that you are creating.

    If the workflow is run when f.e. its status changes then you should be able to use the "modified by" fields user to get the correct user as the owner of the task(?) you are creating.

    The context.UserId will if I am not mistaken give you exactly the same user as leaving the records owner empty.

    After looking more closely at your code I think you might have to return a Owner object instead of user object since owner can also be a team, see if you can set the Output to use Owner instead of OutputCurrentUser.


    Halldór Jóhannsson

    • Marked as answer by Goosey314159 Tuesday, March 13, 2012 8:04 PM
    Wednesday, March 7, 2012 5:10 PM
  • Greetings Goosey,

    If the workflow runs when f.e. a record is created (as in automatically) then the default user will always be the system user or the workflow owner, but if the user runs the workflow himself you would get that user as the owner.

    Ok so with that information and your results I would assume that users will not be running the workflow themself, if the workflow is run when a record is created you can use that records "created by" field to use to set the owner of the task(?) that you are creating.

    If the workflow is run when f.e. its status changes then you should be able to use the "modified by" fields user to get the correct user as the owner of the task(?) you are creating.

    The context.UserId will if I am not mistaken give you exactly the same user as leaving the records owner empty.

    After looking more closely at your code I think you might have to return a Owner object instead of user object since owner can also be a team, see if you can set the Output to use Owner instead of OutputCurrentUser.


    Halldór Jóhannsson

    Thanks Halldór!

    Modified By did the trick :) I like simple answers 

    Tuesday, March 13, 2012 8:05 PM