Advertisement
f1lam3ntx0

TKMB2B_MultipleCCSpecProductsController

Sep 22nd, 2020 (edited)
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. public class TKMB2B_MultipleCCSpecProductsController {
  2.    
  3.    
  4.     @AuraEnabled
  5.     public static List<ccrz__E_Product__c> fetchProducts(String specId,Integer recordLimit, Integer recordOffset ,String searchTerm){
  6.         Integer intLimit = Integer.valueof(recordLimit);
  7.         Integer intOffset = Integer.valueof(recordOffset);
  8.         List<ccrz__E_Product__c> products = new List<ccrz__E_Product__c>();
  9.         List<String>productIds = specProducts(specId);
  10.         List<String>productStatus = new List<String>{'Released'};
  11.         String query = 'SELECT Id, Name, ccrz__SKU__c, ccrz__ProductStatus__c FROM ccrz__E_Product__c ';
  12.        
  13.        
  14.         String whereClause = 'Where ccrz__ProductStatus__c IN :productStatus ';
  15.         if(String.isNotBlank(searchTerm)){
  16.            searchTerm = '%' + String.escapeSingleQuotes(searchTerm.trim()) + '%';
  17.             whereClause+=' AND  (Name Like :searchTerm OR ccrz__SKU__c Like:searchTerm) ';
  18.         }
  19.        
  20.         if(productIds!=null){
  21.              whereClause+=' AND  Id <>:productIds ';
  22.         }
  23.         whereClause+=' LIMIT :intLimit Offset :intOffset';
  24.         products = Database.query(query+whereClause);
  25.         return products;
  26.     }
  27.    
  28.     public static List<String> specProducts(String specId){
  29.         List<String> ids = new List<String>();
  30.         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]){
  31.             ids.add(productSpecs.ccrz__Product__c);
  32.         }
  33.         return ids;
  34.     }
  35.    
  36.      @AuraEnabled
  37.      public static Map<String,Object> saveProducts(String specId, List<String> productIds){
  38.         Map<String,Object> response = new Map<String,Object>();
  39.          try{
  40.             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];
  41.             delete productSpecs;
  42.             List<ccrz__E_Spec__c> specList =[SELECT Id,Name FROM ccrz__E_Spec__c WHERE Id=:specId Limit 1];
  43.             List<ccrz__E_ProductSpec__c> newProductSpecs = new List<ccrz__E_ProductSpec__c>();
  44.              for(ccrz__E_Spec__c spec: specList ){
  45.                  for(String productId : productIds){
  46.                      ccrz__E_ProductSpec__c productSpec  = new ccrz__E_ProductSpec__c();
  47.                      productSpec.ccrz__Product__c = productId;
  48.                      productSpec.ccrz__SpecValue__c = spec.Name;
  49.                      productSpec.ccrz__Spec__c = spec.Id;
  50.                      newProductSpecs.add(productSpec);
  51.                  }
  52.              }
  53.              if(newProductSpecs.size() >0){
  54.                  insert newProductSpecs;
  55.              }
  56.              response.put('success',true);
  57.          }catch(Exception e){
  58.              response.put('success',false);
  59.          }
  60.         return response;
  61.     }
  62.      
  63.  
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement