0

I am writing trigger "On Insert Of Order(Sales order) copy opportunityLineitems to order line items" I have done everything fine upto my knowledge but when I am inserting order it is showing pricebook not set error.

Thanks In Advance

Trigger Code:-

trigger orderLineCreationSherry on order(after insert)
{
    set<string> strSet = new set<string>();
    list<order> orList = new list<order>();
    list<OrderItem> oliPist=new list<OrderItem>();
    list<opportunity> oppList=new list<opportunity>();

    for(order o:trigger.new){
        strSet.add(o.OpportunityId);
    }
    system.debug('strSet========'+strSet);

    map<string,opportunity> mapOpp = new map<string,opportunity>([SELECT id,Name, (SELECT id,name,Product2Id,Quantity FROM OpportunityLineItems) FROM Opportunity WHERE id in:strSet]);
    system.debug('oppList========='+oppList);

    for(order o:trigger.new)
    {
        List<Order> oderLst = new List<Order>();
        Opportunity opp = mapOpp.get(o.OpportunityId);
        if(opp.OpportunityLineItems.size()!=0){
            for(OpportunityLineItem oli : opp.OpportunityLineItems)
            {   
                PriceBook2 pb2Standard =[select Id from Pricebook2 where isStandard=true];
                Id standardPriceBookId = pb2Standard.Id;
                system.debug('standardPriceBookId===========>>>>>>>>>>>>'+standardPriceBookId);
                Id currentProductId=oli.Product2Id;
                system.debug('currentProductId===========>>>>>>'+currentProductId);
                PricebookEntry pbe = [SELECT Id,Product2Id,Pricebook2Id,UnitPrice FROM PricebookEntry WHERE Pricebook2Id =:standardPriceBookId AND Product2Id =:currentProductId AND isActive = true LIMIT 1];
                system.debug('Here Is Pricebook2 ID'+pbe.id);
                system.debug('INsideloop');
                OrderItem od = new OrderItem();
                od.OrderId=o.Id;
                od.Quantity=oli.Quantity;
                od.PricebookEntryId=pbe.Id;
                od.Product2Id=oli.Product2Id;
                oliPist.add(od);
            }
        system.debug('Here Is Field Data=========>>>>>>'+oliPist);
      }  
        system.debug('Its Reaching Here');
    }
    if(oliPist.size()!=0){
        system.debug('It Will Be Debuged Now INsert Zone');
        insert oliPist;
    }
}
1
  • This Error I am Facing Error: Invalid Data. Review all error messages below to correct your data. Apex trigger orderLineCreationSherry caused an unexpected exception, contact your administrator: orderLineCreationSherry: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Not Set on Sales Order: []: Trigger.orderLineCreationSherry: line 44, column 1
    – Guru
    Dec 20 '17 at 12:46
1

you need two triggers one before insert and one after insert on order.

trigger OrderTriggerBefore on Order (before insert) {

set<Id> OpportunityId = new set<Id>();
for(Order ord :trigger.new) {
    if(ord.OpportunityId != null) {
        OpportunityId.add(ord.OpportunityId);
    }
}
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([select Id,pricebook2Id from Opportunity where Id In :OpportunityId]);
for(Order ord :trigger.new) {
    ord.Pricebook2Id = oppMap.get(ord.OpportunityId).pricebook2Id;
}}`

after this do create this

trigger OrderTrigger on Order (After insert) {
set<Id> oppId = new Set<Id>();
set<Id> pcrId = new set<Id>(); 
List<OrderItem> oliPist = new  List<OrderItem>();
for(Order ord : trigger.new) {
    if(ord.opportunityId != null) {
        oppId.add(ord.opportunityId);
    }
}

Map<Id,List<OpportunityLineItem>> olitoOpp = new  Map<Id,List<OpportunityLineItem>>();

for(OpportunityLineItem oli : [select Id,Quantity,UnitPrice,PricebookEntryId,Product2Id,OpportunityId from OpportunityLineItem where opportunityId In : oppId ]) {
    pcrId.add(oli.Product2Id);
    if(olitoOpp.containsKey(oli.opportunityId)) {
        olitoOpp.get(oli.opportunityId).add(oli);
    }
    else {
        olitoOpp.put(oli.opportunityId, new List<OpportunityLineItem>{oli});
    }
}
For(Order ord : trigger.new) {
    if(olitoOpp.containsKey(ord.opportunityId)) {
        for(OpportunityLineItem  oli : olitoOpp.get(ord.opportunityId)){

            OrderItem od = new OrderItem();
            od.OrderId=ord.Id;
            od.Quantity=oli.Quantity;
            od.PricebookEntryId=oli.PricebookEntryId;
            od.UnitPrice = oli.UnitPrice;
            od.Product2Id=oli.Product2Id;
            oliPist.add(od);
        }
    }

}

if(!oliPist.isEmpty()) {
    insert oliPist;
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.