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
wilbur07wilbur07 

Example for a new programmer

Hi!  I'm new to salesforce, and I have a question or call for an example to help get me started.
 
I need to write an Apex trigger that captures when a new product is created and then writes in an entry in the pricebook of 0.00 for that product.
 
Any help would be appreciated.
wilbur07wilbur07

The specific help I need is with how to reference the price book or standard price book.

So, if i have some sample code it might look like:

set a new class

public class test {

// This method updates the Hello field on a list

// of accounts

public static void addPrice(Product[] accs){

for (Product a:accs){

//here i don't know what to use for a.Hello_c  -- should it be a.Standard_Pricebook?????

if (a.Hello__c != 0.00)

a.Hello__c = 0.00;

}

}

}

 

 

trigger trigger_name on Product

(before insert, after insert) {

test.add addPrice(Trigger.new);

}

 

 

 

Any help would be appreciated!

wilbur07wilbur07
public class MyNewProduct {
// This method updates the price field on a list
// of products
public static void addPrice(Product[] accs){
for (Product a:accs){
if (a.standardpricebook.standardprice=='')
a.standardpricebook.standardprice = 0.00;
}
}
}
 
This code is incorrect.  How do I fix it?
wilbur07wilbur07

Okay, I looked up the API Framework in apex_api.pdf and found the object PricebookEntry with the field UnitPrice.

I tried this code but got an error:

Error: Compile Error: SObject type does not allow triggers: PricebookEntry at line 1 column 28
 
public class MyNewProduct {
// This method updates the price field on a list
// of products
public static void addPrice(PricebookEntry[] accs){
for (PricebookEntry a:accs){
if (a.UnitPrice==NULL)
a.UnitPrice = 0.00;
}
}
}
 
and
 
trigger newProductPrice on PricebookEntry (before insert) {
MyNewProduct.addPrice(trigger.new);
}
 
What shall I do now?
wilbur07wilbur07

here's my trigger.  it clears when i save it to triggers but when i try to create a product it throws up an error.  any help is appreciated:

trigger AutoPopulatePricebookEntry on Product2 (after insert) {
 
 PricebookEntry a = new PricebookEntry();
 
 sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
 
 for (Product2 newProduct: Trigger.new) {
  PricebookEntry z = new PricebookEntry(ID='new', Pricebook2Id=s.ID, Product2Id=newProduct.ID, UnitPrice=0.00, IsActive=TRUE, UseStandardPrice=TRUE);
  insert z;
 }
}

 

SuperfellSuperfell
Um, what's the error say ?
wilbur07wilbur07
I solved the problem.  Here's the code:
 
trigger AutoPopulatePricebookEntry on Product2 (after insert) {
 
 PricebookEntry a = new PricebookEntry();
 
 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;
  
 }
}