You need to sign in to do that
Don't have an account?
Yashh
Modify this code on trigger need help
1>So the scenario is that when I created new product name As Chase Auto loans and saved and created another has the same name so its give an error that name already exists.
here is my code based on 1st point
trigger DuplicateProductName on Product2 (before insert,before update) {
List<Product2> accList=new List<Product2>([select Name from Product2]);
map<String,Product2> accmap=new map<String,Product2>();
for(Product2 acc:accList){
accmap.put(acc.Name,acc);
}
if(Trigger.isbefore&&(Trigger.isinsert|| Trigger.isupdate)){
for(Product2 acc:Trigger.new){
if(accmap.get(acc.Name)!=null){
acc.adderror('This Product name already exists');
}
}
}
}
2>but now i want to modify this code that if I create the same name but that duplicate name record checkbox is disabled then your record will be saved but the checkbox is active you cant save.
here is my code based on 1st point
trigger DuplicateProductName on Product2 (before insert,before update) {
List<Product2> accList=new List<Product2>([select Name from Product2]);
map<String,Product2> accmap=new map<String,Product2>();
for(Product2 acc:accList){
accmap.put(acc.Name,acc);
}
if(Trigger.isbefore&&(Trigger.isinsert|| Trigger.isupdate)){
for(Product2 acc:Trigger.new){
if(accmap.get(acc.Name)!=null){
acc.adderror('This Product name already exists');
}
}
}
}
2>but now i want to modify this code that if I create the same name but that duplicate name record checkbox is disabled then your record will be saved but the checkbox is active you cant save.
Please change your code with the below code:-
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
All Answers
By duplicate name record checkbox you mean Is Master Product?? if this is NO then you should be allowed to add product?
Please change your code with the below code:-
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
yes correct
hi Suraj Tripathi
i am geeting this error
please change map to map<string,boolean> = new map<string,boolean>();
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
can you give me a test class for this code because I don't have any idea about the test class?
trigger DuplicateProductName on Product2 (before insert, before update) {
for(Product2 prod : Trigger.new){
List<Product2> pr = [Select id, Name, CK_Product_Code__c,isActive from Product2 where id!=:prod.id and Name=:prod.Name And isActive=true];
if(pr.size() > 0){
for(Product2 product : pr){
prod.adderror('<br/>Product Name matches with: <a href="https://creditkarma--ckuat.my.salesforce.com/'+product.id+'"target="_blank">'+product.CK_Product_Code__c+'</a>',false);
}
}
}
}
Just insert product with these fields Name, CK_Product_Code__c,isActive from Product2.
your test class will work fine
Thanks & Regards
Suraj Tripathi