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
YashhYashh 

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.User-added image
Best Answer chosen by Yashh
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please change your code with the below code:-
trigger DuplicateProductName on Product2 (before insert,before update) {
       List<Product2> accList=new List<Product2>([select Name,check_box from Product2]);
    map<String,boolean> accmap=new map<String,Product2>();
    for(Product2 acc:accList){
        accmap.put(acc.Name,acc.check_box);
    }
 
    if(Trigger.isbefore&&(Trigger.isinsert|| Trigger.isupdate)){
        for(Product2 acc:Trigger.new){
            if(accmap.get(acc.Name) == true){
                acc.adderror('This Product name already exists');
            }
        }
    }

}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

All Answers

AbhinavAbhinav (Salesforce Developers) 

By duplicate name  record checkbox you mean Is Master Product??   if this is NO then you should be allowed to add product?

 
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please change your code with the below code:-
trigger DuplicateProductName on Product2 (before insert,before update) {
       List<Product2> accList=new List<Product2>([select Name,check_box from Product2]);
    map<String,boolean> accmap=new map<String,Product2>();
    for(Product2 acc:accList){
        accmap.put(acc.Name,acc.check_box);
    }
 
    if(Trigger.isbefore&&(Trigger.isinsert|| Trigger.isupdate)){
        for(Product2 acc:Trigger.new){
            if(accmap.get(acc.Name) == true){
                acc.adderror('This Product name already exists');
            }
        }
    }

}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
This was selected as the best answer
YashhYashh
Hi Abhinav
yes correct
 
YashhYashh
hi Suraj Tripathi i am geeting this error
​​​​​​​​​​​​​​hi Suraj Tripathi
i am geeting this error
Suraj Tripathi 47Suraj Tripathi 47
Hi,
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
YashhYashh
Hi 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);
            }
        }
    }
}
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Just insert product with these fields  Name, CK_Product_Code__c,isActive from Product2.
your test class will work fine
@istest 
public class DuplicateProductNameTest
{  @istest static void disp(){ 
   Product2 p = new Product2(); 
     p.name='test'; 
      p.CK_Product_Code__c = 'required value'; 
     p.isactive =true;  
    insert p; 
}
 }


Thanks & Regards
Suraj Tripathi
YashhYashh
Thanks, Suraj Tripathi