You need to sign in to do that
Don't have an account?

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
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
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.
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
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.
Hope it will help you.
Thank you for your reply.Its working good.
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.
Please do let me know if it helps you.
Regards,
Mahesh
I just want to clarify, did you test the solution which I provided?
Regards,
Mahesh
Thank you for your reply.
Its working great hereafter i will follow your methods to write a trigger what you mentioned above.