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

Stuck on simple (?) trigger update on Opportunity/OpportunityLineItem
Hi All,
What I'm trying to do is set a boolean field on the Opportunity called Has_Kiosk_Hardware__c to True if a product (which is of type "Kiosk Hardware" according to my custom Product_Group__c picklist on the Product2 object) is added to that opportunity.
Here's what I came up with:
It compiles and I get no errors when I add products, but it does not update the Has_Kiosk_Hardware__c when I add a kiosk hardware product. To troubleshoot, I changed the If statement to always be true like so:
And I got this error sent via email:
updateHasKioskHardware: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.updateHasKioskHardware: line 6, column 8
What am I doing wrong?
ps Sorry for the double-post, but I after I initially replied in that thread I got concerned that it would be buried there and not get noticed, so I created a new post with the question.
What I'm trying to do is set a boolean field on the Opportunity called Has_Kiosk_Hardware__c to True if a product (which is of type "Kiosk Hardware" according to my custom Product_Group__c picklist on the Product2 object) is added to that opportunity.
Here's what I came up with:
Code:
trigger updateHasKioskHardware on OpportunityLineItem (after insert) {
For (OpportunityLineItem nOLI:Trigger.new)
//
{If (nOLI.pricebookentry.product2.product_group__c == 'Kiosk Hardware')
{
nOLI.Opportunity.Has_Kiosk_Hardware__c = True;
}
}
}
It compiles and I get no errors when I add products, but it does not update the Has_Kiosk_Hardware__c when I add a kiosk hardware product. To troubleshoot, I changed the If statement to always be true like so:
Code:
trigger updateHasKioskHardware on OpportunityLineItem (after insert) {
For (OpportunityLineItem nOLI:Trigger.new)
//
{If (True)
{
nOLI.Opportunity.Has_Kiosk_Hardware__c = True;
}
}
}
And I got this error sent via email:
updateHasKioskHardware: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.updateHasKioskHardware: line 6, column 8
ps Sorry for the double-post, but I after I initially replied in that thread I got concerned that it would be buried there and not get noticed, so I created a new post with the question.
Message Edited by TehNrd on 07-17-2008 04:58 PM
Wow - I just learned a TON from that sample code - thank you!!
You were right, though, the debug statement you put in there returned a null:
Beginning updateHasKioskHardware on OpportunityLineItem trigger event AfterInsert for 00kR0000001Vcqp 20080718033418.989:Trigger.updateHasKioskHardware: line 5, column 14: SelectLoop:LIST:SOBJECT:OpportunityLineItem 20080718033418.989:Trigger.updateHasKioskHardware: line 7, column 14: group null 20080718033418.989:Trigger.updateHasKioskHardware: line 14, column 14: SelectLoop:LIST:SOBJECT:Opportunity 20080718033418.989:Trigger.updateHasKioskHardware: line 14, column 36: SOQL query with 0 rows finished in 4 ms
Any suggestions for how I can get around this?
ZOMG - no wonder they call you the Super Contributor! You are a genius! Thank you so much - I'm doing a little happy dance at my desk right now!
You totally rock, and I really, really appreciate your help!
In case anyone else wants to follow along, I had to make a few super-tiny edits to your code (adding semicolons and changing a few "oli"s to "nOLI"s):
My bad on the syntax as I just typed it up in notepad.
As proof that no good deed goes unpunished, I have another question...
The trigger is working perfectly, but I actually have 3 checkboxes on the opportunity: Has_Kiosk_Hardware__c, Has_Kiosk_Software__c, and Has_Web_Software__c, and I really need the appropriate box checked when a product is added. I got as far as updating the IF statement to check for all three product groups, but I'm not sure where to go next. If you're sick of me I totally understand, but I'm sheepishly asking anyway!
Here's what I currently have:
Perhaps another happy dance is in order?
http://www.youtube.com/watch?v=GD_5Rtc1gk8
Message Edited by TehNrd on 07-18-2008 10:20 AM
I haven't registered yet but I do plan on attending Dreamforce this year. PM me closer to the event and maybe we can find time to meet up.
Tests can be tricky so report back if you hit any issues.
-Jason
Message Edited by TehNrd on 07-18-2008 11:00 AM
Right on Jason-
Thanks again, and I'll definitely PM as the event gets closer!
I got the unit test written! Wahoo! It's deployed and works beautifully.
Ok, I really hate to ask this, and I know I'm really looking a gift horse in the mouth here, BUT...
What are your thoughts on making it so that, if a product gets deleted, the checkbox gets unchecked? Please don't feel like you have to write complete code for me - you've already done soooo much - but I'm just wondering if you think I could somehow enhance this existing trigger or if I'd need to create another 'after delete' trigger.
Have I mentioned that you rock?
In terms of the logic I can reply later im more detail as it's getting near the end of the day and I'm getting cramped for time. Basically, after the delete you need to re-evaluate all of the lines (SOQL query on all OLI where oppID is of the olis delelted) and then re update the check box on the opp if necessary.
-Jason
Awesome, thanks again!
Crap on a stick - I started writing this and realized that it's not as simple as I was thinking, because if 2 products in the same group are on the opportunity then if one is deleted I don't want to uncheck the box. I think I'm going to need to do some sort of grouping/counting query or something, aren't I?
http://www.youtube.com/watch?v=8pwfO8G5uIo
I think this should work. I haven't tested it but it should be good or at least pretty close. May need to clean up the variable names/syntax as I did it in notepad.
Message Edited by TehNrd on 07-20-2008 11:13 PM