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
Lukasz PiziakLukasz Piziak 

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.
Best Answer chosen by Lukasz Piziak
ShashForceShashForce
Hi Lukasz,

I bet this will work like a charm! I double-checked it out in my developer org as well by setting up everything.

trigger PopulateStandardPrice2 on Product2 (before insert,before update,after insert,after update) {
    list<Id> Idlist = new list<Id>();
    list<product2> ProdsToUpdate = new list<Product2>();
    for(Product2 pr:trigger.new){
        Idlist.add(pr.Id);
    }
    pricebook2 pb = [select Id from pricebook2 where IsStandard = TRUE][0];
    Id stPrId = pb.Id;
    for(product2 p:[select Product_Price_Unit__c,Id from product2 where Id IN :Idlist]){
        for(pricebookentry pbe:[select Product2Id,UnitPrice from pricebookentry where pricebook2Id = :stPrId and product2Id IN :Idlist]){
            If((pbe.Product2Id == p.Id)&&(p.Product_Price_Unit__c!=pbe.UnitPrice)){
                p.Product_Price_Unit__c = pbe.UnitPrice;
                system.debug('this is it '+pbe.UnitPrice);
                system.debug('this is that '+p.Product_Price_Unit__c);
                ProdsToUpdate.add(p);
            }
        }
    }
    If(ProdsToUpdate.size()>0&&trigger.isafter){
            update ProdsToUpdate;
    }
}

Apologies for too many corrections :)

All Answers

ShashForceShashForce
Hi,

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
Lukasz PiziakLukasz Piziak
Hi 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:
User-added image

Could you please advise what I'm doing wrong?
I appreciate any help from you.

Best Regards,
Lukasz
ShashForceShashForce
Hi 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:

trigger PopulateStandardPrice on Product2 (before insert, after insert, before update, after update) {
    list<Product2> tlist = new list<Product2>();
    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(Product2 t:trigger.new){
        t.Product_Price_Unit__c = pbe.UnitPrice;
        tlist.add(t);
    }
    update(tlist);
}

Thanks,
Shashank
Lukasz PiziakLukasz Piziak
Hi 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:
User-added image

Could you please help with this as well???

Thank you so much,
Lukasz
ShashForceShashForce
Hi Lucasz,

Please try this new trigger:

trigger PopulateStandardPrice on Product2 (after insert,after update) {
    list<Id> Idlist = new list<Id>();
    list<product2> ProdsToUpdate = new list<Product2>();
    for(Product2 pr:trigger.new){
        Idlist.add(pr.Id);
    }
    pricebook2 pb = [select Id from pricebook2 where IsStandard = TRUE][0];
    Id stPrId = pb.Id;
    for(product2 p:[select Id from product2 where Id IN :Idlist]){
        for(pricebookentry pbe:[select UnitPrice from pricebookentry where pricebook2Id = :stPrId and product2Id IN :Idlist]){
            p.Product_Price_Unit__c = pbe.UnitPrice;
            ProdsToUpdate.add(p);
        }
    }
    update ProdsToUpdate;
}

Thanks,
Shashank
Lukasz PiziakLukasz Piziak
Hi Shashank,

Please see what I'm getting back from the system after updating product record:
User-added image
ShashForceShashForce
Please try this:

trigger PopulateStandardPrice on Product2 (after insert,after update) {
    list<Id> Idlist = new list<Id>();
    list<product2> ProdsToUpdate = new list<Product2>();
    for(Product2 pr:trigger.new){
        Idlist.add(pr.Id);
    }
    pricebook2 pb = [select Id from pricebook2 where IsStandard = TRUE][0];
    Id stPrId = pb.Id;
    for(product2 p:[select Id from product2 where Id IN :Idlist]){
        for(pricebookentry pbe:[select Product2Id,UnitPrice from pricebookentry where pricebook2Id = :stPrId and product2Id IN :Idlist]){
            If(pbe.Product2Id == p.Id){
                p.Product_Price_Unit__c = pbe.UnitPrice;
                ProdsToUpdate.add(p);
            }
        }
    }
    If(ProdsToUpdate.size()>0){
            update ProdsToUpdate;
    }
}

Thanks,
Shashank
Lukasz PiziakLukasz Piziak
See the message error:
User-added image
Many thanks,
Lukasz
Lukasz PiziakLukasz Piziak
Hi Shashank,
Maybe this information will be helpful for you.
User-added image
Thank you again for your help all your effort.
Lukasz
ShashForceShashForce
Please try this:

trigger PopulateStandardPrice on Product2 (before insert,before update) {
    list<Id> Idlist = new list<Id>();
    list<product2> ProdsToUpdate = new list<Product2>();
    for(Product2 pr:trigger.new){
        Idlist.add(pr.Id);
    }
    pricebook2 pb = [select Id from pricebook2 where IsStandard = TRUE][0];
    Id stPrId = pb.Id;
    for(product2 p:[select Id from product2 where Id IN :Idlist]){
        for(pricebookentry pbe:[select Product2Id,UnitPrice from pricebookentry where pricebook2Id = :stPrId and product2Id IN :Idlist]){
            If(pbe.Product2Id == p.Id){
                p.Product_Price_Unit__c = pbe.UnitPrice;
                ProdsToUpdate.add(p);
            }
        }
    }
    If(ProdsToUpdate.size()>0){
            update ProdsToUpdate;
    }
}

Thanks,
Shashank
Lukasz PiziakLukasz Piziak
Shashank,

Please see below:
User-added image

Thanks,
Lukasz
ShashForceShashForce
Hi Lukasz,

Please try this now:

trigger PopulateStandardPrice on Product2 (before insert,before update) {
    list<Id> Idlist = new list<Id>();
    for(Product2 pr:trigger.new){
        Idlist.add(pr.Id);
    }
    pricebook2 pb = [select Id from pricebook2 where IsStandard = TRUE][0];
    Id stPrId = pb.Id;
    for(product2 p:[select Id from product2 where Id IN :Idlist]){
        for(pricebookentry pbe:[select Product2Id,UnitPrice from pricebookentry where pricebook2Id = :stPrId and product2Id IN :Idlist]){
            If(pbe.Product2Id == p.Id){
                p.Product_Price_Unit__c = pbe.UnitPrice;
            }
        }
    }
}


Lukasz PiziakLukasz Piziak
Hi Shashank,

This time Apex code is not updating my field after edit&save action. There is no errors.
ShashForceShashForce
Hi Lukasz,

I bet this will work like a charm! I double-checked it out in my developer org as well by setting up everything.

trigger PopulateStandardPrice2 on Product2 (before insert,before update,after insert,after update) {
    list<Id> Idlist = new list<Id>();
    list<product2> ProdsToUpdate = new list<Product2>();
    for(Product2 pr:trigger.new){
        Idlist.add(pr.Id);
    }
    pricebook2 pb = [select Id from pricebook2 where IsStandard = TRUE][0];
    Id stPrId = pb.Id;
    for(product2 p:[select Product_Price_Unit__c,Id from product2 where Id IN :Idlist]){
        for(pricebookentry pbe:[select Product2Id,UnitPrice from pricebookentry where pricebook2Id = :stPrId and product2Id IN :Idlist]){
            If((pbe.Product2Id == p.Id)&&(p.Product_Price_Unit__c!=pbe.UnitPrice)){
                p.Product_Price_Unit__c = pbe.UnitPrice;
                system.debug('this is it '+pbe.UnitPrice);
                system.debug('this is that '+p.Product_Price_Unit__c);
                ProdsToUpdate.add(p);
            }
        }
    }
    If(ProdsToUpdate.size()>0&&trigger.isafter){
            update ProdsToUpdate;
    }
}

Apologies for too many corrections :)
This was selected as the best answer
Lukasz PiziakLukasz Piziak
Shashank T H A N K  Y O U!!!!!!!
Its working!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Wow
Thank you so much :-)
Lukasz PiziakLukasz Piziak
Hallo Shashank,

I'm just wondering if you will be able to help with another formula? 
See link (https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AG0tIAG)
Lukasz PiziakLukasz Piziak
Hallo Shashank,

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
Onttu Lindeman 12Onttu Lindeman 12
How do I use a different pricebook? 
Onttu Lindeman 12Onttu Lindeman 12
Or if i wanted to use List Price?
simply buzzes 9simply buzzes 9
Amazing stuff many thanks for your kind information, Could you please have a look on my site (https://cbdmusclebalm.co.uk/) and recommend me a best setting to boost my homepage design. 
jaxon parkerjaxon parker
Amazing stuff many thanks for your kind information, Could you please have a look on my site (cbd muscle balm (https://thecbdsupplier.co.uk/product-category/cbd-balm/)) and recommend me a best setting to boost my homepage design. 
Gaye LawtonGaye Lawton
To populate the standard price product (https://www.pricesurvey.pk/) value from the Standard Price Book to another custom object/field, you can use an Apex trigger. Here is a sample trigger:
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.