Вы находитесь на странице: 1из 4

public class Surajit1ProductWrapperClassController { //Our collection of the class/wrapper objects public List<ProductWrapper> listProductWrapper {get; set;} public List<ProductWrapper>

getBottleProductWrapper {get; set;} public List<ProductWrapper> getCasesProductWrapper {get; set;} public List<ProductWrapper> getKegsProductWrapper {get; set;} public List<ProductWrapper> getcartonsProductWrapper {get; set;}public Map<Id,St ring> mapProductByBaseUnit {get; set;} //This is just a temp work around till we get Customer Portal up -and runnin g Contact objContact = [SELECT ID,NAME FROM CONTACT LIMIT 1]; List<Sales_Order_Header__c> listSalesOrderHeader =[Select ID,Name From Sales _Order_Header__c where Account_Contact__r.Contact__c=:objContact.ID AND Status__ C ='Draft'LIMIT 1]; //Constructor of the class.1 public Surajit1ProductWrapperClassController (){ getProducts(); } //Method to fetch all the details of products and will be shown to a user. public void getProducts() { /* Collection [Key = SAP Material ID & value = Sales_Order_Item__c objec t] * This holds Sales_Order_Item__c records to fetch Quantity correspondin g * to a particular SAP Material ID and use it in Wrapper Method */ Map<String,Sales_Order_Item__c> mapSAP_MAT_ID_Sales_Order_Item = new Map <String,Sales_Order_Item__c>(); // Temporary list to hold Product data List<Product__c> listtempProducts = new List<Product__c>(); // This is based on the assumption that For a single Account & Contact c ombination only one Sales order header // record with status = Draft can be there. ID idSales_Order_Header = listSalesOrderHeader[0].ID; // Collection to contain unique SAP MAT ID w.r.t. Product Data Set<String> setSAP_MAT_ID = new Set<String>(); // This loop will populate Temp Product list declared above & also the S et collection declared above. mapProductByBaseUnit = new Map<Id,String>(); for(Product__c p : [Select Id, Name,SAP_Material_ID__c,Base_Unit_of_Meas ure__c ,Level_6_Desc__c,Base_Unit_of_Measure_Description__ c from Product__c where Hierarchy_Level__c = '7']){ listtempProducts.add(p); setSAP_MAT_ID.add(p.SAP_Material_ID__c); if(p.Base_Unit_of_Measure_Description__c!= null p.Base_Unit_of_Me asure_Description__c == ''){

if(p.Base_Unit_of_Measure_Description__c != mapProductByBaseUnit .get(p.ID)){ mapProductByBaseUnit.put(p.ID,p.Base_Unit_of_Measure_Descript ion__c); } } } // This loop will iterates over the Sales Order Item filtered on the Set of SAP MAterial Ids and // corresponding to Sales Order header (If there is any ). String strQuery = 'Select SAP_Material__c,Qty_Ordered__c from Sales_Orde r_Item__c where SAP_Material__c IN: setSAP_MAT_ID '; if(idSales_Order_Header!= null){ strQuery += 'AND Sales_Order_Header__c=:idSales_Order_Header'; } for(SObject so:Database.query(strQuery)){ Sales_Order_Item__c soi = (Sales_Order_Item__c )so; mapSAP_MAT_ID_Sales_Order_Item.put(soi.SAP_Material__c,soi); } // If List Of wrapper is empty if(listProductWrapper == null){ listProductWrapper = new List<ProductWrapper>(); // Product iteration for(Product__c p : listtempProducts ){ // If the Corresponding Sap Material Id is not null i.e. in case an order exists w.r.t. to any Header & has items //if(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID__c )!= null ) //{ if(p.Base_Unit_of_Measure_Description__c == 'Kegs'){ if(listProductKegs == null){ listProductKegs = new List<ProductWrapper>(); } if(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID_ _c )!=null) listProductKegs.add(new ProductWrapper(p,Integer.val ueof(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID__c ).Qty_Ordered__c) ) ); else listProductKegs.add(new ProductWrapper(p,1)); } else if(p.Base_Unit_of_Measure_Description__c == 'Case'){ if(listProductCases == null){ listProductCases = new List<ProductWrapper>(); } if(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID_ _c )!=null) listProductCases.add(new ProductWrapper(p,Integer.va

lueof(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID__c ).Qty_Ordered__c) )); else listProductCases.add(new ProductWrapper(p,1));

} else if(p.Base_Unit_of_Measure_Description__c == 'Bottles') { if(listProductBottles == null){ listProductBottles = new List<ProductWrapper>() ; } if(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID_ _c )!=null) listProductBottles.add(new ProductWrapper(p,Integer. valueof(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID__c ).Qty_Ordered__c ) )); else listProductBottles.add(new ProductWrapper(p,1)); } else if(p.Base_Unit_of_Measure_Description__c == 'Cartons'){ if(listProductCartons == null){ listProductCartons = new List<ProductWrapper>() ; } if(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID __c )!=null) listProductCases.add(new ProductWrapper(p,Integer.va lueof(mapSAP_MAT_ID_Sales_Order_Item.get(p.SAP_Material_ID__c ).Qty_Ordered__c) )); else listProductCartons.add(new ProductWrapper(p,1)); } else{ Integer j = 1; listProductWrapper.add(new ProductWrapper(p,Integer.valu eof(j))); } //} //else{ // mapSAP_MAT_ID_Sales_Order_Item.put(p.SAP_Material_ID__c,) //} } } system.debug('#####'+mapProductByBaseUnit+'#####'); For(ProductWrapper pw: listProductWrapper){ system.debug('#####'+pw.product.ID+'#####'); }

// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other ob jects. In this example a wrapper class contains both the custom salesforce objec t Product and a Integer value i.e. the quantity public class ProductWrapper{ public Product__c product {get; set;} public Integer quantity {get; set;} //This is the contructor method. When we create a new ProductWrapper obj ect we pass a Product that is set to the Product property. We also set the Quant ity value to 1 public ProductWrapper(Product__c paramProduct,Integer paramQuantity) { this.product = paramProduct; this.quantity = paramQuantity; } } }

Вам также может понравиться