Announcements
Any help would be appreciated. This simple plugin shows no errors and registers fine but fails when a contact is created.
It should simply create a follow-up task after contact is created.
Here is the error: Generic.
Exception Message: An error occurred in MyPlug-in.
ErrorCode: -2147220891
HexErrorCode: 0x80040265
ErrorDetails:
OperationStatus: 0
SubErrorCode: -2146233088
Plugin:
ExceptionFromPluginExecute: TaskCreate.TaskCreate
ExceptionRetriable: False
ExceptionSource: PluginExecution
OriginalException: PluginExecution
PluginTrace:
HelpLink: go.microsoft.com/.../
TraceText:
[HelloWorldPopulateDescriptionField: TaskCreate.TaskCreate]
[89954400-b2a3-ed11-aad1-000d3a2d2719: TaskCreate.TaskCreate: Create of contact]
Starting sync workflow 'Lock Company ID On Contact', Id: 247e8283-1e97-ed11-aad1-000d3a2d2719
Entering ConditionStep1_step:
Entering ConditionBranchStep2_step:
Sync workflow 'Lock Company ID On Contact' completed successfully
Activity Id: bc59adea-2a97-4572-add7-d48ad26374e1
Here is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
namespace TaskCreate
{
public class TaskCreate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity contact = (Entity)context.InputParameters["Target"];
try
{
// Plug-in business logic
//When user creates a contact, a task must be created automatically by the system. The task should be completed within 2 days.
//this is an object not a new record - it is ready for the service.create to use this in memory object
Entity taskRecord = new Entity("task");
//this sets the single lines of text in the task
taskRecord.Attributes.Add("subject", "Follow up");
//date attribute populated in the task and set for 2 days later
taskRecord.Attributes.Add("scheduledend", DateTime.Now.AddDays(2));
//Set option set value attribute sets status
taskRecord.Attributes.Add("priority", new OptionSetValue(2));
//Set the Parent record attribute lookup regarding attribute on the task to ilnk it to parent contact
taskRecord.Attributes.Add("regardingobjectid", new EntityReference("contact", contact.Id ));
Guid taskGuid = service.Create(taskRecord);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
The code is signed and built. The plugin registration tool is used:
Create
Contact
PostOperation
I would love to use the profiler to debug, but there is an error there, too, when I try to install it. You may think there is a link, but other plugins have registered fine and work and the issue is not with registering, it is at the point of a contact being created in CRM.
Thank you Manoj. This is very helpful.
For those interested, I was going through it line by line and have figured out where the issue is coming from:
//Set option set value attribute sets status
taskRecord.Attributes.Add("priority", new OptionSetValue(2));
Will now do a little digging into how to set this value according to Microsoft documentation correctly.
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156