+ Start a Discussion
raji devi 1raji devi 1 

trigger on  product add specifice product to specific price book only

Hi,
on product object we have currency__c picklist  (INR, USA, etc)and pricebooke also  have same currency__c(India, USA etc) picklist of same currencies. i created pircebooks  depends on picklist values. I am unable to map the related product to related pirce books records.
exm: if product is INR , it go and store to Indian pricebook( For this i am using ID mapping )
Here i am posting my code, Give me solutions for this, Thanks in advance


trigger Createdefaultpricebook on Product2 (after insert) {

 list<pricebookentry> z = new list<pricebookentry>();
    list<product2> produ = new list<product2>();
    
    sobject priceb = [select id, name from pricebook2 where isstandard = true];
    for(product2 p : trigger.new){
    pricebookentry pbe =new pricebookEntry();
    pbe.product2id= p.id;
    pbe.pricebook2id = priceb.id;
    pbe.unitprice = 0.00;
    pbe.isactive = true;
    pbe.UseStandardPrice=false;    
    z.add(pbe);
    }    
    
    insert z;
    
    //id mapping of currencies
    map<id, pricebook2> pbmap = new map<id, pricebook2>();
    list<pricebook2 > pbl = [select id, name from pricebook2 where CurrencyType__c !=null];
   for(pricebook2 pb : pbl){
    
    pbmap.put(pb.id, pb);
   }
   system.debug('***********'+ pbmap);
    
    id  indiaidpb = '01s28000007GbZ9AAK';
    id  usaidpb = '01s28000007Gby5AAC';
    id  euroidpb = '01s28000007GbyFAAS'; 
    id  yenumpb = '01s28000007GbyKAAS';
    id  englandidpb = '01s28000007GbyPAAS';
    
    
    list<pricebookentry> lispbe = new list<pricebookentry >();
    for(product2 p : trigger.new){  
   if(p.CurrencyType__c == 'INR'){
        pricebookentry custompric = new pricebookentry();   
        custompric.pricebook2id = '01s28000007GbZ9AAK';
        custompric.product2id = p.id;
        custompric.unitprice = 0.00;
        custompric.isactive = true;
        lispbe.add(custompric);
  
    }
    
    else if(p.CurrencyType__c == 'USD'){
    
        pricebookentry custompric = new pricebookentry();   
        custompric.pricebook2id = '01s28000007Gby5AAC';
        custompric.product2id = p.id;
        custompric.unitprice = 0.00;
        custompric.isactive = true;
        lispbe.add(custompric);
    
    }
    
         else if(p.CurrencyType__c == 'ERO'){
    
        pricebookentry custompric = new pricebookentry();   
        custompric.pricebook2id = '01s28000007GbyFAAS';
        custompric.product2id = p.id;
        custompric.unitprice = 0.00;
        custompric.isactive = true;
        lispbe.add(custompric);
    
    }
    
         else if(p.CurrencyType__c == 'YEN'){
    
        pricebookentry custompric = new pricebookentry();   
        custompric.pricebook2id = '01s28000007GbyKAAS';
        custompric.product2id = p.id;
        custompric.unitprice = 0.00;
        custompric.isactive = true;
        lispbe.add(custompric);
    
    }
    
         else if(p.CurrencyType__c == 'POUND'){
    
        pricebookentry custompric = new pricebookentry();   
        custompric.pricebook2id = '01s28000007GbyPAAS';
        custompric.product2id = p.id;
        custompric.unitprice = 0.00;
        custompric.isactive = true;
        lispbe.add(custompric);
    
    }
    
    }
    
    insert lispbe;
    

}

 
grodigrodi
Hi raji,
can you pls describe in detail was is not working?! Furthermore are 'POUND', 'YEN', 'INR' etc the picklist values or just the translated labels?!
--dirk
Richard Jimenez 9Richard Jimenez 9
Hi raji,

Before you continue with your code, here are some things to consider:
  • Are you using the Salesforce Multi-Currencies feature - if not, why? If you use the multi-currency feature, you can add products to one pricebook in multiple currencies with different prices.
  • How do you set prices - E.g. are they set by Country or Currency?
  • Can a product be sold in multiple currencies?
Thanks,
Richard.

 
Richard Jimenez 9Richard Jimenez 9
Hi raji,

You can store the same picklist values on the product AND the pricebook to use as your map (if it is 1-to-1).

However, my advice would be, if I was doing this, I would make sure I fully understand the business pricing model and best practises around pricebooks and multi-currency management.

Thanks,
Richard.
raji devi 1raji devi 1
Hi Richard,

We  are enable the multi- currency in my organization, we are  using pick list for different currencies like 
INR - Indian Rupee, USD - USA Dollar ,HKD - Hong  kong Dollar, But we are not getting conversion rates? How to assign conversion rates for different currencies? Conversion rate happen automatically (or ) we have to set the conversion rates?

Thanks
Raji