You need to sign in to do that
Don't have an account?
OpportunityLineItem trigger questions
I've written a trigger on opportunitylineitem and if I have added three line items, for example, and I want to display an error message on just one line, I can't seem to do that. For example,
trigger triggerCheck on OpportunityLineItem (before insert, before update) {
trigger.new[0]].addError('Error');
}
Displays 'Error' on all three line items.
If I do this:
trigger triggerCheck on OpportunityLineItem (before insert, before update) {
for (integer index=0;index<trigger.new.size();index++){
trigger.new[index].adderror('Index = ' + index);
}
}
It displays unique error message on each line, but if I modify it to add a break; (e.g. trigger.new[index].adderror('Index = ' + index); break;), it displays 'Index = 0' on all three line items.
Is there a way to only display an error on, for example, just the 2nd line? Or just the 3rd line?
Thanks, Dave
As I mentioned above you have to give some condition based on which you want to display the error otherwise the error will come in all the lineitems. Please give for which condition, you want to display the error and see what happens.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
All Answers
On which basis do u want to display the error?. Means for which reason. Then give condition based on that particular reason so that your error will display on that particular lineitem where the condition satisfies.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Lets take the example Lil bit further.
Lets say you want to display error for lineitem where quantity is 3
So your code should be.
Now say you want to show error when user is going to add 3rd Line item :
Please Note : The above trigger is not bulk safe you may want to bulkify the same
Thanks, I probably wasn't clear in my explanation. I actually am adding 3 OLI's simultaneously and I determine that two of the items are incompatible and can't be put onto the same opportunity ((this can't be done in a validation because the combinations and permutation are vast). When I use your sample (just as in mine), my output looks like:
Product Qty Price Errors
Product 1 1 50.00 You cannot add more than 2 Line Items
Product 2 1 75.00 You cannot add more than 2 Line Items
Product 3 1 100.00 You cannot add more than 2 Line Items
So I have the same error displayed on all OLI's and I only want to display it on the one line that isn't valid.
I hope that makes more sense.
Well that wont change the code much.
That's basically what my initial sample code does and it doesn't work. However, I have gained some insight on it. If I use this code:
trigger triggerCheck on OpportunityLineItem (before insert, before update) {
trigger.new[trigger.new.size()-1].adderror('There are' + trigger.new.size() +' triggers');
}
Then the three lines ahve error messages that read:
There are 1 triggers
There are 2 triggers
There are 3 triggers
So, the trigger enters for each line item. I think I can use that info to get where I want to go ... I hope.
Thanks for the help!
David there is difference in your code what I gave you. I gave you two examples on how to check condtion/validation based on values. Your code just addErrors to every record. It doesnt check anything. It will blindly add error whether the lineItem follows the validation or not
you have to do a if() check before adding error,
As I mentioned above you have to give some condition based on which you want to display the error otherwise the error will come in all the lineitems. Please give for which condition, you want to display the error and see what happens.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Thanks. I appreciate all teh assistance. Your comments definitely lead me down the right path. I did actually have the 'if' correct in my real code, but I was mistakenly adding the error to the wrong OLI.
Thanks, Dave