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
SFDC pvSFDC pv 

How do i remove Duplicates below is my code. AuraEnabled methods do not support return type of Set<String> so i could not add the list to set and return the set value. Can someone help

    @auraEnabled
    Public static List<string> invokeWebServiceString ssnumber) {
        String [] arrayOfResponsestrings = new List<String>();

        for(String num : ssnumber.split(',')){          
            ResponseWrapper APIResponse = InvokeAPI.invokeRespAPI(num);
            arrayOfResponsestrings.add(JSON.serialize(APIResponse ));
            System.debug('arrayOfResponsestrings :'+arrayOfResponsestrings);
        }
        for(Integer i=0 ;i<arrayOfResponsestrings.size() ; i++) {
        }
        return arrayOfResponsestrings;
    } 
Best Answer chosen by SFDC pv
GulshanRajGulshanRaj
Hi @SFDC,

Why not you can do like this way:
String [] arrayOfResponsestrings = new List<String>();
//Set to collect unique values
Set<String> uniqueValues = new Set<String>();
for(String num : ssnumber.split(',')){          
    ResponseWrapper APIResponse = InvokeAPI.invokeRespAPI(num);
    uniqueValues.add(JSON.serialize(APIResponse ));
    System.debug('arrayOfResponsestrings :'+arrayOfResponsestrings);
}
//add unique values to list
arrayOfResponsestrings.addall(uniqueValues);
return arrayOfResponsestrings;

Please let me know if you have any question.


Thanks
Gulshan Raj

 

All Answers

Raj VakatiRaj Vakati
Try this code
 
@auraEnabled
    Public static List<string> invokeWebServiceString ssnumber) {
      //  String [] arrayOfResponsestrings = new List<String>();
Set<String> str = new Set<String>() ;
        for(String num : ssnumber.split(',')){          
            ResponseWrapper APIResponse = InvokeAPI.invokeRespAPI(num);
            arrayOfResponsestrings.add(JSON.serialize(APIResponse ));
            System.debug('arrayOfResponsestrings :'+arrayOfResponsestrings);
        }
        for(Integer i=0 ;i<arrayOfResponsestrings.size() ; i++) {
        }
		
		List<String> finalResult = new List<String>() ;
		finalResult.addAll(str);
        return str;
    }

 
Raj VakatiRaj Vakati
You can create a set --> after that create a list --> add all to the list from the set -->return the list 
SFDC pvSFDC pv
Hi Raj, 
Thanks for you reply. But i have to keep this statement String [] arrayOfResponsestrings = new List<String>(); as my response is coming in JSON format for some reason i have to get it in array format. without commenting that statement how do i remove duplicates. Please help 
GulshanRajGulshanRaj
Hi @SFDC,

Why not you can do like this way:
String [] arrayOfResponsestrings = new List<String>();
//Set to collect unique values
Set<String> uniqueValues = new Set<String>();
for(String num : ssnumber.split(',')){          
    ResponseWrapper APIResponse = InvokeAPI.invokeRespAPI(num);
    uniqueValues.add(JSON.serialize(APIResponse ));
    System.debug('arrayOfResponsestrings :'+arrayOfResponsestrings);
}
//add unique values to list
arrayOfResponsestrings.addall(uniqueValues);
return arrayOfResponsestrings;

Please let me know if you have any question.


Thanks
Gulshan Raj

 
This was selected as the best answer
SFDC pvSFDC pv
Hi @Gulshan, 

Thanks a lot that works like a charm.