Reputation: 379
Is there any way to automatically trigger a Custom Workflow Activity every time any Entity's record is opened?
Upvotes: 2
Views: 1204
Reputation: 18895
If you're wanting to trigger a custom workflow activity, and don't need to do anything workflow related in it, I'd recommend creating a custom action. It is very similar to a workflow, but CRM will create a custom end point for you to call. It eliminates the need to keep track of workflow IDs...
Upvotes: 2
Reputation: 2123
You can use a Plugin
instead of Custom Workflow, and register it on the "Retrieve" message.
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
if (context.Depth == 1)
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Obtain the target entity from the input parmameters.
EntityReference entity = (EntityReference)context.InputParameters["Target"];
ColumnSet cols = new ColumnSet(
new String[] { "lastname", "firstname", "address1_name" });
var contact = service.Retrieve("contact", entity.Id, cols);
if (contact != null)
{
if (contact.Attributes.Contains("address1_name") == false)
{
Random rndgen = new Random();
contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString());
}
else
{
contact["address1_name"] = "i already exist";
}
service.Update(contact);
}
}
}
Upvotes: 1
Reputation: 4111
Sure, you could use the ExecuteWorkflow
request from some JavaScript that runs on Form Load. Here's an example of calling ExecuteWorkflow
from JavaScript.
http://www.mscrmconsultant.com/2013/03/execute-workflow-using-javascript-in.html
Upvotes: 2