+ Start a Discussion

Inserting products

Hi all,

I have an app that users can add products to a quote through a wizard that grabs prices from a database and then uploads them to SF.com. It isn't woring and I don't know what I am doing wrong. I am querying all the ids and information from the corresponding pricebook in SF.com. I am attaching my code. Any help would be greatly appreciated.

Thanks in advance.


string sQueryProductID = "Select ID From Pricebook2 where Name = '" + sPriceListName + "'";
sForce.QueryResult qrProd = binding.query(sQueryProductID);
string sPricebookID = string.Empty;
string sPriceBookEntry = string.Empty;
string sProdID = string.Empty;
if (qrProd.size > 0)
sForce.Pricebook2 objProd = (sForce.Pricebook2)qrProd.records[0];
sPricebookID = objProd.Id.ToString();

sForce.SaveResult sr = new sForce.SaveResult();
for (int x = 0; x < sUSPrice.Length; x++)
float dTotal = float.Parse(sQuantities[x].ToString()) * float.Parse(sUSPrice[x].ToString());

string sQueryProdID = "Select ID, Name From Product2 where ProductCode = '" + sProdCode[x] + "'";
sForce.QueryResult qrProdID = binding.query(sQueryProdID);
if (qrProdID.size > 0)
sForce.Product2 objProd2 = (sForce.Product2)qrProdID.records[0];
sProdID = objProd2.Id.ToString();
string sQueryProduct = "Select ID From PricebookEntry where Product2Id = '" + sProdID + "'";
sForce.QueryResult qrProdEntry = binding.query(sQueryProduct);
if (qrProdEntry.size > 0)
sForce.PricebookEntry objProd = (sForce.PricebookEntry)qrProdEntry.records[0];
sPriceBookEntry = objProd.Id.ToString();

sForce.OpportunityLineItem oli = new sForce.OpportunityLineItem();
oli.ProductId = sProdID;
oli.Description = sDesc[x];
oli.OpportunityId = oppId;
oli.PricebookEntryId = sPriceBookEntry;
oli.Quantity = double.Parse(sQuantities[x]);
oli.QuantitySpecified = true;
oli.UnitPrice =double.Parse(sUSPrice[x]);
oli.UnitPriceSpecified = true;
sr = binding.create(new sForce.sObject[] {oli})[0];
Try leaving the ProductId off. Looks like you are getting an Id from a product2 object and trying to use it in the ProductId of the OLI. The productId field on the OLI is for backward compatibility and you should use the PriceBookLineitem only instead.
Here is another problem I ran into. We have a couple different price lists (based on region) that have different descriptions and prices for products with the same product code.

So the queries about are not always returning the correct one. I'm not sure how to distinguish one from the other.


Here is another similar problem. I am trying to update an Opportunity Custom field.

Here is my code.

sForce.Opportunity oop = new sForce.Opportunity();
oop.Id = oppId;
oop.Local_Currency_Amount__c = dLocalTotal;
sr = binding.update(new sForce.sObject[] {oop})[0];


What's the problem ? did you look at the success flag and errors collection in the returned SaveResult ?