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
Vic PrabhuVic Prabhu 

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', '');
        }
    }
}
kibitzerkibitzer
I'm going to assume that CustomPicklist refers to a custom field (such as CustomPicklist__c).
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'.
James LoghryJames Loghry
It's likely that your CustomPicklist__c value is incorrect as kibitzerr mentioned.  However, it looks like you're only grabbing the first Opportunity Line Item / Opportunity, in which case it would also likely fail if you're inserting more than one record at the same time.  See the following doc on how to better handle bulk data: https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
kibitzerkibitzer
Ah - that explains the problem!  Curiously, as writtin it is bulk-safe, because he has a limit of 1 on the OpportunityLineItem query - but that's also the source of the problem. He's always looking at the same OpportunityLineItem object - not the one that was just inserted!

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 :-)
Vic PrabhuVic Prabhu
Kibitzer and James.. thank you for your response.

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?
kibitzerkibitzer
1. It's true that in most cases when working through the UI, all of the OpportunityLineItem objects in a single DML operation would have the same opportunity ID, but as far as I know there is nothing in the system that would enforce this condition. So, if other Apex code was working with OpportunityLineItem objects, or if you were using the data loader, it would be absolutely possible for you to see multiple opportunities represented in the list.

2. ISPICKVAL is a function used in formula expressions, VisualForce pages, etc. In Apex you can treat CustomPicklist__c as a string.

Dan