web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

how to close case using plugin

(0) ShareShare
ReportReport
Posted on by

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;
}
}
}

}

I have the same question (0)
  • Verified answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans