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
sandeep kumar 44sandeep kumar 44 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry currency code does not match opportunity currency code): [PricebookEntryId]:

URGENT : I am getting an error in apex class i am trying to insert oppurtunity and line items .

ERROR : System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry currency code does not match opportunity currency code): [PricebookEntryId]: 

Apex Class : public class UpdateOpptyLineItems{
    public static void UpdateLineItems(List<OpportunityLineItem> oliList,Double ExportQuantity,string Operation){
        List<OpportunityLineItem> oliListToBeUpdated = new List<OpportunityLineItem>();        
        Date PreviousDate = System.Today();
        Date CurrentDate = System.Today();
        Map<Date,List<OpportunityLineItem>> dateFinalOli = new Map<Date,List<OpportunityLineItem>>();
        for(OpportunityLineItem oli:oliList){
            if(dateFinalOli.containsKey(Date.ValueOf(oli.ServiceDate))){
                dateFinalOli.get(Date.ValueOf(oli.ServiceDate)).add(oli);
            }
            else{
                List<OpportunityLineItem> oliList1 = new List<OpportunityLineItem>();
                oliList1.add(oli);
                dateFinalOli.put(Date.ValueOf(oli.ServiceDate),oliList1);
            }
        }
        system.debug('dateFinalOli : ' + dateFinalOli);        
        for(Date dt:dateFinalOli.KeySet()){
            Integer i=0;
            for(OpportunityLineItem oli:dateFinalOli.get(dt)){                
                if(oli.Tipo__c == '1.Estoque Inicial'){                     
                    if(i != 0){
                        if(Operation == 'Remove'){
                            oliListToBeUpdated.add(new OpportunityLineItem(Id = oli.Id,Quantity = oli.Quantity - ExportQuantity, TotalPrice = oli.TotalPrice));       
                        }
                        if(Operation == 'Add'){
                            oliListToBeUpdated.add(new OpportunityLineItem(Id = oli.Id,Quantity = oli.Quantity + ExportQuantity, TotalPrice = oli.TotalPrice));       
                        }
                    }
                }                        
                if(oli.Tipo__c == '6.Estoque Final'){    
                    if(Operation == 'Remove'){
                        oliListToBeUpdated.add(new OpportunityLineItem(Id = oli.Id,Quantity = oli.Quantity - ExportQuantity,TotalPrice = oli.TotalPrice ));
                    }
                    if(Operation == 'Add'){
                        oliListToBeUpdated.add(new OpportunityLineItem(Id = oli.Id,Quantity = oli.Quantity + ExportQuantity,TotalPrice = oli.TotalPrice));
                    }
                }       
                i++;    
            }
        }
        system.debug('Final OLI List : ' + oliListToBeUpdated);
        if(!oliListToBeUpdated.isEmpty()){
            update oliListToBeUpdated;
        }
    }
    public static void UpsertLineItems(List<Opportunity> oppImportFinalRec,Integer StartingMonth,Id pbID,Double ImportQuantity, Decimal UnitPrice){
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        List<OpportunityLineItem> oliListInsert = new List<OpportunityLineItem>();
        Map<Date,List<OpportunityLineItem>> dateFinalOli = new Map<Date,List<OpportunityLineItem>>();
        for(OpportunityLineItem oli:oppImportFinalRec[0].OpportunityLineItems){
            if(dateFinalOli.containsKey(Date.ValueOf(oli.ServiceDate))){
                dateFinalOli.get(Date.ValueOf(oli.ServiceDate)).add(oli);
            }
            else{
                List<OpportunityLineItem> oliList1 = new List<OpportunityLineItem>();
                oliList1.add(oli);
                dateFinalOli.put(Date.ValueOf(oli.ServiceDate),oliList1);
            }
        }       
        system.debug('dateFinalOli : ' + dateFinalOli); 
        for(Integer I=StartingMonth;i<=12;i++){
            Date ServiceDate = Date.newInstance(oppImportFinalRec[0].CloseDate.Year(), I, 1);
            System.debug('ServiceDate : ' + ServiceDate);
            if(dateFinalOli.containsKey(ServiceDate)){
                for(OpportunityLineItem oli:dateFinalOli.get(ServiceDate)){
                    if(oli.Tipo__c == '1.Estoque Inicial'){
                        if(I!= StartingMonth){
                            oliList.add(new OpportunityLineItem(Id = oli.Id,Quantity = oli.Quantity + ImportQuantity));
                        }
                    }
                    if(oli.Tipo__c == '6.Estoque Final'){
                        oliList.add(new OpportunityLineItem(Id = oli.Id,Quantity = oli.Quantity + ImportQuantity));
                    }
                }
            }
            else{           
                if(I!= StartingMonth){
                    oliListInsert.add(new OpportunityLineItem(ServiceDate = ServiceDate,PriceBookEntryId = pbID,OpportunityId = oppImportFinalRec[0].Id,Tipo__c = '1.Estoque Inicial',Quantity = ImportQuantity,UnitPrice = UnitPrice));
                }           
                oliListInsert.add(new OpportunityLineItem(ServiceDate = ServiceDate,PriceBookEntryId = pbID,OpportunityId = oppImportFinalRec[0].Id,Tipo__c = '6.Estoque Final',Quantity = ImportQuantity,UnitPrice = UnitPrice));
            }
        }
        system.debug(' Update List : ' + oliList);
        system.debug(' Insert List : ' + oliListInsert);
        if(!oliList.isEmpty()){
            update oliList;
        }
        if(!oliListInsert.isEmpty()){
            insert oliListInsert;
        }
    }
    public static void InsertLineItems(Integer StartingMonth,string OppID, Id pbID,Double ImportQuantity, Decimal UnitPrice){
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        for(Integer I=StartingMonth;i<=12;i++){
            if(I!= StartingMonth){
                oliList.add(new OpportunityLineItem(PriceBookEntryId = pbID,OpportunityId = OppID,Tipo__c = '1.Estoque Inicial',Quantity = ImportQuantity,UnitPrice = UnitPrice));
            }
            oliList.add(new OpportunityLineItem(PriceBookEntryId = pbID,OpportunityId = OppID,Tipo__c = '6.Estoque Final',Quantity = ImportQuantity,UnitPrice = UnitPrice));
        }
        if(!oliList.isEmpty()){
           insert oliList;
       }   
    
    }
    public static Id InsertOpportunity(Id pbID,string AccountName,string AccountID,string RegionID,string RegionName,string MainYear,string ImportRegionOwner){
        ID recTypeOppPadrao = RecordTypeMemory.getRecType( 'Opportunity', 'Padrao' );
        Periodo_da_Oportunidade__c po = Periodo_da_Oportunidade__c.getInstance();
        system.debug(po.Periodo_Aberto_Ate__c);
        Database.saveresult insOpp;
        String oppName = '';
        oppName += AccountName;
        oppName += ' - '+ MainYear;
        oppName += ' - ' + RegionName;
        Opportunity opp = new Opportunity();
        opp.RecordTypeId = recTypeOppPadrao;
        opp.Name = oppName;
        opp.Owner = new User (Id = ImportRegionOwner);
        opp.AccountID = AccountID;
        opp.CloseDate = Date.ValueOf(po.Periodo_Aberto_Ate__c); 
        opp.Pricebook2Id  = pbID;
        //opp.CloseDate = System.today() + 90;   
        opp.StageName = 'RFC';
        opp.Micro_Regiao_Principal__c = RegionID;       
        insOpp = (Test.isRunningTest()) ? null : Database.insert(opp);
        return insOpp.getId();
    }   
}
Sforce.NinjaSforce.Ninja
Hi Sandeep,
From the error message it seems your currencies are different. There is nothing wrong in your code. There are few things you can do in this case,
one before inserting your oliList you can check the currencyCode and only insert if it is matching and throw an error for the user to enter correct currency. 
sandeep kumar 44sandeep kumar 44
in this case user does nothing i am trying to create a oppurtunity and line items from a trigger .

 public static void InsertLineItems(Integer StartingMonth,string OppID, Id pbID,Double ImportQuantity, Decimal UnitPrice){
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        for(Integer I=StartingMonth;i<=12;i++){
            if(I!= StartingMonth){
                oliList.add(new OpportunityLineItem(PriceBookEntryId = pbID,OpportunityId = OppID,Tipo__c = '1.Estoque Inicial',Quantity = ImportQuantity,UnitPrice = UnitPrice));
            }
            oliList.add(new OpportunityLineItem(PriceBookEntryId = pbID,OpportunityId = OppID,Tipo__c = '6.Estoque Final',Quantity = ImportQuantity,UnitPrice = UnitPrice));
        }
        if(!oliList.isEmpty()){
           insert oliList;
       }       
    }

error comes when i am trying to insert . how can i set the currency values of the line item same as oppurtunity . 

thanks for replying 

 
Sforce.NinjaSforce.Ninja
Basically from what I see, the values that are passed to this function are wrong. Either pbID is wrong so you will have to select the pricebook id correctly or the oppImportFinalRec have wrong currency. Even if its trigger, shouldn't it begin with user insert or update?

public static void UpsertLineItems(List<Opportunity> oppImportFinalRec,Integer StartingMonth,Id pbID,Double ImportQuantity, Decimal UnitPrice){
}