Skip to main content
Hi,

I need to add discount to line item dynamically. For eg: If a customer orders mac + iphone it should add 10% discount, mac + samssung 15% discount, again mac + iphone + samsung 25% should get added. It should check for all possible products and its discount. Can anyone help me with it? 

Thanks,

Vetri
7 risposte
  1. 23 apr 2015, 07:10
    Hi Vetri,

    The requirement is not straight forward and would require customizations. I will help you with the design, and then may be you can start building the required objects etc.

    Design Approach - 

    1. Custom Object (Product Discounts) - Create a new object that stores all possible combinations of products that can exist for an opportunity with the respective discounts. Use a multi-select picklist field for storing the Product Combinations.

    2. On Creating a new Line Item record a piece of code should do the following - 

           - Query the Custom object with the product combination present on the respective opportunity including this newly created Product

           - If the query returns a record then use the discount value mentioned in that record and update the opportunity Total Discount field

    Example: If an Opportunity already has two line items with Products A and B respectively. When the user creates another line item with Product = C, the code should check if the Custom Object contains a record with the combination of A, B and C. If yes then the respective discount value should be used to update the Total Discount field on Opportunity.

    Approach for checking Product Combination - 

    1. Use a multi-select picklist field for storing the Product Combinations and another field to store the discounts.

    2. When a new line item is created, create a string containing all the Products for that opportunity delimited with a semicolon ';'

    3. This should return a single record of the Custom Object that matches the same combination of Products

    4. Use the discount value of the retrieved record and update the Total Discount field of Opportunity

    Point to Consider - 

    You can choose between a Trigger and the new Process Builder for invoking this piece of code when a new line item record is created.

    Sample lines of code to compare the combination of products with the Custom Object - 

    //--- For Loop that traverses all the line item records for an opportunity and creates a ; delimited string of all Products

    For (OpportunityLineItem oLI : [select Product2 from OpportunityLineItem

    where OpportunityId = :ParentId]) {

    //--- Declare ProductList as a String outside this For Loop

    if (ProductList == '')

    ProductList = oLI.Product2;

    else

    ProductList = ProductList + ';' + oLI.Product2;

    }

    //--- Product_Discounts__c is the new Custom Object created to hold the discount information

    //--- MultiSelect_1__c is the multi select picklist

    //--- Discount__c is the field to hold the Discount for the Product Combination

    //--- This query checks the combination of line items with the Custom Object records. The order of Products in ProductList can be different than MultiSelect_1__c, it does not matter.

    List<Product_Discounts__c> pDis = [select Id, MultiSelect_1__c, Discount__c from Product_Discounts__c

    where MultiSelect_1__c = :ProductList];

    if(pDis.size() > 0)

    System.debug('Found a record with Discount equal to ' + pDis[0].Discount__c);

    I have tried to be elaborative in explanation. I hope this solves your problem.

    Thanks

    AR

    If you find this reply useful that solves your problem then please mark it as best answer.