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
ralphralph 

DefaultPrice missing when adding a Product

When I execute the following code I get an error:  Required fields are missing: [DefaultPrice]

As you can see the DeafultPrice has beend hard coded to 1000.00.

Any ideas?

Thanks

 

// get pricebook id
string _priceBookId = GetProcebookId();

//   add part
sforce.Product _product = new sforce.Product();
_product.PricebookId = _priceBookId;
_product.ProductCode = _partCode;
_product.Name = _partName;
_product.DefaultPrice = 1000.00;    // <<<<<<<<<<<<<<<<<<<<<,
_product.Description = "created by poc";

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

if ( sr.Length == 1 )
{
 if (sr[0].success)
 {
  System.Diagnostics.Trace.WriteLine("Part Created: " + sr[0].id);
  _productId = sr[0].id;
 }
 else
 {
  sforce.Error er = sr[0].errors[0];
  System.Diagnostics.Trace.WriteLine(er.message);
  v_message = "Create Part - FAILED
"+er.message;
}

 

ralphralph

Asked and answered...

Need to add the line:

_product.DefaultPriceSpecified = true;

DevAngelDevAngel

Hi Ralph,

In .Net, for some fields (date, int, double) you need to also set a property that tells the .Net serializer that you set the value and it is not the default for the field type.  So in your code you need to add a line that is _product.DefaultPriceSpecified = true;.  The types mentioned above cannot be null, therefore .Net does not know if the value is the default or one you put in unless you tell it explicitly user the __Specified field.  This field is created when the web service proxy is generated.

 

Cheers