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
VPVP 

Saving opp product that does not exist in pricebook

I am using the 6.0 API and Axis Java. I have been told that if I try to save an opportunity product that does not exist in the opp price book - it will get entered in the pricebook on the fly. So I tried sending one opp lineitem with pricebookEntryId=null and productid=DynamicallyCreatedProduct (which does not exist currently in the pricebook) using binding.create(). But the api returned "Error 0: Message=ProductId: id value of incorrect type: DynamicallyCreatedProduct". Am I doing something wrong here?
DevAngelDevAngel
I think a code snippet is in order here.
VPVP
Attaching a sample code snippet:

OpportunityLineItem[] oppLineItemArr=new OpportunityLineItem[1];
OpportunityLineItem oppLineItem=new OpportunityLineItem();
oppLineItem.setDescription("Dynamically Created Opp Prod");
oppLineItem.setOpportunityId(sfOppId);
oppLineItem.setPricebookEntryId(null);
oppLineItem.setProductId("ProductOnTheFly");
oppLineItem.setQuantity(new Double(1));
oppLineItem.setTotalPrice(new Double(1));
oppLineItemArr[0]=oppLineItem;

SaveResult[] createResultArr=null;
try {
createResultArr=binding.create(oppLineItemArr);
} catch (InvalidSObjectFault e1) {
e1.printStackTrace();
} catch (UnexpectedErrorFault e1) {
e1.printStackTrace();
}

- binding is created from the sessionid passed from a custom link in salesforce
- sfOppId is also passed thru' the link
- a pricebook has been selected in SF for that opportunity
- the product "ProductOnTheFly" does not exist in that pricebook
DevAngelDevAngel

Right, so for productId you need the id of the product not the product name or product code.  The ProductId is an 18 character system id that is obtained by querying the Product table.

By the way, you should be using the Product2 entity instead of the product entity.  This means you create a pricebook entry for the line item setting the correct Product2Id on the pricebook entry and then on the opportunity line item use the new PricebookEntryId and don't set the productId.  ProductId and the Product entity are for backwards compatibility with previous integrations.

VPVP
DevAngel wrote:Right, so for productId you need the id of the product not the product name or product code. The ProductId is an 18 character system id that is obtained by querying the Product table. By the way, you should be using the Product2 entity instead of the product entity. This means you create a pricebook entry for the line item setting the correct Product2Id on the pricebook entry and then on the opportunity line item use the new PricebookEntryId and don't set the productId. ProductId and the Product entity are for backwards compatibility with previous integrations.

- But we are trying to create a opp product entry for a product that does not exist in the pricebook - so I do not have any product id. I am trying to create an opp product entry for a dummy product with the id "ProductOnTheFly" that does not exist in the pricebook for that opportunity. Does this mean that the product needs to be present in the SF system and also needs to be associated with the pricebook for that opportunity before I can add it as an opp product?
DevAngelDevAngel
Yes, at least that is what I understand.  If you need a product id, then the product record must exist.