This question already has an answer here :
Closed 4 years ago .
public static List<priceBookEntryWrapper> searchproduct(String sampleId, String seriesValue, list<string> gradeValues){
system.debug('seriesValue->->->->->->->->->->'+seriesValue);
system.debug('gradevalues>>>>>'+gradeValues);
Map<String,priceBookEntryWrapper> backendData = new Map<String,priceBookEntryWrapper>();
String setPriceBookId;
Sample__c p = new Sample__c();
p=[select Id, Price_Book__c,Full_Fillment_Type__c from Sample__c where Id =: sampleId LIMIT 1];
if (p.Price_Book__c != null) {
// System.debug('Id'+pList<priceBookEntryWrapper>.Id);
System.debug('Price_Book__c'+p.Price_Book__c);
setPriceBookId=p.Price_Book__c;
System.debug('setPriceBookId'+setPriceBookId);
}
List<String> conditions = new List<String>();
string query='Select PriceBook2.Name,Product2Id,Product2.Stock_Status__c,product2.discipline__c, product2.subject__c,Product2.Name,UnitPrice,Product2.Series__c,Product2.Book_type__c,Product2.Grades__c,Product2.ISBN__c,Product2.Category__c,Product2.Author_Name__c,Product2.Published_Year__c,Product2.IsActive,Product2.Is_Sample__c,Pricebook2Id From PricebookEntry where (PricebookEntry.IsActive = TRUE OR PricebookEntry.UseStandardPrice=TRUE) AND Product2.IsActive=TRUE AND Product2.Is_Sample__c=TRUE and Product2.Grades__c != null AND Product2.Book_type__c != null AND Pricebook2Id =:setPriceBookId';
if(seriesValue!=null&&seriesValue!=''){
System.debug('seriesValue'+seriesValue);
conditions.add('Product2.Series__c=:seriesValue');
system.debug('for series Value>>>>>>>>'+conditions);
}
if(gradeValues!=null && gradeValues.size()>0){
conditions.add('Product2.Grades__c IN : gradeValues');
}
if (conditions.size() > 0) {
for (Integer i = 0; i < conditions.size(); i++)
query += ' AND ' + conditions[i];
}
list<PricebookEntry> sObjectList = Database.query(query);
System.debug('sObjectList'+sObjectList);
Map<String,Boolean> checkvalues1 = new Map<String,Boolean>();
system.debug('checkvalues1>>>>Map>>>>>>>'+checkvalues1);
Map<String,Boolean> checkbooktype1 = new Map<String,Boolean>();
Map<String,Boolean> chkBooktype1 = new Map<String,Boolean>();
// Boolean chkBooktype;
for(PricebookEntry pbe : sObjectList){
System.debug('pbe'+pbe);
Map<String,Boolean> checkvalues = new Map<String,Boolean>();
Map<String,Boolean> checkbooktype = new Map<String,Boolean>();
Map<String,Boolean> chkBooktype = new Map<String,Boolean>();
priceBookEntryWrapper priceEntryObj = new priceBookEntryWrapper();
productWrapper prodW = new productWrapper();
prodW.productId = pbe.Product2.Id;
prodW.productName = pbe.Product2.Name;
prodW.series = pbe.Product2.Series__c;
prodW.grade= pbe.Product2.Grades__c;
prodW.booktype = pbe.Product2.Book_Type__c;
prodW.UnitPrice= pbe.UnitPrice;
if(prodW.series.contains(seriesValue) && prodW.grade != null && prodW.booktype !=null ){
System.debug('True for map' +prodW.series +' Grade--> '+prodW.grade +'booktype---> '+prodW.booktype);
if(!checkvalues1.containskey(prodW.grade))
checkvalues.put(prodW.grade,true);
checkvalues1.put(prodW.grade,true);
if(!checkbooktype1.containskey(prodW.booktype)){
checkbooktype.put(prodW.booktype,true);
checkbooktype1.put(prodW.booktype,true);
chkBooktype.put(prodW.booktype,true);
chkBooktype1.put(prodW.booktype,true);
}
}
priceEntryObj.parentSeries=seriesValue;
priceEntryObj.reqCourseBookQuantity=1;
priceEntryObj.reqWorkBookQuantity=1;
priceEntryObj.reqLiteratureReaderQuantity=1;
priceEntryObj.reqTeacherBookQuantity=1;
priceEntryObj.reqTeacherCDQuantity=1;
priceEntryObj.mapofGrades = checkvalues;
priceEntryObj.mapofBookTypes=checkbooktype;
priceEntryObj.mapofcheckBookType = chkBooktype;
priceEntryObj.prodWrapList = new List<productWrapper>();
priceEntryObj.prodWrapList.add(prodW);
backendData.put(prodW.productId,priceEntryObj);
}
system.debug('Backend Values'+backendData.values());
return backendData.values();
}
sObjectList
will not be empty and It will cover your loop