Answered by:
Creating a task inside of a plugin

Question
-
Ok, so got another question for you the almighty CRM gods. I've been wracking my brain on this one. But essentially all this plugin is doing is creating a task, if a certain field has data when a contact is created.
I've been picking up some serious know-how for plugins lately, but this one has me confused. The plugin is set for the create message for the contact entity in the parent pipeline and set to run synchronously, in the post stage. It should have no problem performing this action from all I've read/know. But I'm getting errors. here's the code of the method that generates the task. The error is happing on the service.execute line, and its throwing "An unexpected error has occurred".
DynamicEntity newTask = new DynamicEntity(EntityName.task.ToString()); //Subject StringProperty strSubject = new StringProperty("subject", "Met at conference"); newTask.Properties.Add(strSubject); //Description StringProperty strDescription = new StringProperty("description", "Met at Conference. Created automatically when contact was created."); newTask.Properties.Add(strDescription); //Start date newTask.Properties.Add(new CrmDateTimeProperty("scheduledstart", CrmTypes.CreateCrmDateTimeFromUniversal(DateTime.Now))); //End Date newTask.Properties.Add(new CrmDateTimeProperty("scheduledend", CrmTypes.CreateCrmDateTimeFromUniversal(DateTime.Now))); //Regarding Lookup lkpRegardValue = new Lookup(EntityName.contact.ToString(), (Guid)context.OutputParameters.Properties["id"]); LookupProperty lkpRegarding = new LookupProperty(_parentEntityID, lkpRegardValue); newTask.Properties.Add(lkpRegarding); //Conference if (_dentity.Properties.Contains(_firstConference)) { Lookup lkpConfValue = CrmTypes.CreateLookup(_conferenceEntityName, (Guid)_dentity.Properties[_firstConference]); LookupProperty lkpConference = new LookupProperty(_conferenceIDField, lkpConfValue); newTask.Properties.Add(lkpConference); } else if (_dentity.Properties.Contains(_latestConference)) { Lookup lkpConfValue = CrmTypes.CreateLookup(_conferenceEntityName, (Guid)_dentity.Properties[_latestConference]); LookupProperty lkpConference = new LookupProperty(_conferenceIDField, lkpConfValue); newTask.Properties.Add(lkpConference); } //Make Complete CrmBoolean boolCRM = new CrmBoolean(true); CrmBooleanProperty boolMakeComplete = new CrmBooleanProperty(_markComplete, boolCRM); newTask.Properties.Add(boolMakeComplete); TargetUpdateDynamic targetUpdate = new TargetUpdateDynamic(); targetUpdate.Entity = newTask; UpdateRequest updateRequest = new UpdateRequest(); updateRequest.Target = targetUpdate; CrmService service = PluginUtils.GetCRMService(context); UpdateResponse response = (UpdateResponse)service.Execute(updateRequest); service.Dispose();
Things you might need to know _FirstConference and _latestconference are field names that are coming through an XML config set for the plugin. _conferenceEntityName is the name of the custom entity, and _conferenceIDField is the id field for the task entity where the N:1 relationship is. _markcomplete is a bit field that tells a plugin set to run on the create of the task to close the task post creation.
of course I set up trace log on our test environment and here's what it says for this error.
>MSCRM Error Report:
--------------------------------------------------------------------------------------------------------
Error: Exception has been thrown by the target of an invocation.
Error Number: 0x80040265
Error Message: An unexpected error occurred.
Error Details: An unexpected error occurred.
Source File: Not available
Line Number: Not available
Request URL: http://localhost:5555/MicrosoftCRM/sfa/conts/edit.aspx
Stack Trace Info: [Exception: An unexpected error occurred.]
at GFConference.GFContactCreate.Execute(IPluginExecutionContext context) in C:\VPC Drive\Projects\GFCaptureConference\GFCaptureConference\Plugins\GFConferenceContactCreate.cs:line 80
at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)
[InvalidPluginExecutionException: An unexpected error occurred.]
at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.ExtensiblePlatformMessageDispatcher.Execute(PipelineExecutionContext pluginContext)
at Microsoft.Crm.Extensibility.ExtensiblePlatformMessageDispatcher.Create(BusinessEntity entity, ExecutionContext context)
at Microsoft.Crm.BusinessEntities.BusinessProcessObject.CreateWithPipeline(IBusinessEntity entity, ExecutionContext context)
at Microsoft.Crm.BusinessEntities.BusinessProcessObject.Create(IBusinessEntity entity, ExecutionContext context)
at Microsoft.Crm.ObjectModel.ContactService.Create(IBusinessEntity entity, ExecutionContext context)
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Web.Services.Protocols.LogicalMethodInfo.Invoke(Object target, Object[] values)
at Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(IPluginExecutionContext context)
at Microsoft.Crm.Extensibility.PluginStep.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)
at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.Execute(String messageName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, PropertyBag fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.RequestBase.Process(Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.RequestBase.Process(CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.CrmServiceInternal.Execute(RequestBase request, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId)
at Microsoft.Crm.Sdk.InProcessCrmService.Execute(Object request)
at Microsoft.Crm.Application.Platform.ServiceCommands.PlatformCommand.ExecuteInternal()
at Microsoft.Crm.Application.Platform.ServiceCommands.CreateCommand.Execute()
at Microsoft.Crm.Application.Platform.DataSource.Create(Entity entity, Boolean performDuplicateCheck)
at Microsoft.Crm.Application.Platform.EntityProxy.Create(Boolean performDuplicateCheck)
at Microsoft.Crm.Application.Forms.AppForm.SaveEntity(EntityProxy entity, FormEventId eventType, String redirectPath, Boolean performDuplicateCheck)Wednesday, April 14, 2010 1:32 PM
Answers
-
You should be doing a CreateRequest not an UpdateRequest.
Blake Scarlavai - Sonoma Partners - http://www.sonomapartners.com/- Proposed as answer by mardukes Wednesday, April 14, 2010 5:04 PM
- Marked as answer by DavidJennawayMVP, Moderator Thursday, July 1, 2010 12:17 PM
Wednesday, April 14, 2010 1:43 PM