You need to sign in to do that
Don't have an account?
Javier Gimeno 8
Simple Apex trigger to set Opportunity Line Item number
Hi all. I'm just startig with Apex, so I'm really noob to these matters :(
What I'm trying to achieve is to assign an unique number to every Opportunity Line Item is created in each Opportunity.
I've created two custom fields:
- One in the Opportunity object: Product_Count
- One in the Opportunity Product Objetct: Item_Number
I've created some worflows, so when an Opportunity Line Item is created:
- Item_Number = Product_Count + 1
- Product_Count = Product_Count +1
Through worflows, the thing seems to be running fine, but the problem is that when I insert more than one product at a time, they all get the same Item_Number.
Now, I'm trying to write an Apex trigger to do the same thing, but I'm really stuck. I can only set a fix value for each item (the same number to all of them), but I can't update the Opportunity field Product_Count or the Opportunity product Item_Number
This is how may trigger looks:
trigger NumeroItem on OpportunityLineItem (before insert) {
for (OpportunityLineItem OLI: Trigger.new) {
OLI.N_de_Item__c = 3;
}
}
Would you be so kind as to give me a hand with this?
I don't think it should be really hard, but as I'm just starting with Apex, I'm a bit overwhelmed :(
Thank you ver very much for your help.
Kind regards.
What I'm trying to achieve is to assign an unique number to every Opportunity Line Item is created in each Opportunity.
I've created two custom fields:
- One in the Opportunity object: Product_Count
- One in the Opportunity Product Objetct: Item_Number
I've created some worflows, so when an Opportunity Line Item is created:
- Item_Number = Product_Count + 1
- Product_Count = Product_Count +1
Through worflows, the thing seems to be running fine, but the problem is that when I insert more than one product at a time, they all get the same Item_Number.
Now, I'm trying to write an Apex trigger to do the same thing, but I'm really stuck. I can only set a fix value for each item (the same number to all of them), but I can't update the Opportunity field Product_Count or the Opportunity product Item_Number
This is how may trigger looks:
trigger NumeroItem on OpportunityLineItem (before insert) {
for (OpportunityLineItem OLI: Trigger.new) {
OLI.N_de_Item__c = 3;
}
}
Would you be so kind as to give me a hand with this?
I don't think it should be really hard, but as I'm just starting with Apex, I'm a bit overwhelmed :(
Thank you ver very much for your help.
Kind regards.
Hope it helps.
All Answers
1) To assign unique number to every Opportunity Line Item create a auto-number field on Opportunity Line Item
2) Create a "Roll-Up Summary" field on Opportunity which counts the child Opportunity Line Items
I don't think the the autonumber approach would work. The autonumber field would create a unique number for every Opportunity Line Product in every Opportunity. What I'm looking for is something like this, for instance:
Opportunity 1
-Opportunity Line Item Nº 1
-Opportunity Line Item Nº 2
-Opportunity Line Item Nº 3
Opportunity 2
-Opportunity Line Item Nº 1
-Opportunity Line Item Nº 2
-Opportunity Line Item Nº 3
Opportunity 3
-Opportunity Line Item Nº 1
-Opportunity Line Item Nº 2
-Opportunity Line Item Nº 3
-Opportunity Line Item Nº 4
Each Opportunity Line Item must have its own number within the same Opportunity, but that number can be used outside the Opportunity.
Thank you very much for your help.
Hope it helps.
I've been studying your code, doing some minor modifications and it's working fine!!
Thanks again. It wouldn't have been possible without your help.