You need to sign in to do that
Don't have an account?
wilbur07
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.
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!
// 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;
}
}
}
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:
// 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;
}
}
}
MyNewProduct.addPrice(trigger.new);
}
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;
}
}
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;
}
}