function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vetriselvan ManoharanVetriselvan Manoharan 

Adding discount to line item

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
Best Answer chosen by Vetriselvan Manoharan
Anupam RastogiAnupam Rastogi
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.
 

All Answers

Anupam RastogiAnupam Rastogi
Hi Vetri,

Please share some more information on this requirement as per the questions below - 

 - Can you elaborate more on how this discount is calculate for each combination of products chosen?
 - Is there a table/object in Salesforce that maintains this information which needs to be referenced dynamically when a combination of product is chosen?
 - As I understand, there will be a separate line item for each product eg. separate line items for mac and iPhone. So that means that you wish to kind-of roll up the individual discounts of each product onto the parent Quote (or which ever entity)?

Thanks
AR
Vetriselvan ManoharanVetriselvan Manoharan
Hi Anupam,

Yes we are planning to have a custom object which has the combination of products and their discount. The line item will be seperate for each product. How do I get the combination correctly for applying discount in line item based on the combination.

Thanks,
Vetri
Anupam RastogiAnupam Rastogi
Hi Vetri,

Also let me know where exactly this discount figure will be shown on the UI, because there will be separate line items for each product therefore it should be desirable to show it on the Parent object. Something like Total Discount.

Please confirm.

Thanks
AR
Vetriselvan ManoharanVetriselvan Manoharan

Hi Anupam,

Yes that would be fine I guess. We can show the discount in opportunity object and amount field in opportunity object should change based on it if the combination is correct. It would great if you help me to do it.

Thanks,
Vetri
Vetriselvan ManoharanVetriselvan Manoharan
I just need the core logic of comparing the line item with custom object which is going to be created. If a product added in line item matches the combination discount should be added to it.
Anupam RastogiAnupam Rastogi
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.
 
This was selected as the best answer
Vetriselvan ManoharanVetriselvan Manoharan
Hi Anupam,

Thanks for idea!!! thats great. I will let you know if I need any help

Thanks,
Vetri