1global List<PricebookEntry> search(SObject quote, Map<String,Object> fieldValuesMap){
2 // Get all possible filter fields from the search filter field set
3 List<Schema.FieldSetMember> searchFilterFieldSetFields = SObjectType.Product2.FieldSets.SBQQ__SearchFilters.getFields();
4 // Get all possible fields from the search result field set
5 List<Schema.FieldSetMember> searchResultFieldSetFields = SObjectType.Product2.FieldSets.SBQQ__SearchResults.getFields();
6 // Build the Select string
7 String selectClause = 'SELECT ';
8 for(Schema.FieldSetMember field : searchResultFieldSetFields) {
9 selectClause += 'Product2.' + field.getFieldPath() + ', ';
10 }
11 selectClause += 'Id, UnitPrice, Pricebook2Id, Product2Id, Product2.Id';
12 // Build the Where clause
13 String whereClause = '';
14 for(Schema.FieldSetMember field : searchFilterFieldSetFields) {
15 if(!fieldValuesMap.containsKey(field.getFieldPath())) {
16 continue;
17 }
18 if(field.getType() == Schema.DisplayType.String || field.getType() == Schema.DisplayType.Picklist) {
19 whereClause += 'Product2.' + field.getFieldPath() + ' LIKE \'%' + fieldValuesMap.get(field.getFieldPath()) + '%\' AND ';
20 }
21 }
22 whereClause += 'Pricebook2Id = \'' + quote.get('SBQQ__Pricebook__c') + '\'';
23 // Build the query
24 String query = selectClause + ' FROM PricebookEntry WHERE ' + whereClause;
25 // Perform the query
26 List<PricebookEntry> pbes = new List<PricebookEntry>();
27 pbes = Database.query(query);
28 return pbes;
29}