You need to sign in to do that
Don't have an account?
MaheemSam
How to remove duplicate values in the list
Hi,
I need a suggestion how to remove duplicate values in the list I wrote a method which will display value from SOQL query return now there are lot of duplicate values which need to be removed and display only the unique values
Please suggest me how to remove the duplicate values in the list.
Method
I need a suggestion how to remove duplicate values in the list I wrote a method which will display value from SOQL query return now there are lot of duplicate values which need to be removed and display only the unique values
Please suggest me how to remove the duplicate values in the list.
Method
/* Display product category select list */ public List<SelectOption> ProdCatList { get { Set<String> setprocat = new Set<String>(); ProdcatLst = [Select PricebookEntry.Product2.Category__c FROM OpportunityLineItem WHERE OpportunityId = :PageID]; ProdCatList = new List<SelectOption>(); for(OpportunityLineItem opl: ProdcatLst ) { ProdCatList.add(new SelectOption(opl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c)); setprocat.add(opl.PricebookEntry.Product2.Category__c); } return ProdCatList ; } set; }Visual force
<apex:selectOptions value="{!ProdCatList}"></apex:selectOptions> </apex:selectList> <apex:inputText id="prodcatdiscount" value="{!prodcatdiscount }" label="Product Discount"/>
for(OpportunityLineItem opl: ProdcatLst )
{
if(!setprocat.contains(opl.PricebookEntry.Product2.Category__c))
ProdCatList.add(newSelectOption(opl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c)); setprocat.add(opl.PricebookEntry.Product2.Category__);
}
return ProdCatList ;
All Answers
HI Sudhir,
rather than using a list you can use maps
declare as map<string,string> ProdCatmap= new map<string,string>(); and add this to options .
for(OpportunityLineItem opl: ProdcatLst ) {
ProdCatmap.put(pl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c));
ProdCatList.add(newSelectOption(ProdCatmap))
setprocat.add(opl.PricebookEntry.Product2.Category__c);
}
return ProdCatList ;
Please find error below
Error: Compile Error: Method does not exist or incorrect signature: newSelectOption(Map<String,String>)
You reate one more list called UniqProdCatList. Loop through ProdCatList and then inside the loop you write a for loop to loop through UniqProdCatList (if UniqProdCatList .length>0) and add the sobjects to UniqProdCatList If ProdCatList sobject is not there in the UniqProdCatList and return UniqProdCatList .
code would be something like this,
Pls mark as solved if it helps you
Regards
I created another list as you suggested
List<SelectOption> UniqProdCatList = new List<SelectOption>();
I get error in for loop please find below also i cannot use If(UniqueProdCatLst .length>0)
Error: Compile Error: Loop variable must be of type System.SelectOption
Thanks
Sudhir
for(OpportunityLineItem opl: ProdcatLst )
{
if(!setprocat.contains(opl.PricebookEntry.Product2.Category__c))
ProdCatList.add(newSelectOption(opl.PricebookEntry.Product2.Category__c,opl.PricebookEntry.Product2.Category__c)); setprocat.add(opl.PricebookEntry.Product2.Category__);
}
return ProdCatList ;