You need to sign in to do that
Don't have an account?
Alaric Wimer
Trigger not firing on Opportunity Line Item entry
Hi everyone, I have a trigger that should fire but it doesn't seem to work. Basically, if the Opportunity Product being added is an inverter, the trigger should check to see if there is a panel in the Opportunity Products and then set the quanitity of the inverter being added to a number based on the quantity of the panel.
The trigger worked for me when I hardcoded the Product2Id's but I want to avoid doing that. When I changed the Product2Id to Product2.Name the trigger stopped working.
Here is my code, any help is greatly appreciated!
trigger setInverterQuantity on OpportunityLineItem (before insert) { for (OpportunityLineItem oli : Trigger.new) { if (oli.Product2.Name == 'APSystems QS1' || oli.Product2.Name == 'Y6COO') { // inverter 1 or inverter 2 // get quantity of panels List<OpportunityLineItem> panels = [ SELECT Id, Quantity, Product2.Name FROM OpportunityLineItem WHERE OpportunityId = :oli.OpportunityId AND (Product2.Name = 'Heliene 60M-BLK 310W' OR Product2.Name = 'Heliene 60M-HBLK 300W') // panel 1 or panel 2 LIMIT 1 ]; if (!panels.isEmpty()) { // set inverter quantity formulas Decimal inverterQuantityOne = (panels.get(0).Quantity / 4); Decimal inverterQuantityTwo = (panels.get(0).Quantity / 2); inverterQuantityOne = inverterQuantityOne.round(System.RoundingMode.CEILING); inverterQuantityTwo = inverterQuantityTwo.round(System.RoundingMode.CEILING); // set quantity of inverter if (oli.Product2.Name == 'APSystems QS1') { // inverter 1 oli.Quantity = inverterQuantityOne; } else if (oli.Product2.Name == 'Y6COO') { // inverter 2 oli.Quantity = inverterQuantityTwo; } } } } }
Try the below changes,
trigger setInverterQuantity on OpportunityLineItem (before insert) {
Map<Id, Product2> prodMap = new Map<Id, Product2>([Select Id,Name from Product2
Where Name = 'APSystems QS1' OR Name = 'Y6COO']);
List<OpportunityLineItem> panels = [ SELECT Id, Quantity, Product2.Name FROM OpportunityLineItem
WHERE OpportunityId = :oli.OpportunityId
AND (Product2.Name = 'Heliene 60M-BLK 310W'
OR Product2.Name = 'Heliene 60M-HBLK 300W') LIMIT 1];
for (OpportunityLineItem oli : Trigger.new) {
if (prodMap.ContainsKey(oli.Product2Id)) { // inverter 1 or inverter 2
// get quantity of panels
if (!panels.isEmpty()) {
// set inverter quantity formulas
Decimal inverterQuantityOne = (panels.get(0).Quantity / 4);
Decimal inverterQuantityTwo = (panels.get(0).Quantity / 2);
inverterQuantityOne = inverterQuantityOne.round(System.RoundingMode.CEILING);
inverterQuantityTwo = inverterQuantityTwo.round(System.RoundingMode.CEILING);
// set quantity of inverter
if (prodMap.get(oli.Product2Id).Name == 'APSystems QS1') { // inverter 1
oli.Quantity = inverterQuantityOne;
} else if (prodMap.get(oli.Product2Id).Name == 'Y6COO') { // inverter 2
oli.Quantity = inverterQuantityTwo;
}
}
}
}
}
Thanks,
Maharajan.C
All Answers
Try the below changes,
trigger setInverterQuantity on OpportunityLineItem (before insert) {
Map<Id, Product2> prodMap = new Map<Id, Product2>([Select Id,Name from Product2
Where Name = 'APSystems QS1' OR Name = 'Y6COO']);
List<OpportunityLineItem> panels = [ SELECT Id, Quantity, Product2.Name FROM OpportunityLineItem
WHERE OpportunityId = :oli.OpportunityId
AND (Product2.Name = 'Heliene 60M-BLK 310W'
OR Product2.Name = 'Heliene 60M-HBLK 300W') LIMIT 1];
for (OpportunityLineItem oli : Trigger.new) {
if (prodMap.ContainsKey(oli.Product2Id)) { // inverter 1 or inverter 2
// get quantity of panels
if (!panels.isEmpty()) {
// set inverter quantity formulas
Decimal inverterQuantityOne = (panels.get(0).Quantity / 4);
Decimal inverterQuantityTwo = (panels.get(0).Quantity / 2);
inverterQuantityOne = inverterQuantityOne.round(System.RoundingMode.CEILING);
inverterQuantityTwo = inverterQuantityTwo.round(System.RoundingMode.CEILING);
// set quantity of inverter
if (prodMap.get(oli.Product2Id).Name == 'APSystems QS1') { // inverter 1
oli.Quantity = inverterQuantityOne;
} else if (prodMap.get(oli.Product2Id).Name == 'Y6COO') { // inverter 2
oli.Quantity = inverterQuantityTwo;
}
}
}
}
}
Thanks,
Maharajan.C
What if I wanted to add the logic that updates the quantity of inverters when panels are changed. For example, an opportunity has 20 panels and 5 inverters ('APSystems QS1'). A user updates the panels to 40, now the inverters quantity should be 10.
Would I have to create a separate trigger to handle this use case?