OpportunityLineItem:- Field Integrity Error

I am trying to automate the following functionality: When an opportunity stage becomes 'Pending Payment', insert 2 'OpportunityLineItems' using 2 predefined Products.

This is my current code. I am calling the class on an after update event on the Opportunity Sobject:

public void addProductWhenStageIsPendingPayment() {          Set<id> ids = new Set<Id>();         for(Opportunity o : newOpportunities.values()){             if(o.stagename=='Pending Payment')                 ids.add(o.id);         }           List<Opportunity> opps = [Select id, Start_Date__c, End_Date__c,CurrencyIsoCode,PriceBook2id from Opportunity where id in: ids];// and  Start_Date__c!=null and  End_Date__c!=null];          List<Product2> products = [Select id,name, CurrencyIsoCode from Product2];         Id product1Id=NULL;         id product2id=NULL;          list<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry                                               WHERE  PriceBook2.isStandard=true ];         Id priceBook1Id=NULL;         id priceBook2id=NULL;          List<OpportunityLineItem> oppsline = new List<OpportunityLineItem>();          for(Opportunity op : opps)           {             // get the product ids              for(Product2 p : products)             {                 if(p.name=='Name A' & p.CurrencyIsoCode==op.CurrencyIsoCode)                 product1Id=p.id;                 if(p.name=='Name B' &   p.CurrencyIsoCode==op.CurrencyIsoCode)                 product2Id=p.id;              }              // get the price book ids             for(PriceBookEntry pbe : priceBookList)             {                 if(pbe.Product2Id== product1Id)                     priceBook1id = pbe.id;                 if(pbe.Product2Id == product2Id)                     priceBook2id = pbe.id;             }                Date startDate = op.Start_Date__c;             Date endDate = op.End_Date__c;             Integer monthDiff = startDate.monthsBetween(endDate);             monthDiff++;              for(integer i=0; i<monthDiff ; i++)             {                 OpportunityLineItem oli1 = new OpportunityLineItem( PricebookEntryId=  priceBook1Id  , Opportunityid=op.id,UnitPrice=1000,Quantity=22,ServiceDate=endDate,  Description='Name A',Estimate_Actual__c='Estimated');                 OpportunityLineItem oli2 = new OpportunityLineItem( PricebookEntryId=  priceBook2Id , Opportunityid=op.id,UnitPrice=250,Quantity=22,ServiceDate=endDate,    Description='Name B',Estimate_Actual__c='Estimated');                 oppsline.add(oli1);                 oppsline.add(oli2);             }          }          insert oppsline;      } 

This is the current error I am getting:

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger MasterOpportunityTrigger caused an unexpected exception, contact your administrator: MasterOpportunityTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]: Class.ClassOpportunityHandler.addProductWhenStageIsPendingPayment: line 81, column 1

Replay

Category: apex Time: 2016-07-28 Views: 10

Related post

  • field integrity exception: unknown (scheduling not enabled on product): 2016-06-30

    I have written a trigger, when opportunityLineItem is inserted the quantity gets scheduled meaning opportunityLineItemSchedule are also gets inserted. Now to re-enter the quantity I have cloned the edit page of opportunityLineItem where user can re-e

  • Communicator Messages not being saved to Outlook due to Outlook Integration error 2011-05-06

    For the most part my Office Communicator appears to be configured correctly. I can login to my work account and see the work contact list. Outlook is working perfectly had a weird profile problem initially but that was fixed. Unfortunately, even thou

  • Can fields validation errors be displayed below the fields? 2012-04-24

    Can field validation errors be displayed below the fields instead of as a list at the top of the form? --------------Solutions------------- I think you are looking for something like the Inline Form Errors Module. Here is a quote about it (from the m

  • I get image field upload errors with a custom node_add() form 2012-05-11

    I've exposed a node_add form and styled it for certain roles to use. The form is being displayed in panels, through an 'article' node type using the PHP text filter. Users can create new nodes with no issues, but there are errors displayed when users

  • Required field validation error message – Next to field vs. in a Popup 2012-08-13

    In our application, we mark all the required fields with an asterisk and when user tries to perform an action without providing values for all the required fields, we display an error message next to the field, for which the value is missing. Now, ou

  • Field calculator error 2013-04-07

    I realize I posed this question, and got answers, several days ago (see (Field calculator error)) , but none seem to really be working, and so am posing again. Below are two screenshots that show the fields with field properties, and another showing

  • Why does ApexPages.getMessages() return multiple Message objects for field validation errors? 2014-10-07

    I'm using a standard controller with a command button that saves a record. When one of the fields is invalid, the ApexPages.getMessages() function returns multiple messages like this: 19:36:58:445 USER_DEBUG [214]|DEBUG|*** ApexPages.Message["Invalid

  • Outlook integration error in communicator 2014-10-16

    I am using office communicator 2007. I have configured my outlook with two exchange servers(one for my office server another is client server). communicator will take my office credentials. But it is giving an yellow colored warning like "Outlook int

  • VisualForce Field Level Error Message 2015-06-02

    I have a few validation rules which display errors on fields. This is working fine for Standard Pages. But when these validations fail on a VisualForce page, the error messages show on the top of the page in "apex:messages". Is there a way to sh

  • Field validation error on nodereference field during D6 to D7 migration 2015-09-17

    I'm migrating a D6 site to D7. There are several content types that have cck noderefenrce fields that use referenced views for content types that can be referenced. When I run the migration, I get a field validation error: "Field validation error for

  • Aura Integration Error when installing unmanaged package 2015-09-25

    This question already has an answer here: "Aura Integration Service Error" When installing a private package 1 answer I've uploaded unmanaged private package from sandbox and trying to install the package in developer edition but getting below e

  • Open source archiver with integrated Error Correction Code 2015-12-03

    Is there an open source archiver with integrated error correction code? (preferably windows 7, linux is ok, too) presently the most stable procedure for me seems to be archiving with 7z and then creating recovery file sets using multipar (http://mult

  • Field Integrity Exception for Name 2016-07-05

    I am trying to create a Related List with a custom object using the Metadata API. When I make the call I get a Field Integrity Exception saying that the field "Name" does not exist on my object. However, when I go to the object in Setup -> Cr

  • Migrating Drupal 6 - 7 Content Type with optional file field, throws error 2014-11-10

    I am currently working on migrating content from a Drupal 6 site to Drupal 7 and have started with a single content type for testing. The source content type has an optional image field, however if I try to import only those nodes with a file attache

  • Accelerometer double integration error 2015-02-20

    I wanted to use an accelerometer or IMU to measure speed and displacement over a period of about 1 minute or 400m. From this answer I found at stackoverflow, the error propagates at a rate of \$t^2\$ (without considering the rotation), so \$60^2 = 36

  • Package Install Failure (Aura Integration Error) 2015-10-15

    I am trying to install app that contains more than 3000 fields and around 25 objects and 10-12 tabs. I know my app is not compatible with PE but EE should be good to go as each object contains less than 250 fields. This app is managed release. There

  • nested_form shows extra fields after error messages 2016-02-06

    I'm using simple_nested_form_for to build a form with nested fields. Fields are added dynamically When rendering the form with errors (via create) the nested fields go wrong. The same nested fields are shown multiple times and with the wrong index va

  • Unable to parse page for field values error in ULS log 2011-02-16

    We get this error on every page request on our Production environment. All other environments are ok. The environment has been upgraded from SharePoint 2007 to SharePoint 2010. Unable to parse page for field values: The 'mso:CustomDocumentProperties'

  • Field types error when ribbon is shown 2012-01-03

    We upgraded our MOSS 2007 to SharePoint 2010 yesterday. Everything seemed fine until I chose Show Ribbon from the site menu. Now I get an error like this: "One or more field types are not installed properly. Go to the list settings page to delete the

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.535 (s). 13 q(s)