1

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();
        }
    }
 }
2
  • Hello, i m just trying to understand your scenario, per my understanding, you want to add 5 extra products on same category in to cart when a particular product quantity reaches 3 on a particular category. Is that right ? Commented Mar 10, 2017 at 11:44
  • @erkançipil yeah, that`s right
    – Cassie
    Commented Mar 10, 2017 at 12:03

1 Answer 1

1

Try something like below :

 private List<AggregateREsult> selectLaptopsList() {
    List<AggregateREsult> laptops = [
            SELECT cnt COUNT(Quantity__c),r Id
            FROM Product__c
            WHERE Category__c = 'Laptops'
            Group by Id 
            Having COUNT(Quantity__c) <=3
    ];
    return laptops;
}

public void onAfterUpdate(){
    List<AggregateREsult> newLaptops = selectLaptopsList();
    list<Product__C> plist = new list<Product__C>();
    for(AggregateREsult ag : v){
            for(i=0 ; i <5 ; i++)
            plist.add(Product__c.getsobjecttype.newSobject((string)ag.get('r')));
    }
    insert pList;
 }
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .