You need to sign in to do that
Don't have an account?
shwetha prabhakar
copy opportunity product to order product using trigger
Here is the code, running successfully but not copying the products. can someone help on this?
trigger orderproduct on Order__c (before insert,after insert) {
Set<Id> opportunityIds = new Set<Id>();
Set<Id> orderIds = new Set<Id>();
List<Order__c> orderList = new List<Order__c>();
for(Order__c o : Trigger.new)
{
if(o.Opportunity__c != NULL)
{
opportunityIds.add(o.Opportunity__c );
orderIds.add(o.Id);
}
}
if(opportunityIds.size() > 0)
{
list<OpportunityLineItem__c> oppprodList = new list<OpportunityLineItem__c>([Select id,name from OpportunityLineItem__c WHERE Id IN :opportunityIds]);
// Loop through orders
List<OrderItem__c> orderItemsForInsert = new List<OrderItem__c>();
for(OpportunityLineItem__c pd : oppprodList)
{
OrderItem__c oi = new OrderItem__c();
oi.OrderId__c = oi.Id;
oi.Unit_Price__c = 2323;
oi.Quantity__c = 1;
orderItemsForInsert.add(oi);
}
system.debug('sucess');
if(orderItemsForInsert.size() > 0)
{
insert orderItemsForInsert;
}
}
}
trigger orderproduct on Order__c (before insert,after insert) {
Set<Id> opportunityIds = new Set<Id>();
Set<Id> orderIds = new Set<Id>();
List<Order__c> orderList = new List<Order__c>();
for(Order__c o : Trigger.new)
{
if(o.Opportunity__c != NULL)
{
opportunityIds.add(o.Opportunity__c );
orderIds.add(o.Id);
}
}
if(opportunityIds.size() > 0)
{
list<OpportunityLineItem__c> oppprodList = new list<OpportunityLineItem__c>([Select id,name from OpportunityLineItem__c WHERE Id IN :opportunityIds]);
// Loop through orders
List<OrderItem__c> orderItemsForInsert = new List<OrderItem__c>();
for(OpportunityLineItem__c pd : oppprodList)
{
OrderItem__c oi = new OrderItem__c();
oi.OrderId__c = oi.Id;
oi.Unit_Price__c = 2323;
oi.Quantity__c = 1;
orderItemsForInsert.add(oi);
}
system.debug('sucess');
if(orderItemsForInsert.size() > 0)
{
insert orderItemsForInsert;
}
}
}
Try executing code below :-
trigger orderproduct on Order__c (before insert,after insert) {
Set<Id> opportunityIds = new Set<Id>();
Set<Id> orderIds = new Set<Id>();
List<Order__c> orderList = new List<Order__c>();
Map<Id,Id> mapOfOppIdToOdrId = new Map<Id,Id>();
for(Order__c o : Trigger.new)
{
if(o.Opportunity__c != null)
{
opportunityIds.add(o.Opportunity__c );
mapOfOppIdToOdrId .put(o.Opportunity__c ,o.Id);
orderIds.add(o.Id);
}
}
if(opportunityIds.size() > 0)
{
list<OpportunityLineItem__c> oppprodList = new list<OpportunityLineItem__c>([Select id,name from OpportunityLineItem__c WHERE Id IN :opportunityIds]);
// Loop through orders
List<OrderItem__c> orderItemsForInsert = new List<OrderItem__c>();
for(OpportunityLineItem__c pd : oppprodList)
{
OrderItem__c oi = new OrderItem__c();
oi.OrderId__c = mapOfOppIdToOdrId.get(pd.Opportunity__c );
oi.Unit_Price__c = 2323;
oi.Quantity__c = 1;
orderItemsForInsert.add(oi);
}
system.debug('sucess');
if(orderItemsForInsert.size() > 0)
{
insert orderItemsForInsert;
}
}
}
Thanks,
Rishabh Bansal