+ Start a Discussion
PC MayBevPC MayBev 

Trigger to retrieve price from price book to customer object

Hi,
Is there any one written a trigger to retrieve price from selected price book into customer object?
Please advice
Thank you
ShashForceShashForce
Hi,

Couple of questions here. Should this trigger be on the Customer object (is this a custom object or a renamed standard object) or the pricebook or product? How is the priebook selected?

Thanks,
Shashank
PC MayBevPC MayBev
Hi Shashank Thank you for your reply. This is custom object builded from scratch (named Direct Order). They need a quick method to key in order rather than create new opportunity and add product. Price book was selected in Account record. In the custom object, user select account , quantity and product. Trigger is on the custom object. I need the price info so that to calculate the price. Standard formula can't achieve this. Thank you again for your support Best regards Philippe
ShashForceShashForce
You would need a trigger and a class (to avoid recursion) for this. I haven't taken quantity into consideration.

Class:

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Trigger:

trigger GetPrice on Direct_Order__c (after insert, after update) {
    if(checkRecursive.runOnce()){
        set<Id> doIds = new set<Id>();
        set<Id> accIds = new set<Id>();
        set<Id> prodIds = new set<Id>();
        set<Id> pbIds = new set<Id>();
        list<direct_order__c> updlist = new list<direct_order__c>();
        for(direct_order__c dr:trigger.new){
            accIds.add(dr.account__c);
            prodIds.add(dr.product__c);
            doIds.add(dr.Id);
        }
        list<direct_order__c> dolist = [select product__c,account__r.pricebook__c,price__c from direct_order__c where Id IN :doIds];
        for(account a:[select pricebook__c from account where Id IN :accIds]){
            pbIds.add(a.pricebook__c);
        }
        list<pricebookentry> pbelist = [select unitprice,product2Id,pricebook2Id from pricebookentry where product2Id IN :prodIds and pricebook2Id IN :pbIds];
        for(direct_order__c d:dolist){
            for(pricebookentry pbe:pbelist){
                if(d.product__c==pbe.product2Id){
                    if(d.account__r.pricebook__c==pbe.pricebook2Id){
                        direct_order__c dro = new direct_order__c();
                        dro.Id = d.Id;
                        dro.price__c = pbe.unitprice;
                        updlist.add(dro);
                    }
                }
            }
        }
        if(updlist.size()>0) update updlist;
    }
}

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
PC MayBevPC MayBev
Hi Shashank Thank you for your reply. Let me check it out and come back to you. Really appreciate for your help Regards Phil
PC MayBevPC MayBev
Hi Shashank Thank you for your email. I am facing some problem here. The naming issue: Account, and Price In my custom object named Direct Order, user need to select an account (the filed name is Customer) Next user can select 2 products, named Product1 and Product2. I need a trigger to populate Price1 and Price2. Can you help to modify your code? Sorry to disturb you. Appreciate your kind support Thank you & best regards Philippe
PC MayBevPC MayBev
Hi Shashank One more question. In my Direct Order object, the field Price1 and Price2, the Data Type should be changed to currency right? Please advice Thank you & best regards Philippe
ShashForceShashForce
I would suggest you to give it a shot and let me know if you are specifically stuck somewhere. And yes, Price1 and Price2 will be currency fields.
PC MayBevPC MayBev
Hi Shashank, Thank you for your reply I am new in writing Apex Code. To be exact, I have a problem to rename the field "Customer" to "Account" so that it is align with your coding. . The error message is as per below when i try to change "Customer" to "Account" Error: Invalid Data. Review all error messages below to correct your data. can only specify reparentable master detail if field is master detail (Related field: Options) Thank you & best regards Philippe
ShashForceShashForce
You can Allow reparenting option in the master-detail relationship field definition in the setup.
PC MayBevPC MayBev
Hi Shashank, After recreated "Account" field to replace "Customer" and created "Price" currency field. I am able to created the trigger. Then i tried by creating a new Direct Order record, selected an account and a product, but the price field is not updated. What could be the problem? Appreciate your kind advice and assistance. Thank you Phil