Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

how to close case using plugin

Posted on by Microsoft Employee

if csu task rejected =>> close case can some one suggest me how to do that

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.Security.Principal;
using Microsoft.Xrm.Sdk.Query;
namespace taskcheck
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
trace.Trace("optain the tracking service");
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{

//obtain the target entity from the inputparameter
Entity target = (Entity)context.InputParameters["Target"];
//rettrive
Entity entity = service.Retrieve("task", target.Id, new ColumnSet(true));
//csutask status change
EntityReference regardingCaseRef = entity["reagardingobjectid"] as EntityReference;
//retrive regarding from the back end
Entity regardingCase = service.Retrieve(regardingCaseRef.LogicalName, regardingCaseRef.Id, new ColumnSet(true));
//retrive multiple using query expression
QueryExpression qeRelatedTasks = new QueryExpression("task");
qeRelatedTasks.ColumnSet = new ColumnSet(true);
qeRelatedTasks.Criteria.AddCondition(new ConditionExpression("regardingobjectid",ConditionOperator.Equal,regardingCaseRef.Id));
EntityCollection relatedTasks = service.RetrieveMultiple(qeRelatedTasks);
if (AllTaskReject(relatedTasks))
{
trace.Trace("Case title: " + regardingCase["title"]);
//statuscode

}

}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}

}

}

public bool AllTaskReject(EntityCollection relatedTasks)
{


if (relatedTasks.Entities.All(x => x.GetAttributeValue<OptionSetValue>("cos_csutaskstatus")?.Value == 769190003))
{
//Console.WriteLine("All task completed");
return true;
}
else
{
return false;
}
}
}

}

  • Verified answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: how to close case using plugin

    Hi minh nhat,

    Here is the code to close your case

    Entity IncidentResolution = new Entity("incidentresolution");
    IncidentResolution.Attributes["subject"] = "Incident Resolved";
    IncidentResolution.Attributes["incidentid"] = new EntityReference("incident", entity.Id);
    CloseIncidentRequest closeRequest = new CloseIncidentRequest();
    closeRequest.IncidentResolution = IncidentResolution;
    closeRequest.Status = new OptionSetValue(806570002);
    // Execute the close request
    _Service.Execute(closeRequest);

    If I answer your question then please mark it as verified.

    Let me know if I can provide you with more details.

    Thanks
    Regards,

    Abdul Wahab
    Power Platform/Customer Engagement Developer/Lead/Solution Architecture/Project Manager
    Direct/WhatsApp: 923323281237
    E-mail: abdulwahabubit@outlook.com
    Skype: abdul.wahabubit
    Linkedin: www.linkedin.com/.../

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans