Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TKMB2B_MultipleCCSpecProductsController {
- @AuraEnabled
- public static List<ccrz__E_Product__c> fetchProducts(String specId,Integer recordLimit, Integer recordOffset ,String searchTerm){
- Integer intLimit = Integer.valueof(recordLimit);
- Integer intOffset = Integer.valueof(recordOffset);
- List<ccrz__E_Product__c> products = new List<ccrz__E_Product__c>();
- List<String>productIds = specProducts(specId);
- List<String>productStatus = new List<String>{'Released'};
- String query = 'SELECT Id, Name, ccrz__SKU__c, ccrz__ProductStatus__c FROM ccrz__E_Product__c ';
- String whereClause = 'Where ccrz__ProductStatus__c IN :productStatus ';
- if(String.isNotBlank(searchTerm)){
- searchTerm = '%' + String.escapeSingleQuotes(searchTerm.trim()) + '%';
- whereClause+=' AND (Name Like :searchTerm OR ccrz__SKU__c Like:searchTerm) ';
- }
- if(productIds!=null){
- whereClause+=' AND Id <>:productIds ';
- }
- whereClause+=' LIMIT :intLimit Offset :intOffset';
- products = Database.query(query+whereClause);
- return products;
- }
- public static List<String> specProducts(String specId){
- List<String> ids = new List<String>();
- for(ccrz__E_ProductSpec__c productSpecs: [Select Id,ccrz__Product__c,ccrz__SpecValue__c ,ccrz__Spec__c FROM ccrz__E_ProductSpec__c where ccrz__Spec__c=:specId]){
- ids.add(productSpecs.ccrz__Product__c);
- }
- return ids;
- }
- @AuraEnabled
- public static Map<String,Object> saveProducts(String specId, List<String> productIds){
- Map<String,Object> response = new Map<String,Object>();
- try{
- List<ccrz__E_ProductSpec__c> productSpecs= [Select Id,ccrz__Product__c,ccrz__SpecValue__c ,ccrz__Spec__c FROM ccrz__E_ProductSpec__c where ccrz__Spec__c=:specId AND ccrz__Product__c IN:productIds];
- delete productSpecs;
- List<ccrz__E_Spec__c> specList =[SELECT Id,Name FROM ccrz__E_Spec__c WHERE Id=:specId Limit 1];
- List<ccrz__E_ProductSpec__c> newProductSpecs = new List<ccrz__E_ProductSpec__c>();
- for(ccrz__E_Spec__c spec: specList ){
- for(String productId : productIds){
- ccrz__E_ProductSpec__c productSpec = new ccrz__E_ProductSpec__c();
- productSpec.ccrz__Product__c = productId;
- productSpec.ccrz__SpecValue__c = spec.Name;
- productSpec.ccrz__Spec__c = spec.Id;
- newProductSpecs.add(productSpec);
- }
- }
- if(newProductSpecs.size() >0){
- insert newProductSpecs;
- }
- response.put('success',true);
- }catch(Exception e){
- response.put('success',false);
- }
- return response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement