+ Start a Discussion
Cindy GUILLETCindy GUILLET 

Remove duplicates in a string

Hello,

I have a flow which add informations from children records of cases.
In the text field I obtain : 1.5.663,1.5.458,1.5.663,1.4.985
I need to remove duplicates in order to display : 1.5.663,1.5.458,1.4.985

I wrote a trigger based on what I found on many forums but I'm new on Apex and I can't figure it out.

trigger Trg_Case on Case (after update) {
    For(Case myCase: Trigger.new){
        if(myCase.ChildrenVersions__c != null && (myCase.ChildrenVersions__c).contains(',')){
            string reports = myCase.ChildrenVersions__c;
            List<String> l = new List<String>(new Set<String>(reports.split(',')));
            String uniqueReports = String.join(l, ',');
        }
    }
}

Thanks for your answers
Best Answer chosen by Cindy GUILLET
Deepali KulshresthaDeepali Kulshrestha
Hi Cindy,

Try the following code it may be helpful for you and it working fine in my ORG:
->Use Before Update event.
trigger Trg_Case on Case (before update) {
    For(Case myCase: Trigger.new){
        
        if(myCase.ChildrenVersions__c != null && (myCase.ChildrenVersions__c).contains(',')){
            string reports = myCase.ChildrenVersions__c;
            List<String> duplicateRemoveList = new List<String>(new Set<String>(reports.split(',')));
            String uniqueReport = String.join(duplicateRemoveList, ',');
            myCase.ChildrenVersions__c=uniqueReport;
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Cindy,

Try the following code it may be helpful for you and it working fine in my ORG:
->Use Before Update event.
trigger Trg_Case on Case (before update) {
    For(Case myCase: Trigger.new){
        
        if(myCase.ChildrenVersions__c != null && (myCase.ChildrenVersions__c).contains(',')){
            string reports = myCase.ChildrenVersions__c;
            List<String> duplicateRemoveList = new List<String>(new Set<String>(reports.split(',')));
            String uniqueReport = String.join(duplicateRemoveList, ',');
            myCase.ChildrenVersions__c=uniqueReport;
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
This was selected as the best answer
Ravi Dutt SharmaRavi Dutt Sharma
Hi Cindy,

Please try below and let me know if that works:
 
trigger Trg_Case on Case (after update) {
    For(Case myCase: Trigger.new){
        if(myCase.ChildrenVersions__c != null && (myCase.ChildrenVersions__c).contains(',')){
            String[] textArray = myCase.ChildrenVersions__c.split(',');
			Set<String> uniqueValues = new Set<String>();
			for(String str: textArray){
			    uniqueValues.add(str);
			}
			String values = String.join(new List<String>(uniqueValues), ',')
        }
    }
}

 
Ajay K DubediAjay K Dubedi
Hi Cindy,
Try below code in your trigger to remove duplicate values of a String it works fine:
 
String str = '1.5.663,1.5.458,1.5.663,1.4.985';
List<String> duplicateRemoveList = new List<String>(new Set<String>(str.split(',')));
String uniquestr = String.join(duplicateRemoveList, ',');
System.debug('uniquestr'+uniquestr);



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi 
Cindy GUILLETCindy GUILLET
Hello Everyone,

thank you for your help !
I used Deepali code and it works.

Final code :
trigger Trg_Case on Case (before update) {
    For(Case myCase: Trigger.new){
        
        if(myCase.ChildrenVersions__c != null && myCase.ChildrenVersions__c.contains('\n')){
            string reports = myCase.ChildrenVersions__c;
            List<String> duplicateRemoveList = new List<String>(new Set<String>(reports.split('\n')));
            String uniqueReport = String.join(duplicateRemoveList, '\n');
            myCase.ChildrenVersions__c=uniqueReport;
        }
    }
}

 
VICKY_SFDCVICKY_SFDC
100% WORKING CODE


public class ReplaceDuplicate {
    public static void getTemplate() {       
        String str = '1.5.663,1.5.458,1.5.663,1.4.985';
        
         List<String> duplicateRemoveList = new List<String>(new Set<String>(str.split(',')));
         String uniquestr = String.join(duplicateRemoveList, ',');
         System.debug('uniquestr'+uniquestr);
    }
}