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
JN22JN22 

Trigger Question

Hello,

I have the trigger below set to fire when OpportunityLineItem is updated or inserted.  It's meant to assign a unique number to a custom field called Max_Deliv__c on the OpportunityLineItem object.  The trigger works fine when I add single products, however, when I add multiple products at the same time it assigns the same number to each.  Does anyone know how I can change the trigger to assign a different sequential number to each product added?  Auto number fields will not work because I need the numbering sequence to start over on each new Opportunity.  Thanks,

//Assigns a unique ID to each deliverable in every Opportunity so DyNad can track.
trigger UniqueDelivID on OpportunityLineItem (before insert, before update) {   

if(checkRecursiveBI.runOnceBI()||checkRecursiveBU.runOnceBU())
{
    Set<ID> setOliIds = new Set<ID>();
        for(OpportunityLineItem oli:Trigger.new){
            setOliIds.add(oli.Id);
        }
        Map<ID, OpportunityLineItem> mapOli = new Map<ID, OpportunityLineItem>([SELECT Id, Opportunity.Max_Deliv_Hx__c, Max_Deliv__c
                                                                                FROM OpportunityLineItem
                                                                                WHERE Id in:setOliIds]);
        if(mapOli.size()>0){

/*        List<OpportunityLineItem> oli1 = [SELECT Id, Max_Deliv__c
                                         FROM OpportunityLineItem
                                         WHERE Id IN: Trigger.newMap.keySet()]; 
                                     
            for (OpportunityLineItem oli :oli1){*/
            for(OpportunityLineItem oli1:Trigger.New){
                IF(mapOli.containsKey(oli1.Id) && (oli1.Max_Deliv__c == 0 || oli1.Max_Deliv__c == null)){
                    oli1.Max_Deliv__c = mapOli.get(oli1.Id).Opportunity.Max_Deliv_Hx__c + 1;
                }
            }
        }
}

}


Best Answer chosen by JN22
JN22JN22
For anyone interested, I got my trigger working with the code below:

//Assigns a unique ID to each deliverable in every Opportunity so DyNad can track.
trigger UniqueDelivID on OpportunityLineItem (before insert, before update) { 

if(checkRecursiveBI.runOnceBI()||checkRecursiveBU.runOnceBU())
{
Set<ID> setOppIds = new Set<ID>();
for(OpportunityLineItem oli:Trigger.new){
if(oli.Opportunityid != null)
{
setOppIds.add(oli.Opportunityid);
}
}

List<Opportunity> ListOli = [SELECT Id, Max_Deliv_Hx__c
FROM Opportunity
WHERE Id in : setOppIds ];
integer i = 0;
if(ListOli.size() > 0 )
{
    For (OpportunityLineItem oli : trigger.new)
    {
        for (Opportunity opp : ListOli )
        {
            if (oli.Opportunityid == opp.id && oli.Max_Deliv__c == 0)
            {
                if(i == 0 )
                {
                    oli.Max_Deliv__c = opp.Max_Deliv_Hx__c;
                    i++;
                }
                
                else
                {
                    oli.Max_Deliv__c = opp.Max_Deliv_Hx__c + i;
                    i++;
                }
            }
        }
    }
}
}
}


All Answers

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
trigger UniqueDelivID on OpportunityLineItem (before insert, before update)
{  
if(checkRecursiveBI.runOnceBI()||checkRecursiveBU.runOnceBU())
{
Set<ID> setOliIds = new Set<ID>();
For(Integer i=0;i<trigger.new.size();i++)
{
for(OpportunityLineItem oli:Trigger.new[i])
{
setOliIds.add(oli.Id);
}
}
For(Integer j=0;j<setOliIds.size();j++)
{
Map<ID, OpportunityLineItem> mapOli = new Map<ID, OpportunityLineItem>([SELECT Id, Opportunity.Max_Deliv_Hx__c, Max_Deliv__c FROM OpportunityLineItem                      WHERE Id in:setOliIds[i]]);
}
if(mapOli.size()>0)
{
For(Integer b=o;b<trigger.new.size();b++)
{
for(OpportunityLineItem oli1:Trigger.New[i])
{
IF(mapOli.containsKey(oli1.Id) && (oli1.Max_Deliv__c == 0 || oli1.Max_Deliv__c == null)
{
oli1.Max_Deliv__c = mapOli.get(oli1.Id).Opportunity.Max_Deliv_Hx__c + 1;
}
}
}
}
}
Try this code
Vinit_KumarVinit_Kumar
What you are trying to possible is by following below code :-

trigger addUniqueNumber on OpportunityLineItem (before insert,before update) 
{
    
    for(integer j=0;j<trigger.new.size();j++)
    {
            if(j==0)
            {
                 trigger.new[j].Unique_Number__c = 1; // You can replace here your opportunity field number by querying the opportunity field
            }
            else
            {
                trigger.new[j].Unique_Number__c = j+1;
            }  
    }
}

If this helps,please mark it as best answer to help others :)
JN22JN22
Hi Vinit,

Is the Unique_Number__c field you have meant to be the custom field on my OpportunityLineItem object?
JN22JN22
Hi Vidhyasagaran,

I tried your code but I get an error:

Compile Error: Variable does not exist: i at line 15 column 202


Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
in the same code change it
Map<ID, OpportunityLineItem> mapOli = new Map<ID, OpportunityLineItem>([SELECT Id, Opportunity.Max_Deliv_Hx__c, Max_Deliv__c FROM OpportunityLineItem                      WHERE Id in:setOliIds[j]]);
instead of i in soql give j
JN22JN22
Thanks.  But now I get the same error for line 22.
Vinit_KumarVinit_Kumar
Yes,Unique_Number__c field is the field for storing the inuque number on OpportunityLineItem object
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
for(OpportunityLineItem oli1:Trigger.New[b]) change this and try in the line 22
JN22JN22
Thanks.  But now I get the error:

Compile Error: Loop must iterate over a collection type: SOBJECT:OpportunityLineItem at line 8 column 49


JN22JN22
Vinit,

Do you mean the line should look like this:

trigger.new[j].Max_Deliv__c = Opportunity.Max_Deliv_Hx__c;
Vinit_KumarVinit_Kumar
Do something like below ,

for(integer j=0;j<trigger.new.size();j++)
    {
            if(j==0)
            {
                 trigger.new[j].Max_Deliv__c  = Opportunity.Max_Deliv_Hx__c; // You need to query your field value here,then you can use it
            }
            else
            {
                trigger.new[j].Max_Deliv__c  = Opportunity.Max_Deliv_Hx__c + j;
            }  
    }




JN22JN22
Thanks.  But now I get:

Compile Error: Illegal assignment from Schema.SObjectField to Decimal at line 11 column 33
Vinit_KumarVinit_Kumar
I think,you should be fixing these issues..
JN22JN22
I would love nothing better, but I'm not a developer so I don't know what the error means.  Thanks for your help anyway.
JN22JN22
For anyone interested, I got my trigger working with the code below:

//Assigns a unique ID to each deliverable in every Opportunity so DyNad can track.
trigger UniqueDelivID on OpportunityLineItem (before insert, before update) { 

if(checkRecursiveBI.runOnceBI()||checkRecursiveBU.runOnceBU())
{
Set<ID> setOppIds = new Set<ID>();
for(OpportunityLineItem oli:Trigger.new){
if(oli.Opportunityid != null)
{
setOppIds.add(oli.Opportunityid);
}
}

List<Opportunity> ListOli = [SELECT Id, Max_Deliv_Hx__c
FROM Opportunity
WHERE Id in : setOppIds ];
integer i = 0;
if(ListOli.size() > 0 )
{
    For (OpportunityLineItem oli : trigger.new)
    {
        for (Opportunity opp : ListOli )
        {
            if (oli.Opportunityid == opp.id && oli.Max_Deliv__c == 0)
            {
                if(i == 0 )
                {
                    oli.Max_Deliv__c = opp.Max_Deliv_Hx__c;
                    i++;
                }
                
                else
                {
                    oli.Max_Deliv__c = opp.Max_Deliv_Hx__c + i;
                    i++;
                }
            }
        }
    }
}
}
}


This was selected as the best answer