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
rahul soni 20rahul soni 20 

this one is duplicate trigger. can't catch the error..

// to get the duplicate recored (bulkified)

 trigger duplicateUpdateOnApexObj on APEX_TEST__c (before insert) {   
    Set<APEX_TEST__c> stuList = new Set<APEX_TEST__c>();               // set to store the single record of 1 kind
    for(APEX_TEST__c s : trigger.new){                                  
        stuList.add(s);                                                // add value to set
        
    }    
    
    List<APEX_TEST__c> stuUpdateList = [SELECT ID, Name__c from APEX_TEST__c WHERE Name__c in : stuList];
    
    Map<String, APEX_TEST__c> mapStudent = new Map<String, APEX_TEST__c>();
    for(APEX_TEST__c a : stuUpdateList){
        mapStudent.put(a.Name__c, a);
    }
    for(APEX_TEST__c f : trigger.new){
        if(mapStudent.get(f.APEX_TEST__c) != null){
            f.addError('D U P L I CA TE ');
        }
    }
        
}

17 Variable does not exist: APEX_TEST__c
10 Invalid bind expression type of APEX_TEST__c for column of type String
AnudeepAnudeep (Salesforce Developers) 
Hi Rahul,

At what line of the code are you seeing the error? I am guessing the below expresession is causing the issue. Can you try puting a debug statement to confirm? Thanks

if(mapStudent.get(f.APEX_TEST__c) != null){
}

Anudeep
SUCHARITA MONDALSUCHARITA MONDAL
Hi Rahul,
On Line 10 --> Error is there because you set contains entire object not only the name field.  Need to create set of name.
On Line 17--> Your map contains key as Name__c not as APEX_TEST__c 

Check with below code.. 
// to get the duplicate recored (bulkified)

 trigger duplicateUpdateOnApexObj on APEX_TEST__c (before insert) {   
    Set<APEX_TEST__c> stuList = new Set<APEX_TEST__c>();              
    for(APEX_TEST__c s : trigger.new){ 
        if(s.name!=null)
            stuList.add(s.name);     //creating set of name                                           
        
    }    
    if(stuList.size() >0)
        List<APEX_TEST__c> stuUpdateList = [SELECT ID, Name__c from APEX_TEST__c WHERE Name__c in : stuList];
    
    Map<String, APEX_TEST__c> mapStudent = new Map<String, APEX_TEST__c>();
    if(stuUpdateList.size()>0){
        for(APEX_TEST__c a : stuUpdateList){
            mapStudent.put(a.Name__c, a);
        }
    }
    for(APEX_TEST__c f : trigger.new){
        if(f.name__c == mapStudent.get(f.name__c).name__c){
            f.addError('D U P L I CA TE ');
        }
    }
        
}


Share your thoughts!

Thanks,
Sucharita