I am working on a website on Salesforce. I have two main pages - login, home. On the home page, I just rerender different blocks with products of various types, information about the site and shopping cart. Each product is actually a record of Product__c custom type with fields like Name, Price__c, Description__c, Quantity__c and Category__c (TVs, laptops, mobile phones, monitors). I am able to add some products to my shopping cart. And in the shopping cart block, I can remove items from the list and buy them. So I need to write trigger - when the number of items in the particular category reaches 3, 5 more items should be added to this category. I tried to implement it on laptops category and then repeat for others, but after I buy all the products from a category it is still not working. And I do not really know how to create new products when each of them have unique name and description. I would really appreciate some help with that.
Here is trigger code:
trigger ProductTrigger on Product__c (after update) {
if(Trigger.isAfter){
if(Trigger.isUpdate){
new TestContoller().onAfterUpdate();
}
}
}
It is the method handler from my controller for the page and method which counts total quantity of objects in category:
private List<Product__c> selectLaptopsList() {
List<Product__c> laptops = [
SELECT Id
, Name
, Price__c
, Description__c
, Image__c
, Quantity__c
FROM Product__c
WHERE Category__c = 'Laptops'
];
return laptops;
}
private Decimal countLaptopsNumber() {
Decimal laptopsNumber = 0;
for (Product__c laptop : laptopsList) {
laptopsNumber += laptop.Quantity__c;
}
return laptopsNumber;
}
public void onAfterUpdate(){
List<Product__c> newLaptops = List<Product__c>();
if(laptopsQuantity < 3){
for(int i = 0; i < 6; i++){
newLaptops[i] = new Product__c();
}
}
}