function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pallavi singhPallavi singh 

Lightning component to show all the cases with duplicates

Can we acheive this through lightning component to show the duplicate cases on the bases of Email, Casenumber and in subject if casenumber is mentioned.
Something like this.

User-added image
Thank you in advance
Best Answer chosen by Pallavi singh
Prateek Prasoon 25Prateek Prasoon 25
<aura:component controller="DuplicateCasesController">
    <aura:attribute name="email" type="String" />
    <aura:attribute name="caseNumber" type="String" />
    <aura:attribute name="subject" type="String" />
    <aura:attribute name="duplicateCases" type="Case[]" />
    
    <lightning:input label="Email" value="{!v.email}" />
    <lightning:input label="Case Number" value="{!v.caseNumber}" />
    <lightning:input label="Subject" value="{!v.subject}" />
    
    <lightning:button label="Search for Duplicates" onclick="{!c.searchForDuplicates}" />
    
    <aura:if isTrue="{!v.duplicateCases.length > 0}">
        <table>
            <thead>
                <tr>
                    <th>Case Number</th>
                    <th>Subject</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration items="{!v.duplicateCases}" var="case">
                    <tr>
                        <td>{!case.CaseNumber}</td>
                        <td>{!case.Subject}</td>
                        <td>{!case.Contact.Email}</td>
                    </tr>
                </aura:iteration>
            </tbody>
        </table>
    </aura:if>
</aura:component>



Create an Apex controller:
public with sharing class DuplicateCasesController {
    @AuraEnabled
    public static List<Case> searchForDuplicates(String email, String caseNumber, String subject) {
        List<Case> duplicateCases = new List<Case>();
        
        // Query for cases with the same email and case number, and same subject if the case number is mentioned
        String caseNumberCondition = String.isBlank(caseNumber) ? '' : 'AND CaseNumber = :caseNumber';
        String subjectCondition = String.isBlank(caseNumber) ? '' : 'AND Subject = :subject';
        List<Case> cases = [SELECT CaseNumber, Subject, Contact.Email FROM Case WHERE Contact.Email = :email :caseNumberCondition :subjectCondition];
        
        // Filter out the current case (if any) from the list of duplicates
        String currentCaseId = ApexPages.currentPage().getParameters().get('id');
        if (currentCaseId != null) {
            cases = cases.filter(c => c.Id != currentCaseId);
        }
        
        // Add the duplicate cases to the list to be returned
        duplicateCases.addAll(cases);
        
        return duplicateCases;
    }
}

Create the Lightning component controller:
({
    searchForDuplicates : function(component, event, helper) {
        var email = component.get("v.email");
        var caseNumber = component.get("v.caseNumber");
        var subject = component.get("v.subject");
        
        // Call the Apex controller to search for duplicate cases
        var action = component.get("c.searchForDuplicates");
        action.setParams({
            email: email,
            caseNumber: caseNumber,
            subject: subject
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var duplicateCases = response.getReturnValue();
                component.set("v.duplicateCases", duplicateCases);
            }
        });
        $A.enqueueAction(action);
    }
})

If you find this answer helpful, Please mark it as the best answer.

All Answers

Gian Piere VallejosGian Piere Vallejos
Of course that you can. You just need an Apex query that gets all that duplicate cases and show it in the component through a datatable. 
SubratSubrat (Salesforce Developers) 
Hello Pallavi ,

Assuming that you want to retrieve duplicate cases based on a specific field (e.g. Case Number), you can use the following Apex query to fetch the duplicate cases:
 
List<Case> duplicateCases = [SELECT CaseNumber, Count(Id) FROM Case GROUP BY CaseNumber HAVING Count(Id) > 1];

This query will retrieve all the cases that have a duplicate CaseNumber, along with the count of duplicate cases. You can then use this list to populate the datatable in your component.

After this you can create a VF page to display the duplicate cases in a datatable.

If it helps please mark this as best answer.
Thank you.
Prateek Prasoon 25Prateek Prasoon 25
<aura:component controller="DuplicateCasesController">
    <aura:attribute name="email" type="String" />
    <aura:attribute name="caseNumber" type="String" />
    <aura:attribute name="subject" type="String" />
    <aura:attribute name="duplicateCases" type="Case[]" />
    
    <lightning:input label="Email" value="{!v.email}" />
    <lightning:input label="Case Number" value="{!v.caseNumber}" />
    <lightning:input label="Subject" value="{!v.subject}" />
    
    <lightning:button label="Search for Duplicates" onclick="{!c.searchForDuplicates}" />
    
    <aura:if isTrue="{!v.duplicateCases.length > 0}">
        <table>
            <thead>
                <tr>
                    <th>Case Number</th>
                    <th>Subject</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration items="{!v.duplicateCases}" var="case">
                    <tr>
                        <td>{!case.CaseNumber}</td>
                        <td>{!case.Subject}</td>
                        <td>{!case.Contact.Email}</td>
                    </tr>
                </aura:iteration>
            </tbody>
        </table>
    </aura:if>
</aura:component>



Create an Apex controller:
public with sharing class DuplicateCasesController {
    @AuraEnabled
    public static List<Case> searchForDuplicates(String email, String caseNumber, String subject) {
        List<Case> duplicateCases = new List<Case>();
        
        // Query for cases with the same email and case number, and same subject if the case number is mentioned
        String caseNumberCondition = String.isBlank(caseNumber) ? '' : 'AND CaseNumber = :caseNumber';
        String subjectCondition = String.isBlank(caseNumber) ? '' : 'AND Subject = :subject';
        List<Case> cases = [SELECT CaseNumber, Subject, Contact.Email FROM Case WHERE Contact.Email = :email :caseNumberCondition :subjectCondition];
        
        // Filter out the current case (if any) from the list of duplicates
        String currentCaseId = ApexPages.currentPage().getParameters().get('id');
        if (currentCaseId != null) {
            cases = cases.filter(c => c.Id != currentCaseId);
        }
        
        // Add the duplicate cases to the list to be returned
        duplicateCases.addAll(cases);
        
        return duplicateCases;
    }
}

Create the Lightning component controller:
({
    searchForDuplicates : function(component, event, helper) {
        var email = component.get("v.email");
        var caseNumber = component.get("v.caseNumber");
        var subject = component.get("v.subject");
        
        // Call the Apex controller to search for duplicate cases
        var action = component.get("c.searchForDuplicates");
        action.setParams({
            email: email,
            caseNumber: caseNumber,
            subject: subject
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var duplicateCases = response.getReturnValue();
                component.set("v.duplicateCases", duplicateCases);
            }
        });
        $A.enqueueAction(action);
    }
})

If you find this answer helpful, Please mark it as the best answer.
This was selected as the best answer
Pallavi singhPallavi singh
Hi Prateek,

Thank you so much for your help.

But in the query [SELECT CaseNumber, Subject, Contact.Email FROM Case WHERE Contact.Email = :email :caseNumberCondition :subjectCondition];  can we just replace the sign '=:' as its throwing an error. 

Thank you
Pallavi