You need to sign in to do that
Don't have an account?

Conditional Apex Trigger
Hello,
I have an Apex Trigger that excutes whenever a Product is (Inserted/Edited/Deleted) from OpportunityLineItem. The trigger needs to check a picklist value from a custom picklist in Opportunity Object. The code in the trigger will execute if there is matching value, else do nothing. I have the trigger written as follows. I'm not getting any error, the trigger excutes the desired code without checking the IF Condition.
Apex Trigger:
trigger OppAmountTotal on OpportunityLineItem (after delete, after insert, after update, after undelete) {
OpportunityLineItem oli = [Select OpportunityId from OpportunityLineItem LIMIT 1];
Opportunity o = [SELECT CustomPicklist FROM Opportunity WHERE Id = :oli.OpportunityId];
if(o.CustomPicklist == 'Yes - Add to Total'){
if(trigger.isInsert || trigger.isUpdate || trigger.isUnDelete){
list<TotalAmount.fieldDefinition> fieldDefinitions =
new list<TotalAmount.fieldDefinition> {
new TotalAmount.fieldDefinition('SUM', 'TotalPrice',
'Amount')
};
TotalAmount.rollUpTrigger(fieldDefinitions, trigger.new,
'OpportunityLineItem', 'OpportunityId', 'Opportunity', '');
}
if(trigger.isDelete){
list<TotalAmount.fieldDefinition> fieldDefinitions =
new list<TotalAmount.fieldDefinition> {
new TotalAmount.fieldDefinition('SUM', 'TotalPrice',
'Amount')
};
TotalAmount.rollUpTrigger(fieldDefinitions, trigger.old,
'OpportunityLineItem', 'OpportunityId', 'Opportunity', '');
}
}
}
I have an Apex Trigger that excutes whenever a Product is (Inserted/Edited/Deleted) from OpportunityLineItem. The trigger needs to check a picklist value from a custom picklist in Opportunity Object. The code in the trigger will execute if there is matching value, else do nothing. I have the trigger written as follows. I'm not getting any error, the trigger excutes the desired code without checking the IF Condition.
Apex Trigger:
trigger OppAmountTotal on OpportunityLineItem (after delete, after insert, after update, after undelete) {
OpportunityLineItem oli = [Select OpportunityId from OpportunityLineItem LIMIT 1];
Opportunity o = [SELECT CustomPicklist FROM Opportunity WHERE Id = :oli.OpportunityId];
if(o.CustomPicklist == 'Yes - Add to Total'){
if(trigger.isInsert || trigger.isUpdate || trigger.isUnDelete){
list<TotalAmount.fieldDefinition> fieldDefinitions =
new list<TotalAmount.fieldDefinition> {
new TotalAmount.fieldDefinition('SUM', 'TotalPrice',
'Amount')
};
TotalAmount.rollUpTrigger(fieldDefinitions, trigger.new,
'OpportunityLineItem', 'OpportunityId', 'Opportunity', '');
}
if(trigger.isDelete){
list<TotalAmount.fieldDefinition> fieldDefinitions =
new list<TotalAmount.fieldDefinition> {
new TotalAmount.fieldDefinition('SUM', 'TotalPrice',
'Amount')
};
TotalAmount.rollUpTrigger(fieldDefinitions, trigger.old,
'OpportunityLineItem', 'OpportunityId', 'Opportunity', '');
}
}
}
Have you thrown a debug statement in there to see what is the value of the picklist in the logs?
Looking at the code, I think it is checking the If condition, and that you most likely have a data issue that is causing the picklist value to always be 'Yes - Add to Total'.
To get this to "work" on one item, the query has to be updated:
OpportunityLineItem oli = [Select OpportunityId from OpportunityLineItem LIMIT 1 where ID = trigger.new[0].id]; (or trigger.old for the delete).
But now there are more issues:
1 - What if you insert,update,delete more than one at a time
2 - Do you even need to query the OpportunityLineItem? You can probably use the trigger variable directly.
So yes, this code needs some attention.
And thanks James, for the hint :-)
So the reason i'm only looking at 1 OpportunityLineItem, is because i only need the OpportunityId. Isn't the OpportunityID value going to be same, no matter which line item i look in that Opportunity. The CustomPiklict__c is a custom field in Opportunity. So i need to OpportunityId to check the value in that custom field.
Since the Opportunity.CustomPicklist__c is a picklist, do i need to use ISPICKVAL() to compare values?
2. ISPICKVAL is a function used in formula expressions, VisualForce pages, etc. In Apex you can treat CustomPicklist__c as a string.
Dan