You need to sign in to do that
Don't have an account?

How to populate standard price product value from Standard Price Book to another custom object/field?
I'm just wondering if its possible to auto populate standard price product value from Standard price book to another custom field? I'm preparing our company app as a product configurator where ideally I would like to manage my pricese in one place in SF. Maybe Apex Code?
Thanks for any ideas and help.
Thanks for any ideas and help.
I bet this will work like a charm! I double-checked it out in my developer org as well by setting up everything.
Apologies for too many corrections :)
All Answers
You will have to use ann apex trigger to do this. Here is a sample:
trigger PopulateStandardPrice on Test_Custom__c (before insert, after insert, before update, after update) {
list<test_custom__c> tlist = new list<test_custom__c>();
pricebook2 stpb = [select Id from pricebook2 where Name = 'Standard'][0];
Id stpbId = stpb.Id;
pricebookentry pbe = [select UnitPrice from pricebookentry where Pricebook2Id = :stpbId and Name = 'Product Name'];
for(test_custom__c t:trigger.new){
t.Price_from_trigger__c = pbe.UnitPrice;
tlist.add(t);
}
update(tlist);
}
If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.
Thanks,
Shashank
Thank you for apex trigger above.
I have used on Product object as a new trigger and still getting something wrong.
I have created my custom field (Product_Price_Unit__c) on Product2 object.
Code is like follows:
trigger PopulateStandardPrice on Product2 (before insert, after insert, before update, after update) {
list<Product_Price_Unit__c> tlist = new list<Product_Price_Unit__c>();
pricebook2 stpb = [select Id from pricebook2 where Name = 'Standard'][0];
Id stpbId = stpb.Id;
pricebookentry pbe = [select UnitPrice from pricebookentry where Pricebook2Id = :stpbId and Name = 'Product Name'];
for(Product_Price_Unit__c t:trigger.new){
t.Price_from_trigger__c = pbe.UnitPrice;
tlist.add(t);
}
update(tlist);
}
When saving I'm getting the error:
Could you please advise what I'm doing wrong?
I appreciate any help from you.
Best Regards,
Lukasz
The trigger is on Product2 object. Hence, Trigger.new will have product records and not yout custom object records. The "test_custom__c" I mentioned in my code is a custom object, not a field. If you want the custom field (Product_Price_Unit__c) on the standard product object, please try something like this:
Thanks,
Shashank
My appologies for that mistake but I'm new to Apex coding. This is not the end of my problems. After saving this code I went to my product record and I have tryied to edit and save it again to see if trigger is working.
Please see the error message from Salesforce:
Could you please help with this as well???
Thank you so much,
Lukasz
Please try this new trigger:
Thanks,
Shashank
Please see what I'm getting back from the system after updating product record:
Thanks,
Shashank
Many thanks,
Lukasz
Maybe this information will be helpful for you.
Thank you again for your help all your effort.
Lukasz
Thanks,
Shashank
Please see below:
Thanks,
Lukasz
Please try this now:
This time Apex code is not updating my field after edit&save action. There is no errors.
I bet this will work like a charm! I double-checked it out in my developer org as well by setting up everything.
Apologies for too many corrections :)
Its working!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Wow
Thank you so much :-)
I'm just wondering if you will be able to help with another formula?
See link (https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AG0tIAG)
Are you familiar with Apex Classes?
I have a serious problem with deploying my app where before I have got a great help from you.
Please see the link for more details: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AeyzIAC
Any your help would be more than appreciate.
Regards
Lukasz
trigger PopulateStandardPrice on Test_Custom__c (before insert, after insert, before update, after update) { list<test_custom__c> tlist = new list<test_custom__c>(); pricebook2 stpb = [select Id from pricebook2 where Name = 'Standard']; Id stpbId = stpb.Id; pricebookentry pbe = [select UnitPrice from pricebookentry where Pricebook2Id = :stpbId and Name = 'Product Name']; for (test_custom__c record: trigger.new) { record.Standard_Price__c = pbe.UnitPrice; tlist.add(record); } if (tlist.size() > 0) { update tlist; } }
This trigger will populate the Standard_Price__c field on all Test_Custom__c records with the standard price from the Standard Price Book.
Here is a breakdown of what the trigger is doing:
The first line creates a list of Test_Custom__c records that were created or updated in the trigger context.
The second line gets the Id of the Standard Price Book.
The third line gets the UnitPrice of the product with the name Product Name in the Standard Price Book.
The fourth line loops through the list of Test_Custom__c records and sets the Standard_Price__c field to the UnitPrice from the Standard Price Book.
The fifth line checks if there are any records that were updated.
The sixth line updates the records that were updated.
This is just a sample trigger, and you may need to modify it to fit your specific needs. For example, you may need to change the name of the Standard Price Book or the name of the product. You may also need to add additional logic to the trigger, such as checking if the Standard_Price__c field is already populated.