locked
create task on resolve of case using plugin RRS feed

  • Question

  • how to create a task on resolving a case in ms crm using plugin.
    Thursday, May 21, 2015 6:37 AM

Answers

  • Amy,

    Incident Resolution is an entity. You are trying to attach a not to an incident resolution entity. You need to attach the note to the incedent entity. You should retrieve the incident first and work you way down to create the note with the incident:

     if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
    
    if(!entity.containsKey("incidentid"))
    return;
    
    Entity TheRealIncidentAndNotTheIncidentResolutionRecord = service.Retrieve("incident", ((EntityReference)entity["incidentid"]).Id, new ColumnSet(true));
    
    
    
    
    ...
    }

    • Marked as answer by Amy.4 Thursday, May 28, 2015 6:14 AM
    Friday, May 22, 2015 10:43 AM
  • Hi,

    Your plugin is not working, because you creating a new entity:

    Entity Note = new Entity("annotation");

    and then trying to change entity attributes:

    Note["notetext"] = "Hello!! Created a note";
    Note["ObjectId"] = new EntityReference(context.PrimaryEntityName, new Guid(context.PrimaryEntityId.ToString()));
    Note["Subject"] = "Your Title";

    Witch are not exits in your entity.

    You should use

    Note.Attributes.Add()
    function to add an attribute

    https://msdn.microsoft.com/en-us/library/gg307396.aspx

    For example:

    instead of:

    Note["notetext"] = "Hello!! Created a note";

    You should write:

    Note.Attributes.Add(new KeyValuePair<string, object>("notetext", "Hello!! Created a note"));


    • Edited by xjomanx Friday, May 22, 2015 10:53 AM
    • Marked as answer by Amy.4 Thursday, May 28, 2015 6:14 AM
    Friday, May 22, 2015 10:52 AM

All replies

  • Hi,

    To create an Entity in plugin you can use:

    service.create();

    More info: https://msdn.microsoft.com/en-us/library/gg594416.aspx

    Thursday, May 21, 2015 12:43 PM
  • Amy,

    This example shows you how to create a task when an Account record is created. Just use this code in your resolve case plugin. This example shows you how to write a plugin when the case is resolved.

    Hope it helps,

    Kind Regards

    Friday, May 22, 2015 6:44 AM
  • Hi Jochen , i have been using the below code but unable to create the note.. any suggestions on this if i need to change anything... in the sdk i have registered the plugin on create of "incident resolution"

    My aim is to create a note whenever a case is resolved....

     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));
    
                // Create service with context of current user
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                tracingService.Trace("input parameters");
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                                
                    if (((OptionSetValue)entity.Attributes["statuscode"]).Value == 5) //Status Reason = Problem solved
                    {
                        Entity Note = new Entity("annotation");
                      
                        Note["notetext"] = "Hello!! Created a note";
                        
                        Note["ObjectId"] = new EntityReference(context.PrimaryEntityName, new Guid(context.PrimaryEntityId.ToString()));
                       
                        Note["Subject"] = "Your Title";
                       
                        Guid annotationId = service.Create(Note);

    Friday, May 22, 2015 10:23 AM
  • Amy,

    Incident Resolution is an entity. You are trying to attach a not to an incident resolution entity. You need to attach the note to the incedent entity. You should retrieve the incident first and work you way down to create the note with the incident:

     if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
    
    if(!entity.containsKey("incidentid"))
    return;
    
    Entity TheRealIncidentAndNotTheIncidentResolutionRecord = service.Retrieve("incident", ((EntityReference)entity["incidentid"]).Id, new ColumnSet(true));
    
    
    
    
    ...
    }

    • Marked as answer by Amy.4 Thursday, May 28, 2015 6:14 AM
    Friday, May 22, 2015 10:43 AM
  • Hi,

    Your plugin is not working, because you creating a new entity:

    Entity Note = new Entity("annotation");

    and then trying to change entity attributes:

    Note["notetext"] = "Hello!! Created a note";
    Note["ObjectId"] = new EntityReference(context.PrimaryEntityName, new Guid(context.PrimaryEntityId.ToString()));
    Note["Subject"] = "Your Title";

    Witch are not exits in your entity.

    You should use

    Note.Attributes.Add()
    function to add an attribute

    https://msdn.microsoft.com/en-us/library/gg307396.aspx

    For example:

    instead of:

    Note["notetext"] = "Hello!! Created a note";

    You should write:

    Note.Attributes.Add(new KeyValuePair<string, object>("notetext", "Hello!! Created a note"));


    • Edited by xjomanx Friday, May 22, 2015 10:53 AM
    • Marked as answer by Amy.4 Thursday, May 28, 2015 6:14 AM
    Friday, May 22, 2015 10:52 AM
  • Thank you for the response Jochen and Xjomanx .. also can i know on which meesage and entity(incident or incidentresolution) this needs to be registered in sdk..?? 
    Friday, May 22, 2015 11:35 AM
  • Incidentresolution and get the incident from the target like I mentioned above.

    Kind Regards

    Friday, May 22, 2015 11:38 AM