You need to sign in to do that
Don't have an account?
Sid Lightning
Need help in writting Test class for TriggerHandler Class on Products
The trigger automatically attaches pricebooks, when a produt get created. which pricebook it should attach depends upon the LOB
public class ProductTriggerHandler extends TriggerHandler{
public ProductTriggerHandler() {
}
public override void afterUpdate() {
System.debug('this is after update');
}
public override void beforeUpdate() {
}
public override void beforeInsert() {
}
public override void afterInsert() {
System.debug('this is after insert');
createPricebookEntries((List<Product2>)Trigger.new);
}
public void createPricebookEntries(List<Product2> productList){
List<PricebookEntry> peList = new List<PricebookEntry>();
List<PricebookEntry> mpeList = new List<PricebookEntry>(); //For Modular Kitchen
List<PricebookEntry> opeList = new List<PricebookEntry>(); // For All other LOB
List<Pricebook2> pricebookIds = [Select id,Site_Code__c from Pricebook2 where Pricebook_Category__c ='FR']; // Fetches only Furniture Pricebook
List<Pricebook2> MKpricebookIds = [Select id,Site_Code__c from Pricebook2 where Pricebook_Category__c ='MK' OR IsStandard = TRUE]; // Fethches only MK Pricebook and Standard Pricebook
List<Pricebook2> opricebookIds = [Select id,Site_Code__c from Pricebook2 where IsStandard = TRUE]; // Fetches only Standard Pricebook
// Attaching Standard Pricebook and Furniture Pricebooks when LOB is Furniture, Home Improvement , Home Improvements
if(pricebookIds.size() > 0){
for(Product2 prod: productList){
System.debug('prod.LOB__c:'+prod.LOB__c);
if(prod.LOB__c!=null && Label.FR_Pricebook.contains(prod.LOB__c)){
for(Pricebook2 pb2 : pricebookIds){
PricebookEntry pe = new PricebookEntry();
pe.UnitPrice = 1;
pe.Pricebook2Id = pb2.id;
pe.Product2Id = prod.id;
peList.add(pe);
}
}
}
if(peList.size()>0){
insert peList;
System.debug('id:'+peList[0].id);
}
}
// Attaches Standard pricebook to all products whose LOB is not Furniture, Modular Kitchen, Mk , Home Improvement,Home Improvements
if(opricebookIds.size() > 0){
for(Product2 prod: productList){
System.debug('prod.LOB__c:'+prod.LOB__c);
if(prod.LOB__c!=null && Label.Other_Pricebooks.contains(prod.LOB__c)){
for(Pricebook2 opb2 : opricebookIds){
PricebookEntry ope = new PricebookEntry();
ope.UnitPrice = 1;
ope.Pricebook2Id = opb2.id;
ope.Product2Id = prod.id;
opeList.add(ope);
}
}
}
if(opeList.size()>0){
insert opeList;
System.debug('id:'+opeList[0].id);
}
}
// Attaching Standard Pricebook and Modular Kitchen Pricebook when LOB is Modular Kitchen or MK
if(MKpricebookIds.size() > 0){
for(Product2 prod: productList){
System.debug('prod.LOB__c:'+prod.LOB__c);
if(prod.LOB__c == 'Modular Kitchen' || prod.LOB__c == 'MK'){
for(Pricebook2 mpb2 : mkpricebookIds){
PricebookEntry mpe = new PricebookEntry();
mpe.UnitPrice = 1;
mpe.Pricebook2Id = mpb2.id;
mpe.Product2Id = prod.id;
mpeList.add(mpe);
}
}
}
if(mpeList.size()>0){
insert mpeList;
System.debug('id:'+mpeList[0].id);
}
}
}
}
public class ProductTriggerHandler extends TriggerHandler{
public ProductTriggerHandler() {
}
public override void afterUpdate() {
System.debug('this is after update');
}
public override void beforeUpdate() {
}
public override void beforeInsert() {
}
public override void afterInsert() {
System.debug('this is after insert');
createPricebookEntries((List<Product2>)Trigger.new);
}
public void createPricebookEntries(List<Product2> productList){
List<PricebookEntry> peList = new List<PricebookEntry>();
List<PricebookEntry> mpeList = new List<PricebookEntry>(); //For Modular Kitchen
List<PricebookEntry> opeList = new List<PricebookEntry>(); // For All other LOB
List<Pricebook2> pricebookIds = [Select id,Site_Code__c from Pricebook2 where Pricebook_Category__c ='FR']; // Fetches only Furniture Pricebook
List<Pricebook2> MKpricebookIds = [Select id,Site_Code__c from Pricebook2 where Pricebook_Category__c ='MK' OR IsStandard = TRUE]; // Fethches only MK Pricebook and Standard Pricebook
List<Pricebook2> opricebookIds = [Select id,Site_Code__c from Pricebook2 where IsStandard = TRUE]; // Fetches only Standard Pricebook
// Attaching Standard Pricebook and Furniture Pricebooks when LOB is Furniture, Home Improvement , Home Improvements
if(pricebookIds.size() > 0){
for(Product2 prod: productList){
System.debug('prod.LOB__c:'+prod.LOB__c);
if(prod.LOB__c!=null && Label.FR_Pricebook.contains(prod.LOB__c)){
for(Pricebook2 pb2 : pricebookIds){
PricebookEntry pe = new PricebookEntry();
pe.UnitPrice = 1;
pe.Pricebook2Id = pb2.id;
pe.Product2Id = prod.id;
peList.add(pe);
}
}
}
if(peList.size()>0){
insert peList;
System.debug('id:'+peList[0].id);
}
}
// Attaches Standard pricebook to all products whose LOB is not Furniture, Modular Kitchen, Mk , Home Improvement,Home Improvements
if(opricebookIds.size() > 0){
for(Product2 prod: productList){
System.debug('prod.LOB__c:'+prod.LOB__c);
if(prod.LOB__c!=null && Label.Other_Pricebooks.contains(prod.LOB__c)){
for(Pricebook2 opb2 : opricebookIds){
PricebookEntry ope = new PricebookEntry();
ope.UnitPrice = 1;
ope.Pricebook2Id = opb2.id;
ope.Product2Id = prod.id;
opeList.add(ope);
}
}
}
if(opeList.size()>0){
insert opeList;
System.debug('id:'+opeList[0].id);
}
}
// Attaching Standard Pricebook and Modular Kitchen Pricebook when LOB is Modular Kitchen or MK
if(MKpricebookIds.size() > 0){
for(Product2 prod: productList){
System.debug('prod.LOB__c:'+prod.LOB__c);
if(prod.LOB__c == 'Modular Kitchen' || prod.LOB__c == 'MK'){
for(Pricebook2 mpb2 : mkpricebookIds){
PricebookEntry mpe = new PricebookEntry();
mpe.UnitPrice = 1;
mpe.Pricebook2Id = mpb2.id;
mpe.Product2Id = prod.id;
mpeList.add(mpe);
}
}
}
if(mpeList.size()>0){
insert mpeList;
System.debug('id:'+mpeList[0].id);
}
}
}
}
Sid Lightning
Can anyone help me with this ?
elen maoel
Is this good idea to use the script in dynamic website? I'm looking for my condo kitchen compnay (https://impressionskitchens.com/condo-kitchen-renovation-company/) project.