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
sfdev179sfdev179 

allow addition of only one opportunity product to Opportunity

I am a beginner and I have to write an apex class that allows only one opportunity product to be added to any opportunity and then invoke it using trigger.It sounds simple,but I cant seem to develop the logic.

 

Any sample code or any help would be much appreciated.

simple-forcesimple-force

Hi sfdc179,

 

this is a possible solution for your issue. The trigger runs before insert and queries for all related opportunities and all related opportunity line items. If there is more than 0 opportunity line item of a related opportunity the trigger will add a error message.

 

// Pseudo code

trigger OpportunityLineItemTrigger on OpportunityLineItem (before insert) {

Set<Id>opportunityIds = new Set<Id>();

 

// get all parent IDs

for(OpportunityLineItem i : trigger.new)

{

opportunityIds.add(i.OpportunityId);

}

// query for related Olis (Opportunity Line Items)

Map<Id, Opportunity> opps = new Map<Id, Opportunity>([SELECT ID, (SELECT ID FROM OpportunityLineItems) FROM Opportunity WHERE ID IN :opportunityIds.add]);

for(OpportunityLineItem i : trigger.new)

{

    if(opps.get(i.OpportunityId).OpportunityLineItems.size()>0)

{

   i.addError('Your Message');

}

}
}

 

Christian

sfdev179sfdev179

I added the beforre update even to the trigger too.This code displays an error message If I add more than one opportunity product after one has already been saved.

However,if on a new opportunity I select more than 1 opportunity product at the same time,it allows them to be saved and gives no error.I want to be able to display an error even then too and allow selection of just one opportunity product per opportunity.

Please tel me how to resolve this issue.

 

 

Thanks for your help.

simple-forcesimple-force

In addition you can count opportunity Ids of opportunity line items from trigger new

 

like

 

// Pseudo code

trigger OpportunityLineItemTrigger on OpportunityLineItem (before insert) {

Set<Id>opportunityIds = new Set<Id>();

 

// get all parent IDs

for(OpportunityLineItem i : trigger.new)

{

opportunityIds.add(i.OpportunityId);

}

// query for related Olis (Opportunity Line Items)

Map<Id, Opportunity> opps = new Map<Id, Opportunity>([SELECT ID, (SELECT ID FROM OpportunityLineItems) FROM Opportunity WHERE ID IN :opportunityIds.add]);

 

 

>>>>>>>> New code

// opp counter of new records

Map<Id, Integer>oppCounter = new Map<Id, Integer>();

for(OpportunityLineItem i : trigger.new)

{

if(oppCounter.containsKey(i.OpportunityId)

{

oppCounter.put(i.OpportunityId, oppCounter.put.get(i.OpportunityId)+1);

}

else

{

oppCounter.put(i.OpportunityId, 1);

}

}

>>>>>>>>>>

 

for(OpportunityLineItem i : trigger.new)

{

    if(opps.get(i.OpportunityId).OpportunityLineItems.size()+oppCounter.get(i.OpportunityId)>1)

{

   i.addError('Your Message');

}

}
}