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
bikla78bikla78 

Insert Pricebook entry

When a product2 record is inserted the product2.payrate should  insert or update the pricebookentry.unitprice field with the custom calculation and assign  01s300000000EfuAAE pricebookentry.pricebook2id

pricebookentry.unitprice =     product2.payrate / .90 *  2.5 + 5 and rounded to the nearest 5 dollars

I have started this but having issues with it with associating the objects

 
trigger Product_Before_Insert_Update on Product2 (before insert) {
    
    sObject s = [select , payrate__cID from Pricebook2 where id = '01s300000000EfuAAE'];

    for (Product2 newProduct: Trigger.new) {

     double billrate = payrate / .90 * 2.5 + 5;

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


Message Edited by bikla78 on 09-30-2009 05:01 PM
ThomasTTThomasTT

...and what's wrong?

 

ThomasTT

 

P.S.

you shouldn't insert the PriceBookEntry in the loop... put in a list and insert the whole list after the loop.

Message Edited by ThomasTT on 09-30-2009 10:37 PM
bikla78bikla78

Okay , here we go. I got the pricebookentry to insert but how to i grab the product2.payrate__c and calculate the formula and insert into the unitprice as a currency datatype?

 

formula is this: pricebookentry.unitprice = product2.payrate__c / .90 * 2.5 + 5 and round the answer to nearest 5 dollar

 

trigger Product_Before_Insert_Update 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;

}

}
 
Message Edited by bikla78 on 09-30-2009 11:57 PM
ThomasTTThomasTT

???  You're pretty much answering to your question... I don't know what's the point... but, isn't that just

 

trigger Product_Before_Insert_Update on Product2 (after insert) {

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

for (Product2 newProduct: Trigger.new) {

double unitPrice = newProduct.payrate__c == null ? 0.0 : newProduct.payrate__c / .90 * 2.5 + 5 and round the answer to nearest 5 dollar
PricebookEntry z = new PricebookEntry(Pricebook2Id=s.ID,Product2Id=newProduct.ID, UnitPrice=unitPrice, IsActive=TRUE, UseStandardPrice=FALSE);
insert z;

}

}

 at least, null point check is something I can help (double could be null in Apex) but, what was wrong?  you are getting newProduct.id so you know how to get values from object, and you create and insert a object, so you know how to insert... it didn't work? you got an error? currency datatype is the point? and have you ever tried it?

 

ThomasTT

 

Message Edited by ThomasTT on 10-01-2009 03:24 AM
bikla78bikla78

I changed my code just to this for now to get it to compile and fine tune as we go forward

trigger Product_Before_Insert_Update on Product2 (after insert) {

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

for (Product2 newProduct: Trigger.new) {

double unitPrice = newProduct.payrate__c / .90 * 2.5 + 5;

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

}

}
 

and it compiles it but I get the following exeception

 Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Product_Before_Insert_Update caused an unexpected exception, contact your administrator: Product_Before_Insert_Update: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: Trigger.Product_Before_Insert_Update: line 10, column 1

 

2 things:

 

1. I don't see a currency datatype. The current datatype for the payrate__c is a number(5,2). How do I convert the unitprice to same format?

 

 

2. You're right- since it is a before insert, it thinks the value is null before the trigger executes so I am getting an error.

 

Thanks Again Thomas

ThomasTTThomasTT

I think there can be only 1 PriceBookEntry for a set of PriceBook2, Product2 (and maybe CurrencyIsoCode).

I can imagine that your trigger can easily violate the restriction. That makes sense.

It's up to the specification, but you may query the PriceBookEntry first, and decide to update it or create new one (if no record).

 

> 1.

Curenncy and Number type is a type of field. I think the both of corresponding Apex data type is double or decimal.

> 2.

it's not about before insert... if payrate__c is set from VF page or Apex Class, you can get the value even in before insert trigger. You or user may not initialize the field/variable, that's what I was talking about.

 

ThomasTT

Message Edited by ThomasTT on 10-01-2009 12:21 PM
bikla78bikla78

Couple things I updated was I changed the trigger to a before insert and also initilized the payrate to 0.00. Once I can get the unit price to update, I'll work on the decimal conversion and concat a $.Now I am getting the error that Pricebookentry can not be inserted since Product2 is missing but I included this statement: ID ProductID = newProduct.ID which should grab it from my Product2 but still having the error

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Product_Before_Insert_Update caused an unexpected exception, contact your administrator: Product_Before_Insert_Update: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Product2Id]: [Product2Id]: Trigger.Product_Before_Insert_Update: line 13, column 1

 

 

 

trigger Product_Before_Insert_Update on Product2 (before insert) {

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


for (Product2 newProduct: Trigger.new) {

 newProduct.payrate__c = 0.00;
double unitPrice= newProduct.payrate__c / .90 * 2.5 + 5;
ID ProductID = newProduct.ID;

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

}

}

ThomasTTThomasTT

...why don't you just make it success first, then change things?

1)  REQUIRED_FIELD_MISSING

Did you check the value of ProductID? check it. it's supposed to be null.

Change back to after insert and tell me the reason why it needs to be before insert.

ID is generated after insert. If you need to create PriceBookEntry based on the Product2, you need to insert Product2 first.

Use System.debug() and system log. You are coding blindly. Nobody can do that.

2) Once I can get the unit price to update, I'll work on the decimal conversion and concat a $.

You do not have to do any of this. UnitPrice is currency field as field definition. As long as you put the value as double or decimal (double shoudl work fine), SFDC will format it as currency with the user's currency code.

ThomasTT

Message Edited by ThomasTT on 10-01-2009 03:51 PM
bikla78bikla78

Good news. I changed it back to after insert to work and it calculated it correctly. I switched it back to an after insert. And looks like it auto changed it to currency type. Now when I update the payrate I get:

 


Product_Before_Insert_Update: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: Trigger.Product_Before_Insert_Update: line 12, column 1

 

How do I change the logic to include update z;

 

 

trigger Product_Before_Insert_Update on Product2 (after insert, after update) {

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

for (Product2 newProduct: Trigger.new) {


double unitPrice = newProduct.payrate__c / .90 * 2.5 + 5;


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

}
 
Message Edited by bikla78 on 10-01-2009 01:25 PM
ThomasTTThomasTT
I recommend to read Apex reference manual - Apex Trigger section. Trigger.isInsert/isUpdate is the one you shoud look at. I have an urgent real job to do. For update, you need to query PriceBookEntry first and update the value and update the object. ThomasTT