You need to sign in to do that
Don't have an account?
JN22
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,
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; } } } } }
All Answers
{
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
If this helps,please mark it as best answer to help others :)
Is the Unique_Number__c field you have meant to be the custom field on my OpportunityLineItem object?
I tried your code but I get an error:
Compile Error: Variable does not exist: i at line 15 column 202
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
Compile Error: Loop must iterate over a collection type: SOBJECT:OpportunityLineItem at line 8 column 49
Do you mean the line should look like this:
trigger.new[j].Max_Deliv__c = Opportunity.Max_Deliv_Hx__c;
Compile Error: Illegal assignment from Schema.SObjectField to Decimal at line 11 column 33