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
shobana shobana 1shobana shobana 1 

Trigger is not firring

My scenario is i want to insert a orderItem whenever the opportunitylineItem is created(only when opportunity's StageName is "closedWon").
Actually in my Triggers its not throughing any error but trigger is not firring.
Anyone give me an suggestion to solve this issue.
My code is
trigger ToUpdateProduct on OpportunityLineItem (before insert) {
Map<String,Order> od = new Map<String,Order>();
Set<Id> s = new Set<Id>();
List<OrderItem> oiList = new List<OrderItem>();
for(OpportunityLineItem olt : Trigger.New) 
 If(olt.Opportunity.StageName == 'Closed Won')
   s.add(olt.OpportunityId);

for(Order o : [select Id,OpportunityId from Order where OpportunityId IN:s])  
   od.put(String.valueOf(o.OpportunityId),o);

for(OpportunityLineItem olt : Trigger.New) 
{
  If(olt.Opportunity.StageName == 'Closed Won'){
    OrderItem oi = new OrderItem();
    oi.OrderId = od.get(String.valueOf(olt.OpportunityId)).Id;
    oi.Quantity = olt.Quantity;
    oi.PricebookEntryId = olt.PricebookEntryId;
    oi.UnitPrice = olt.UnitPrice;  
    oiList.add(oi); }
}

If(oiList.size() > 0)
 insert oiList;


}
Thanks in Advance

 
Best Answer chosen by shobana shobana 1
Mahesh DMahesh D
Hi Shobana,

Please fine the below trigger:

I followed 

(1) Considered all positive and negative scenarios
(2) Followed best practices.
(3) Streamlined the code.
(4) Followed Bulkify Trigger.

 
trigger OpportunityLineItemTrigger on OpportunityLineItem (before insert) {
    
    Map<Id,Order> oppOrdMap = new Map<Id,Order>();
    
    Set<Id> OppIds = new Set<Id>();
    List<OrderItem> oiList = new List<OrderItem>();
    
    for(OpportunityLineItem olt : Trigger.New)
        OppIds.add(olt.OpportunityId);
    
    Map<Id,Opportunity> oppMap = New Map<Id,Opportunity>([SELECT ID,StageName FROM Opportunity WHERE Id IN :OppIds AND StageName = 'Closed Won']);
    
    if(!oppMap.isEmpty()) {
		for(Order o : [select Id, OpportunityId from Order where OpportunityId IN: oppMap.keySet()]) {
			oppOrdMap.put(o.OpportunityId,o);
		}
		
		if(!oppOrdMap.isEmpty()) {		
			for(OpportunityLineItem olt : Trigger.New) 
			{
				If(oppMap.get(olt.OpportunityId) != null && oppOrdMap.get(olt.OpportunityId) != null){
					OrderItem oi = new OrderItem();
					oi.OrderId = oppOrdMap.get(olt.OpportunityId).Id;
					oi.Quantity = olt.Quantity;
					oi.PricebookEntryId = olt.PricebookEntryId;
					oi.UnitPrice = olt.UnitPrice;  
					oiList.add(oi); 
				}
			}
			
			If(!oiList.isEmpty())
				insert oiList;
		}
	}    
}

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Nagendra ChinchinadaNagendra Chinchinada
Hi,
olt.Opportunity.StageName​ doesnot holds any value, you need to query Opportunity fields separatley.​
One more suggestion is instaed of Map<String,Order> od = new Map<String,Order>(), you can take Map<Id,Order> od = new Map<Id,Order>().
I did some changes in entire code.

 
trigger TestOPPlineutm on OpportunityLineItem (before insert) {
    
    Map<Id,Order> od = new Map<Id,Order>();
    Set<Id> s = new Set<Id>();
    Set<Id> OppIds = new Set<Id>();
    List<OrderItem> oiList = new List<OrderItem>();
    
    for(OpportunityLineItem olt : Trigger.New)
        OppIds.add(olt.OpportunityId);
    
    Map<Id,Opportunity> OppMap = New Map<Id,Opportunity>([SELECT ID,StageName FROM Opportunity WHERE Id IN :OppIds]);
    
    
    for(OpportunityLineItem olt : Trigger.New) 
        If(OppMap.get(olt.OpportunityId).StageName == 'Closed Won')
        s.add(olt.OpportunityId);
    
    for(Order o : [select Id,OpportunityId from Order where OpportunityId IN:s])  
        od.put(o.OpportunityId,o);
    
    for(OpportunityLineItem olt : Trigger.New) 
    {
        If(OppMap.get(olt.OpportunityId).StageName == 'Closed Won'){
            OrderItem oi = new OrderItem();
            oi.OrderId = od.get(olt.OpportunityId).Id;
            oi.Quantity = olt.Quantity;
            oi.PricebookEntryId = olt.PricebookEntryId;
            oi.UnitPrice = olt.UnitPrice;  
            oiList.add(oi); }
    }
    
    If(oiList.size() > 0)
        insert oiList;
    
}

Hope it will help you.
shobana shobana 1shobana shobana 1
Hi Nagendra Prasad

Thank you for your reply.Its working good.

 
Mahesh DMahesh D
Hi Shobana,

Please fine the below trigger:

I followed 

(1) Considered all positive and negative scenarios
(2) Followed best practices.
(3) Streamlined the code.
(4) Followed Bulkify Trigger.

 
trigger OpportunityLineItemTrigger on OpportunityLineItem (before insert) {
    
    Map<Id,Order> oppOrdMap = new Map<Id,Order>();
    
    Set<Id> OppIds = new Set<Id>();
    List<OrderItem> oiList = new List<OrderItem>();
    
    for(OpportunityLineItem olt : Trigger.New)
        OppIds.add(olt.OpportunityId);
    
    Map<Id,Opportunity> oppMap = New Map<Id,Opportunity>([SELECT ID,StageName FROM Opportunity WHERE Id IN :OppIds AND StageName = 'Closed Won']);
    
    if(!oppMap.isEmpty()) {
		for(Order o : [select Id, OpportunityId from Order where OpportunityId IN: oppMap.keySet()]) {
			oppOrdMap.put(o.OpportunityId,o);
		}
		
		if(!oppOrdMap.isEmpty()) {		
			for(OpportunityLineItem olt : Trigger.New) 
			{
				If(oppMap.get(olt.OpportunityId) != null && oppOrdMap.get(olt.OpportunityId) != null){
					OrderItem oi = new OrderItem();
					oi.OrderId = oppOrdMap.get(olt.OpportunityId).Id;
					oi.Quantity = olt.Quantity;
					oi.PricebookEntryId = olt.PricebookEntryId;
					oi.UnitPrice = olt.UnitPrice;  
					oiList.add(oi); 
				}
			}
			
			If(!oiList.isEmpty())
				insert oiList;
		}
	}    
}

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Mahesh DMahesh D
Hi Shobana,

I just want to clarify, did you test the solution which I provided?

Regards,
Mahesh
shobana shobana 1shobana shobana 1
Hi Mahesh

Thank you for your reply.
Its working great hereafter i will follow your methods to write a trigger what you mentioned above.