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
maz00maz00 

Problem adding/updating products to opportunities

Hi,

I'm having problems creating and updating products assigned to opportunities. Here is my setup

* I have defined around 10 products and with their defaultprice
* I have created a way to update currently assigned products to opportunities which works fine as long as the product was added to the opportunity using SalesForce.com and not my application. 
* I have created a way to assign new products to individual opportunities.
* My application automatically calculates the UnitPrice for individual account based on certain criteria.

Problem: Everytime I add a new product to my opportunity and I try to update it, it uses the TotalPrice/Quantity to calculate my UnitPrice even though I have the .UnitPrice assignment which messes up everything since I always want to take the Quantity * UnitPrice.  However, when I call my update twice sending the same parameters it then works fine.  Basically updating the record twice with the same parameters (UnitPrice, Quantity)

My code assignment
For Update
sforce.OpportunityLineItem OppLineItem = new sforce.OpportunityLineItem();

OppLineItem.Id = Id;
OppLineItem.Quantity = Quantity;
OppLineItem.QuantitySpecified = true;
OppLineItem.UnitPrice = resellerPrice;
OppLineItem.UnitPriceSpecified =
true;
OppLineItem.udfPSPrice__cSpecified =
true;

sforce.SaveResult[] sr = binding.update(new sforce.sObject[]{OppLineItem});

and for Insert

sforce.OpportunityLineItem OppLineItem = new sforce.OpportunityLineItem();

OppLineItem.OpportunityId = OppId;
OppLineItem.ProductId = ProductId;
OppLineItem.UnitPrice = resellerPrice;
OppLineItem.UnitPriceSpecified = true;
OppLineItem.Quantity = Quantity;
OppLineItem.QuantitySpecified =
true;
sforce.sObject[] records = new sforce.sObject[] {OppLineItem};
sforce.SaveResult[] saveResults = binding.create(records);

Can someone please help me get this working? Why does it take the Total and divides by Quantity the first after an insert?

Thank you
Maziar A.


 

DevAngelDevAngel

Hi maz00,

Below is the correct way to create and update opportunity line items.

//Create a line item on an opp 00630000000h8df

sforce.OpportunityLineItem oli = new Tester.sforce.OpportunityLineItem();

oli.Description = "Test Line Item";

oli.OpportunityId = "00630000000h8df";

oli.PricebookEntryId = "01u300000009SeAAAU";

oli.Quantity = 2;

oli.QuantitySpecified = true;

oli.UnitPrice = 500.00;

oli.UnitPriceSpecified = true;

sforce.SaveResult sr = binding.create(new sforce.sObject[] {oli})[0];

if (sr.success)

System.Windows.Forms.MessageBox.Show("Opportunity Line Item created: " + sr.id);

else

System.Windows.Forms.MessageBox.Show("Error creating line item: " + sr.errors[0].message);

//Save the id

string id = sr.id;

//Update the line item and see what happens

sforce.OpportunityLineItem oliNew = new Tester.sforce.OpportunityLineItem();

oliNew.Id = id;

oliNew.Quantity = 3;

oliNew.QuantitySpecified = true;

oliNew.UnitPrice = 600.00;

oliNew.UnitPriceSpecified = true;

sr = binding.update(new sforce.sObject[] {oliNew})[0];

if (sr.success)

System.Windows.Forms.MessageBox.Show("Opportunity Line Item updated: " + sr.id);

else

System.Windows.Forms.MessageBox.Show("Error updateing line item: " + sr.errors[0].message);