locked
Access target entity in CRM 2013 ondemand workflow RRS feed

  • Question

  • Hi,

    I have a custom workflow which is running on demand because of which i am not able to get the target entity using below line of code

    Entity entity = (Entity)context.InputParameters["Target"];

    What is the best way to get the target entity? Do we have to retrieve the entity using primaryEntityId from context?

    Any help on this is appreciated.

    Thanks.


    Kruthi Hegde

    Thursday, April 23, 2015 7:30 AM

All replies

  • Hello Kruthi,

    I think the best way to get the entity the workflow is running on is with the PrimaryEntityId. Example below

    protected override void Execute(CodeActivityContext executionContext)
    {
    	IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
    	IOrganizationServiceFactory  serviceFactory = executionContext.GetExtension(IOrganizationServiceFactory>();
    	IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
    	
    	Guid entityId = context.PrimaryEntityId;
    	
    	Entity entity = service.Retrieve(context.PrimaryEntityName, entityId, new ColumnSet(true));
    	
    	....
    }

    I always get the entity this way.

    Hope it helps,

    Kind Regards

    Thursday, April 23, 2015 7:58 AM
  • Hello,

    So normally on a Plugin you get the Target entity which essentially contains the parameters passed in the request message (say you're updating Country and Phone, those will be the fields contained within the request message - Target). Plus you always get the Entity's Id.

    In Custom workflow activities, regardless of whether they are triggered by a specific event or on-demand, you can get InArgument parameters (Input parameters) to pass fields to the wf context.

    Or you could retrieve the Entity via the PrimaryEntityId, but I would not recommend returning the whole of ColumnSet as it is a best-practice to return only the fields you're looking to interact with.

    Simple how to CWA MSDN

    Hope that helps,


    • Edited by Pedro Beltrao Thursday, April 23, 2015 9:00 PM Add Hyperlink
    Thursday, April 23, 2015 8:59 PM
  • You can get the entity in the PreEntityImages, at least in 2016. Seems odd to put it there without configuring it that way in a custom workflow, instead of keeping with the "Target" paradigm established by Plugins, but there we are.

     Entity definition = (Entity)context.PreEntityImages["PreBusinessEntity"];


    Sunday, June 18, 2017 1:57 PM