Asked by:
CRM 2011 Update Opportunity on Task save from Plugin

Question
-
Hi,
How to write a plugin to update specified opportunity attributes when the taks entity is save ?
How to pass parameters to opportunity entity ?
Tuesday, January 7, 2014 7:23 AM
All replies
-
Hello,
You can write a create plugin on task entity and update regarding opportunity and set specific field using update method.
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Tuesday, January 7, 2014 9:32 AMModerator -
Thanks for reply
How to retrieve the regarding opportunity from task entity ?
I'm new in crm:)
- Edited by rogus1 Tuesday, January 7, 2014 9:46 AM
Tuesday, January 7, 2014 9:39 AM -
You need to check for regardingobjectid field, which will provide you opportunity id for which you are adding task.
HTH
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.Tuesday, January 7, 2014 9:46 AMModerator -
Hmm, can you show me sample code please ?
I don't know where to start;)
Tuesday, January 7, 2014 9:52 AM -
Hello,
For how to write plugin, please download ms crm SDK there are sample code available.
and to get regardingbjectid you can use like below
if(entityobject.Contains("regardingobjectid"))
{
Guid _OpportunityID=entity.GetAttributeValue<EntityReference>("regardingobjectid").Id;
}
Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.- Proposed as answer by HIMBAPModerator Tuesday, January 7, 2014 10:05 AM
Tuesday, January 7, 2014 10:05 AMModerator -
hmm, something like this ?
public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity entity = (Entity)context.InputParameters["Target"]; if (entity.LogicalName != "task") return; try { if (context.InputParameters.Contains("regardingobjectid")) { Guid _OpportunityID = entity.GetAttributeValue<EntityReference>("regardingobjectid").Id; } IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); service.Create(entity); } catch (FaultException<OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("Error", ex); } } }
I don't know what next with _OpportunityID ?
Tuesday, January 7, 2014 10:53 AM