• Javier Gimeno 8
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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.
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.