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

trigger to insert split values as record
Hi Experts,
I need assistance in completing the below trigger.
I have a custom object where it has lookup to Order object. I have a custom fields ProductName, Quantity.
Product Name, Quantity will be with more values sometimes single value which is separated by commas.
Ex: ProductName: SampleProduct,SampleProduct1
Quantity: 5,2
I want to insert multiple Order Item with respect to that Order like ProductName: SampleProduct, Quantity: 5 as 1 record and another record as ProductName: SampleProduct1, Quantity: 2 where ProductName should be matched from Pricebookentry and insert in orderitem.
Thanks in Advance.
I need assistance in completing the below trigger.
I have a custom object where it has lookup to Order object. I have a custom fields ProductName, Quantity.
Product Name, Quantity will be with more values sometimes single value which is separated by commas.
Ex: ProductName: SampleProduct,SampleProduct1
Quantity: 5,2
I want to insert multiple Order Item with respect to that Order like ProductName: SampleProduct, Quantity: 5 as 1 record and another record as ProductName: SampleProduct1, Quantity: 2 where ProductName should be matched from Pricebookentry and insert in orderitem.
trigger insertOrderItem on OrderItem__c (after insert) { // Custom Order Item Collections Set<Id> orderitemIds = new Set<Id>(); List<OrderItem__c> oiList = new List<OrderItem__c>(); List<OrderItem__c> deloiList = new List<OrderItem__c>(); // order collections List<order> ordList = new List<Order>(); Map<Id, Order> ordMap = new Map<Id, Order>(); // pricebookentry collections Map<String, List<pricebookentry>> pricebookMap = new Map<String, List<pricebookentry>>(); List<Pricebookentry> pbeList = new List<Pricebookentry>(); // orderitem collections List<OrderItem> oitList = new List<OrderItem>(); for(OrderItem__c oi : Trigger.New) { orderitemIds.add(oi.Order__c); System.debug('--- Order Id in Custom Oi Obj ---' + oi.Order__c); } if(!orderitemIds.isEmpty()) { pbeList = [SELECT Id, Name, Pricebook2Id, Pricebook2.Name, Product2Id, IsActive FROM Pricebookentry WHERE IsActive = true AND Pricebook2.Name = 'Standard Price Book' AND Pricebook2.Name IN: orderitemIds]; for(Order ord : [SELECT Id FROM Order WHERE Id IN: orderitemIds]) { ordMap.put(ord.Id, ord); } for(Pricebookentry pbe : pbeList) { if(!pricebookMap.containsKey(pbe.Product2.Name)) { pricebookMap.put(pbe.Product2.Name, new List<Pricebookentry> {pbe}); } else { List<Pricebookentry> pbeLists = pricebookMap.get(pbe.Product2.Name); pbeLists.add(pbe); pricebookMap.put(pbe.Product2.Name, pbeLists); } } } for(OrderItem__c oi : Trigger.New) { /* Need to insert Order Item*/ List<String> Name = oi.ProductName__c.split(','); List<String> Qty = oi.Quantity__c.split(','); List<String> UnitPrice = oi.UnitPrice__c.split(','); System.debug('--- Name ---' + Name); System.debug('--- Qty ---' + Qty); System.debug('--- UnitPrice ---' + UnitPrice); Map<Id, Product2> proIdMap = new Map<Id, Product2>([SELECT Id FROM Product2 WHERE Name IN: Name]); OrderItem oit = new OrderItem(); oit.OrderId = oi.Order__c; oit.Quantity = // Split Quantity Value. oit.UnitPrice = // Split UnitPrice Value. oit.PricebookEntryId = // Split ProductName Value; oitList.add(oit); } if(!oitList.isEmpty()) { insert oitList; System.debug('---Order Item Inserted Size---' + oitList.size()); } }
Thanks in Advance.
Are you getting any errors as such while implementing this and also I would suggest you to check through that the soql is not in the loop as it may hit governer limit.
Regards,
Anutej