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
Javier Gimeno 8Javier 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.
Best Answer chosen by Javier Gimeno 8
Lokesh__PareekLokesh__Pareek
Ok. You will get good idea of how to handle the problem with below mentioned code and as you have mentioned that you just started with apex you must handle random deletion of opportunity line item case as below code do not handle this case and just for your reference. 

Hope it helps.
trigger UniqueNumberOLI on OpportunityLineItem (before insert) {
    Set<Id> allOppIds = new Set<Id>();
    //get all the Opportunity records for OpportunityLineItems oli are added
    for(OpportunityLineItem oli : Trigger.new) {
        allOppIds.add(oli.OpportunityId);
    }
    
    
    //query Opportunity to get roll up summary, here Total_Opp_Line_Items__c is Roll up summary field  
    Map<Id,Opportunity> oppRecords = new Map<Id,Opportunity>([Select Total_Opp_Line_Items__c From Opportunity Where Id IN:allOppIds]); 
    
    //map contains Id of opportunity to latest child no
    Map<Id,Integer> latestChild = new Map<Id,Integer>();
    
   
        for(OpportunityLineItem oli : Trigger.new) {         
            if(latestChild.containsKey(oli.Id)) {
                oli.Item_Number__c = latestChild.get(oli.OpportunityId);
            }    
            else {
                oli.Item_Number__c = oppRecords.get(oli.OpportunityId).Total_Opp_Line_Items__c +1;
                latestChild.put(oli.OpportunityId, (Integer)oli.Item_Number__c);
            }
                
        }   
}

 

All Answers

Lokesh__PareekLokesh__Pareek
You don't need to write code for this,

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

 
Javier Gimeno 8Javier Gimeno 8
Hi.

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.
 
Lokesh__PareekLokesh__Pareek
Ok. You will get good idea of how to handle the problem with below mentioned code and as you have mentioned that you just started with apex you must handle random deletion of opportunity line item case as below code do not handle this case and just for your reference. 

Hope it helps.
trigger UniqueNumberOLI on OpportunityLineItem (before insert) {
    Set<Id> allOppIds = new Set<Id>();
    //get all the Opportunity records for OpportunityLineItems oli are added
    for(OpportunityLineItem oli : Trigger.new) {
        allOppIds.add(oli.OpportunityId);
    }
    
    
    //query Opportunity to get roll up summary, here Total_Opp_Line_Items__c is Roll up summary field  
    Map<Id,Opportunity> oppRecords = new Map<Id,Opportunity>([Select Total_Opp_Line_Items__c From Opportunity Where Id IN:allOppIds]); 
    
    //map contains Id of opportunity to latest child no
    Map<Id,Integer> latestChild = new Map<Id,Integer>();
    
   
        for(OpportunityLineItem oli : Trigger.new) {         
            if(latestChild.containsKey(oli.Id)) {
                oli.Item_Number__c = latestChild.get(oli.OpportunityId);
            }    
            else {
                oli.Item_Number__c = oppRecords.get(oli.OpportunityId).Total_Opp_Line_Items__c +1;
                latestChild.put(oli.OpportunityId, (Integer)oli.Item_Number__c);
            }
                
        }   
}

 
This was selected as the best answer
Javier Gimeno 8Javier Gimeno 8
Thank you very much!!

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.