+ Start a Discussion
shan876shan876 

Pricebook..Standard Price???

Hi all:
   I am using an Integration Tool to upload data into the products2 and pricebookEntry table... Now, I can load data into the product2 table no issues there. But when I try to load the pricebookentry to link back to product2Is and pricebook2Id.. the issue I have having is "[SFDC] No standard price defined for this product    [SFDC] STANDARD_PRICE_NOT_DEFINED"
Ok Now I know that standard price is the default unit price if you go through the excel connector in a Product table...
But how do you fill it through an API when it is not part of the product2 table???
Any help ... Please Advise...
Thanks
Shan
wilbur07wilbur07
You have to set the standard prices for each product. I have a trigger that does this upon creation of a product.


trigger AutoPopulatePricebookEntry on Product2 (after insert) {

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];

for (Product2 newProduct: Trigger.new) {

PricebookEntry z = new PricebookEntry(Pricebook2Id=s.ID,Product2Id=newProduct.ID, UnitPrice=0.00, IsActive=TRUE, UseStandardPrice=FALSE);
insert z;

}

}


Hope this helps,

James
shan876shan876

 hi Wilbur:

   Thank you for the response... This trigger is this a custom s-control?? or an app you wrote...

It looks good but How do I utilize it and where would I place it..

Because I am uploading the products from an integration tool but ready to write a VB app to do so?

Please advise

thanks

Shan

wilbur07wilbur07
It is a trigger on Product2. Setup->Customize->Product2->Triggers, then copy and paste the code in.

You may want to set the Unit Price to something other than 0.00 in the code.

To set the standard price for all your products you can manually insert a price for each product in salesforce or write an s-control that sets the price and then run it from a custom tab or button. The code will look something like this:

Product2[] a = [select id from Product2];

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];

for( Product2 b : a ) {
PricebookEntry z = new PricebookEntry(Pricebook2Id=s.ID, Product2Id=b.ID, UnitPrice=0.00, IsActive=TRUE, UseStandardPrice=FALSE);
insert z;
}

but of course your s-control will not use APEX code but probably AJAX code with salesforce classes.

James
wilbur07wilbur07
You said: "Because I am uploading the products from an integration tool but ready to write a VB app to do so?"

On second thought if you are uploading the products from somewhere else, upon creation the trigger will create a standard price for each of your products. No need to write an s-control that does this.

James
shan876shan876

Hi :

   So I created the Trigger and package in my test environment and now I am trying to upload it and I get the following error:

No testMethods found in the selected Apex code for the package

 

What does that mean??

wilbur07wilbur07

you have to create a test class to test the code in the trigger.  It's not hard, just read the documentation on unit testing.

 

James

shan876shan876

so do I create this class within the same trigger that I created or a different trigger??

 

shan876shan876
Could you give me the link to the doc that has this I am trying to find it in the Apex developer guide.. Maybe not looking at the right thing... I am very new at triggers as this is my first one...
Thanks
Shan
wilbur07wilbur07
Here's my unit test.  Setup->Develop->Apex Classes->New
then paste my code in and save.

When you create the package include this class.

public
class myClass {

static testMethod void testInsertLine() {
Product2 p = new product2(name='x',isactive=TRUE);


insert p;



System.assertEquals(0.00, [select UnitPrice from PricebookEntry
where product2id = :p.id].UnitPrice);



}
}
shan876shan876
what is the smiley face represent in your code:
System.assertEquals(0.00, [select UnitPrice from PricebookEntry
where product2id = .id].UnitPrice);

Also, thank you so much for helping me out.. I do really appreciate this...
Thanks
Shan
wilbur07wilbur07
it's not meant to be a smiley face it's :p or colon "p"
shan876shan876

hi:

   So I deployed the trigger and class and all through the eclipse tool... Works nicely

Now, I am using the intergration tool to upload my products into salesforce.com through Bluewolf ESI Tool

I keep getting an email and errors stating:

Apex script unhandled trigger exception by user/organization: 00550000000lxVc/00D500000006sSF

AutoPopulatePricebookEntry: execution of AfterInsert

caused by: System.Exception: Too many DML statements: 21

Trigger.AutoPopulatePricebookEntry: line 9, column 11

 
What does this mean??
It works when I do it manually to create a new product but does work with the integration tool when it uploads?
I even set the amount of records to upload to 100 then tried 99 then tried 200... it did not work... It did work for some and the others did not...
Anyone, get this message ever???
Thanks
Shan
 
wilbur07wilbur07
There's a limit of 20 dml statements or statements which affect the database and when you migrate data to salesforce it does it in bulk, or all at the same time.  You have to set your bulk uploads to under 20 entries at a time to resolve this.  I don't know how since I don't do migration into salesforce, perhaps you can ask again here on this discussion board.

James
shan876shan876

I emailed support and they told me to:

It looks like you need to pull your insert statement out of that loop- instead of doing an insert for each record processed, you can cache them all up in an array-type structure and then insert them all at once

so modified it to this:
Code:
trigger AutoPopulatePricebookEntry on Product2 (after insert) {
     sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
     
     List<PriceBookEntry> toInsert = new List<PriceBookEntry>();
     
     for (Product2 newProduct: Trigger.new) {

        PricebookEntry z = new PricebookEntry(Pricebook2Id=s.ID,Product2Id=newProduct.ID,UnitPrice=0.00,
            IsActive=TRUE, UseStandardPrice=FALSE);
         toInsert.add(z);
     }
     
     insert toInsert;
 }

 I have not tried it ... still trying to figure out a better way...
Thanks
Shan
Paulo PerezPaulo Perez
Hello Shan

I'm trying execute you test and did not work, show me message fail  below:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoPopulatePricebookEntry: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.AutoPopulatePricebookEntry: line 2, column 1: []

Regard,
Paulo
EliHEliH
I got the same error and found out it was because I was trying to add the products to a custom pricebook. According to this document:
https://help.salesforce.com/servlet/servlet.FileDownload?file=01530000001OdbFAAS (https://help.salesforce.com/servlet/servlet.FileDownload?file=01530000001OdbFAAS" target="_blank)
you have to add them to the standard pricebook first, and then a standard price exists so you can add them to a custom pricebook.