You need to sign in to do that
Don't have an account?
John Edwards 3
Issue Inserting Multiple OpportunityLineItems
Hello Crew,
I have a problem that is absolutely baffling me. I am trying to write a test class for the following function where it successfully inserts new OpportunityLineItems:
public static String insertOpportunityLineItems(List<OpportunityLineItem> oppLineItemsToUpdate)
{
String updateExceptions = '';
try
{
insert oppLineItemsToUpdate;
}
catch(Exception e)
{
updateExceptions += 'Update OpportunityLineItems: ' + String.valueOf(oppLineItemsToUpdate) + ', Exception: ' + e.getMessage() + ' ';
}
System.debug(updateExceptions);
return updateExceptions;
}
The test I have written is:
@isTest
public static void newInsertOpportunityLineItemsSuccess()
{
OpportunityLineItem startLinteItem = [
Select
Id,
OpportunityId,
Product2Id,
PricebookEntryId,
Quantity,
UnitPrice,
Split_Products__c
From OpportunityLineItem
WHERE Quantity = 1
AND ARC__c = 2
AND TotalPrice = 1]; //'1,2,3'
List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
for(Integer i; i < 2; i++)
{
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
}
String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
System.assertEquals('', testResult);
List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
System.assertEquals(2, testLineItems.size());
}
This fails, with the error INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]. However, when I take the creation of the OpportunityLineItems out of the loop it passes:
@isTest
public static void newInsertOpportunityLineItemsSuccess()
{
//This object is setup in the @setup method
OpportunityLineItem startLinteItem = [
Select
Id,
OpportunityId,
Product2Id,
PricebookEntryId,
Quantity,
UnitPrice,
Split_Products__c
From OpportunityLineItem
WHERE Quantity = 1
AND ARC__c = 2
AND TotalPrice = 1]; //'1,2,3'
List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
System.assertEquals('', testResult);
List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
System.assertEquals(2, testLineItems.size());
}
As if this behavior wasn't bizarre enough if I add just one more OpportunityLineItem to the list and try and again *queue stranger things music* it fails again with the same error INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
@isTest
public static void newInsertOpportunityLineItemsSuccess()
{
//This object is setup in the @setup method
OpportunityLineItem startLinteItem = [
Select
Id,
OpportunityId,
Product2Id,
PricebookEntryId,
Quantity,
UnitPrice,
Split_Products__c
From OpportunityLineItem
WHERE Quantity = 1
AND ARC__c = 2
AND TotalPrice = 1]; //'1,2,3'
List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
System.assertEquals('', testResult);
List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
System.assertEquals(2, testLineItems.size());
}
I am both mystified and horrified. I have similar logic in a trigger I am about to push to production and all end-to-end and ui tests have passed but I don't know if I can deploy with this cockroach in the back of my mind.
If someone could provide some insight I would be extremely grateful.
Thanks!
I have a problem that is absolutely baffling me. I am trying to write a test class for the following function where it successfully inserts new OpportunityLineItems:
public static String insertOpportunityLineItems(List<OpportunityLineItem> oppLineItemsToUpdate)
{
String updateExceptions = '';
try
{
insert oppLineItemsToUpdate;
}
catch(Exception e)
{
updateExceptions += 'Update OpportunityLineItems: ' + String.valueOf(oppLineItemsToUpdate) + ', Exception: ' + e.getMessage() + ' ';
}
System.debug(updateExceptions);
return updateExceptions;
}
The test I have written is:
@isTest
public static void newInsertOpportunityLineItemsSuccess()
{
OpportunityLineItem startLinteItem = [
Select
Id,
OpportunityId,
Product2Id,
PricebookEntryId,
Quantity,
UnitPrice,
Split_Products__c
From OpportunityLineItem
WHERE Quantity = 1
AND ARC__c = 2
AND TotalPrice = 1]; //'1,2,3'
List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
for(Integer i; i < 2; i++)
{
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
}
String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
System.assertEquals('', testResult);
List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
System.assertEquals(2, testLineItems.size());
}
This fails, with the error INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]. However, when I take the creation of the OpportunityLineItems out of the loop it passes:
@isTest
public static void newInsertOpportunityLineItemsSuccess()
{
//This object is setup in the @setup method
OpportunityLineItem startLinteItem = [
Select
Id,
OpportunityId,
Product2Id,
PricebookEntryId,
Quantity,
UnitPrice,
Split_Products__c
From OpportunityLineItem
WHERE Quantity = 1
AND ARC__c = 2
AND TotalPrice = 1]; //'1,2,3'
List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
System.assertEquals('', testResult);
List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
System.assertEquals(2, testLineItems.size());
}
As if this behavior wasn't bizarre enough if I add just one more OpportunityLineItem to the list and try and again *queue stranger things music* it fails again with the same error INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
@isTest
public static void newInsertOpportunityLineItemsSuccess()
{
//This object is setup in the @setup method
OpportunityLineItem startLinteItem = [
Select
Id,
OpportunityId,
Product2Id,
PricebookEntryId,
Quantity,
UnitPrice,
Split_Products__c
From OpportunityLineItem
WHERE Quantity = 1
AND ARC__c = 2
AND TotalPrice = 1]; //'1,2,3'
List<OpportunityLineItem> startList = New List<OpportunityLineItem>();
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
startList.add(New OpportunityLineItem(
OpportunityId = startLinteItem.OpportunityId,
Product2Id = startLinteItem.Product2Id,
PricebookEntryId = startLinteItem.PricebookEntryId,
Quantity = startLinteItem.Quantity,
UnitPrice = startLinteItem.UnitPrice,
ARC__c = 20));
String testResult = OpportunityLineItemTriggerHandler.insertOpportunityLineItems(startList);
System.assertEquals('', testResult);
List<OpportunityLineItem> testLineItems = [Select Id From OpportunityLineItem Where ARC__c = 20];
System.assertEquals(2, testLineItems.size());
}
I am both mystified and horrified. I have similar logic in a trigger I am about to push to production and all end-to-end and ui tests have passed but I don't know if I can deploy with this cockroach in the back of my mind.
If someone could provide some insight I would be extremely grateful.
Thanks!
Try the code mentioned above.
For further guidance you can check this too,
https://developer.salesforce.com/forums/?id=906F00000008z5PIAQ
I hope it helps.