Search code examples
javascriptc#dynamics-crmcrm

Add link to the newly created entity record using Notify.js - CRM


I have a custom workflow activity that creates new opportunity from my custom workflow activity. I have that activity as an action step. I am using action, because it has an output. I need to get Id of opportunity i created. I call that action from JS web resource using Process.js. After that I use Notify.js to notify the user that opportunity was created. On that notification i need to have button that would be link to the newly created opportunity.

Here are some parts of the C# code related to the output parameter. Just to notice that part of the code for creating opportunity, and doing some more tasks works fine:

//define variable
[Output("Opportunity")]
[ReferenceTarget("opportunity")]
public OutArgument<EntityReference> NewOpportunity { get; set; }

//create opportunity and entity reference(i am not sure do ineed entity reference, or something else for that link)
Guid opportunityId = service.Create(opportunity);
EntityReference OppId = new EntityReference(opportunity.LogicalName, opportunityId);

//assign value to the output variable
NewOpportunity.Set(Econtext, OppId);

Here is how i created my action parameter in action definition: enter image description here

And here is the JS code where action is called:

function toOpportunity(){

Process.callAction("ad_opportunity",
    [{
        key: "Target",
        type: Process.Type.EntityReference,
        value: { id: Xrm.Page.data.entity.getId(), entityType: "ad_productsamplerequest" }
    }],
    function (param) {
        //Success            
         Notify.add("New Opportunity was created:", "INFO", "opportunity",
    [{
        type: "button",
        text: "Go to opportunity",
        callback: function () {

        }
    }]);
    },
    function () {
        // Error
        alert("Opportunity was not created");
    }
);

Just to say, it works, action is called, opportunity is created, there is notification after that. just don't know how to use action output parameter to set the link to the opportunity.


Solution

  • It looks like you are trying to handle the action in a CodeActivity class. This will not work. OutArgument properties are only accessible within the workflow, they cannot be returned to a calling process.

    Instead, create an action with the desired input and output parameters. Then create a plugin and register a synchronous post update step on this action. The plugin class implementation must add the opportunity ID to the OutputParameters collection, like this:

    public void Execute(IServiceProvider serviceProvider)
    {
        var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        var service = factory.CreateOrganizationService(context.UserId);
    
        Entity opportunity;
        // TODO: add code here to build the opportunity.
    
        Guid opportunityId = service.Create(opportunity);
        context.OutputParameters.Add("NewOpportunity", new EntityReference("opportunity", opportunityId));
    }
    

    See also this explanation on MSDN.