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
Akshay MhetreAkshay Mhetre 

test class Old Map New Map

After execution of test ,Its showing errors for these two fields--

  FAM3.Fi_Agent__c='FAMagent3';
  FAM3.PinCode__c='400 003';

Main Class::::

public class AgentMappingTriggerHandlerException  extends Exception {
public static void afterInsert(map<Id, FI_Agent_Mapping__c> oldMap, map<Id, FI_Agent_Mapping__c> newMap, List<FI_Agent_Mapping__c> newList){
    
        Map<String,String> AgentMappingMap = new Map<String,String>();
        Map<String,String> GroupMap = new Map<String,String>();
        Map<String,String> agentmappingUserMap = new Map<String,String>();
        List<String> AgencyList  = new List<String>();
        
        for(Sobject obj : newList){
            AgentMappingMap.put(String.valueOf(obj.get('Id')),String.valueOf(obj.get('Agency_Name__c')));
            AgencyList.add(String.valueOf(obj.get('Agency_Name__c')));
        }
    system.debug('AgentMappingMap==='+AgentMappingMap);
    
        
        String groupquery='select id,name from Group where name in: AgencyList';
        List<Sobject> targetobj=Database.query(groupquery);
        if(targetobj.size() > 0){
            for(Sobject obj : targetobj){
                GroupMap.put(String.valueOf(obj.get('Name')),String.valueOf(obj.get('Id')));   
                
            }
            system.debug('Group Map==='+GroupMap);
        }
    
     for(Sobject obj : newList){
            agentmappingUserMap.put(String.valueOf(obj.get('Id')),GroupMap.get(String.valueOf(AgentMappingMap.get(String.valueOf(obj.get('Id'))))));
     system.debug('agentmappingUserMap=='+agentmappingUserMap);   
     }
    
        SharingUtility.agentMappingShareRecord(agentmappingUserMap,'Edit', 'Manual');        
     }
    
    public static void beforeUpdate(map<Id, FI_Agent_Mapping__c> oldMap, map<Id, FI_Agent_Mapping__c> newMap, List<FI_Agent_Mapping__c> newList){
           
    }

    public static void afterUpdate(map<Id, FI_Agent_Mapping__c> olaMap, map<Id, FI_Agent_Mapping__c> newMap, List<FI_Agent_Mapping__c> newList){
    }
   
    public static void beforeInsert(List<FI_Agent_Mapping__c> AgentMappingList){
       
        List<String> useridList = new List<String>();
        List<Id> pincodeList = new List<Id>();
        for(FI_Agent_Mapping__c obj : AgentMappingList){
            useridList.add(String.valueOf(obj.get('Fi_Agent__c')));
            pincodeList.add(obj.PinCode__c);
        }
        String agentmappingquery='select id,Pincode_Map__c,Fi_Agent__c from FI_Agent_Mapping__c where PinCode__c in: pincodeList';
        List<FI_Agent_Mapping__c> targetobj=Database.query(agentmappingquery);
        if(targetobj.size() > 0){
            AgentMappingList.get(0).addError( 'User Has already Mapped to the same pincode');   
        }
    } 
}

Please Correct my Test class:::

@isTest
public class AgentMappingTriggerHandlerExceptionTest {
    
@isTest         
    public static void afterInsertTest(){
    
     FI_Agent_Mapping__c FAM1 = new FI_Agent_Mapping__c();
      FAM1.Name ='FAMname1';
      FAM1.Agency_Name__c='A R ASSOCIATES';
      FAM1.Fi_Agent__c='FAMagent1';
      FAM1.PinCode__c='400 001';
      insert FAM1;
      
      FI_Agent_Mapping__c FAM2 = new FI_Agent_Mapping__c();
      FAM2.Name ='FAMname2';
      FAM2.Agency_Name__c= 'COGENT';
      FAM2.Fi_Agent__c='FAMagent2';
      FAM2.PinCode__c='400 002';
      insert FAM2;
       
      
      Map<Id,FI_Agent_Mapping__c> afterInsertMap  =  New Map<Id,FI_Agent_Mapping__c>();
      FI_Agent_Mapping__c FAM3 = new FI_Agent_Mapping__c();
      FAM3.Name ='FAMname3';
      FAM3.Agency_Name__c= 'BEST SERVICES';
      FAM3.Fi_Agent__c='FAMagent3';
      FAM3.PinCode__c='400 003';
      insert FAM3;
      afterInsertMap.put(FAM3.Id,FAM3);
      
      List<FI_Agent_Mapping__c> newListA = new List<FI_Agent_Mapping__c>();
      FI_Agent_Mapping__c FAM4 = new FI_Agent_Mapping__c();
      FAM4.Name ='FAMname4';
      FAM4.Agency_Name__c='BANRAD FINSERV';
      FAM4.Fi_Agent__c='FAMagent4';
      FAM4.PinCode__c='400 004';
      insert FAM4;
      newListA.add(FAM4);
        
       Test.startTest();
       AgentMappingTriggerHandlerException.afterInsert(afterInsertMap,afterInsertMap,newListA ); 
       Test.stopTest();
      
    }
@isTest
    public static void beforeUpdateTest(){
        
    }
@isTest
    public static void afterUpdateTest(){
        
    }
@isTest
    public static void beforeInsertTest(){
    
    }
}

 

Best Answer chosen by Akshay Mhetre
Suraj Tripathi 47Suraj Tripathi 47

Hi Akshay,

Maybe Try this Test class. Maybe it will work properly as excepted.

@isTest
public class AgentMappingTriggerHandlerExceptionTest {
    
  @isTest         
    public static void afterInsertTest(){
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
         
        User usr = new User(LastName = 'LIVESTON',
                           FirstName='JASON',
                           Alias = 'jliv',
                           Email = 'jason.liveston@asdf.com',
                           Username = 'jason.liveston@asdf.com',
                           ProfileId = profileId.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                          LocaleSidKey = 'en_US'
                           );
        
       Group gr= new Group();
        gr.Name='BANRAD FINSERV';
        insert gr;
        
        PinCode__c pin= new Pincode__c();
        pin.PinCode__c=200201;
        insert pin;
        
        Map<Id,FI_Agent_Mapping__c> afterInsertMap  =  New Map<Id,FI_Agent_Mapping__c>();
        
        List<FI_Agent_Mapping__c> newListA = new List<FI_Agent_Mapping__c>();
        FI_Agent_Mapping__c FAM4 = new FI_Agent_Mapping__c();
        FAM4.Name ='FAMname4';
        FAM4.Agency_Name__c='BANRAD FINSERV';
        FAM4.Fi_Agent__c=usr.id;
        FAM4.PinCode__c=pin.Id;
        insert FAM4;
        newListA.add(FAM4);
        afterInsertMap.put(FAM4.Id,FAM4);
        
        Test.startTest();
        AgentMappingTriggerHandlerException.afterInsert(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.beforeUpdate(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.afterUpdate(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.beforeInsert(newListA);
        Test.stopTest();
        
    }
}

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Akshay,

This class with 100% coverage and take reference from this below code.
@isTest
public class AgentMappingTriggerHandlerExceptionTest {
    
    @isTest         
    public static void afterInsertTest(){
       Group gr= new Group();
        gr.Name='BANRAD FINSERV';
        insert gr;
        
        Map<Id,FI_Agent_Mapping__c> afterInsertMap  =  New Map<Id,FI_Agent_Mapping__c>();
        FI_Agent_Mapping__c FAM3 = new FI_Agent_Mapping__c();
        FAM3.Name ='FAMname3';
        FAM3.Agency_Name__c= 'BEST SERVICES';
        FAM3.Fi_Agent__c='FAMagent3';
        FAM3.PinCode__c=gr.Id;
        insert FAM3;
        afterInsertMap.put(FAM3.Id,FAM3);
        
        List<FI_Agent_Mapping__c> newListA = new List<FI_Agent_Mapping__c>();
        FI_Agent_Mapping__c FAM4 = new FI_Agent_Mapping__c();
        FAM4.Name ='FAMname4';
        FAM4.Agency_Name__c='BANRAD FINSERV';
        FAM4.Fi_Agent__c='FAMagent4';
        FAM4.PinCode__c=gr.Id;
        insert FAM4;
        newListA.add(FAM4);
        
        Test.startTest();
        AgentMappingTriggerHandlerException.afterInsert(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.beforeUpdate(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.afterUpdate(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.beforeInsert(newListA);
        Test.stopTest();
        
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
Akshay MhetreAkshay Mhetre
Hello Suraj...I forgot to tell you that few of the fields are lookup...Do you need those "fields details" to create data fo testing.? Will code change ??
Akshay MhetreAkshay Mhetre

User-added image

Right now above solution is showing error on 

FAM3.Fi_Agent__c='FAMagent3';    and     insert FAM3;

Fields Details::

1) PinCode__c Lookup(PinCode)

2) Fi_Agent__c Lookup(User)

Suraj Tripathi 47Suraj Tripathi 47

Hi Akshay,

Are you getting any error by using that test class which I have shared to you.If yes, then please share that error .

Thanks

Akshay MhetreAkshay Mhetre

Hello Suraj,

Above I have shared the field details and error snippet.

Again I am sharing my issue details after using your solution.

1)After RunTest facing error on FAM3.Fi_Agent__c='FAMagent3';  (Line No.14--May be because of Lookup(User) ).

So I comment that line-14 and 23.

Then I faced this error.(Snippet)

User-added image

Suraj Tripathi 47Suraj Tripathi 47
Okay Akshay,

Please tell me PinCode__c field is Lookup with Which object?
and tell me Object Api name.
Akshay MhetreAkshay Mhetre
PinCode__c field is Lookup with PinCode__c (Custom Object.). In this custom object field details are-- PinCode PinCode__c Number(6, 0) (External ID)
Suraj Tripathi 47Suraj Tripathi 47
Hi Akshay.

Try this test class:-
@isTest
public class AgentMappingTriggerHandlerExceptionTest {
    
  @isTest         
    public static void afterInsertTest(){
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
         
        User usr = new User(LastName = 'LIVESTON',
                           FirstName='JASON',
                           Alias = 'jliv',
                           Email = 'jason.liveston@asdf.com',
                           Username = 'jason.liveston@asdf.com',
                           ProfileId = profileId.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                          LocaleSidKey = 'en_US'
                           );
        
       Group gr= new Group();
        gr.Name='BANRAD FINSERV';
        insert gr;
        
        PinCode__c pin= new Pincode__c();
        pin.PinCode__c=200201;
        insert pin;
        
        Map<Id,FI_Agent_Mapping__c> afterInsertMap  =  New Map<Id,FI_Agent_Mapping__c>();
        FI_Agent_Mapping__c FAM3 = new FI_Agent_Mapping__c();
        FAM3.Name ='FAMname3';
        FAM3.Agency_Name__c= 'BEST SERVICES';
        FAM3.Fi_Agent__c=usr.id;
        FAM3.PinCode__c=pin.id;
        insert FAM3;
        afterInsertMap.put(FAM3.Id,FAM3);
        
        List<FI_Agent_Mapping__c> newListA = new List<FI_Agent_Mapping__c>();
        FI_Agent_Mapping__c FAM4 = new FI_Agent_Mapping__c();
        FAM4.Name ='FAMname4';
        FAM4.Agency_Name__c='BANRAD FINSERV';
        FAM4.Fi_Agent__c=usr.id;
        FAM4.PinCode__c=pin.Id;
        insert FAM4;
        newListA.add(FAM4);
        
        Test.startTest();
        AgentMappingTriggerHandlerException.afterInsert(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.beforeUpdate(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.afterUpdate(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.beforeInsert(newListA);
        Test.stopTest();
        
    }
}

 
Akshay MhetreAkshay Mhetre

Hey Suraj...Thanks for you quick solution.Still showing one error. and from your solutions I am trying to learn also.
Error:: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, User Has already Mapped to the same pincode: []

Stack Trace:: Class.AgentMappingTriggerHandlerExceptionTest.afterInsertTest: line 43, column 1

Suraj Tripathi 47Suraj Tripathi 47
Hi Akshay, 
Check your apex code you have put the custom validation rule That's why you are getting error.
User-added image
 AgentMappingList.get(0).addError( 'User Has already Mapped to the same pincode')----- The error is getting for this line
Thanks
Akshay MhetreAkshay Mhetre

Hey Suraj,

Tell me the ways how to overcome on this issue if you have.Thanks for help. :)

Suraj Tripathi 47Suraj Tripathi 47

Hi Akshay,

Maybe Try this Test class. Maybe it will work properly as excepted.

@isTest
public class AgentMappingTriggerHandlerExceptionTest {
    
  @isTest         
    public static void afterInsertTest(){
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
         
        User usr = new User(LastName = 'LIVESTON',
                           FirstName='JASON',
                           Alias = 'jliv',
                           Email = 'jason.liveston@asdf.com',
                           Username = 'jason.liveston@asdf.com',
                           ProfileId = profileId.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                          LocaleSidKey = 'en_US'
                           );
        
       Group gr= new Group();
        gr.Name='BANRAD FINSERV';
        insert gr;
        
        PinCode__c pin= new Pincode__c();
        pin.PinCode__c=200201;
        insert pin;
        
        Map<Id,FI_Agent_Mapping__c> afterInsertMap  =  New Map<Id,FI_Agent_Mapping__c>();
        
        List<FI_Agent_Mapping__c> newListA = new List<FI_Agent_Mapping__c>();
        FI_Agent_Mapping__c FAM4 = new FI_Agent_Mapping__c();
        FAM4.Name ='FAMname4';
        FAM4.Agency_Name__c='BANRAD FINSERV';
        FAM4.Fi_Agent__c=usr.id;
        FAM4.PinCode__c=pin.Id;
        insert FAM4;
        newListA.add(FAM4);
        afterInsertMap.put(FAM4.Id,FAM4);
        
        Test.startTest();
        AgentMappingTriggerHandlerException.afterInsert(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.beforeUpdate(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.afterUpdate(afterInsertMap,afterInsertMap,newListA ); 
        AgentMappingTriggerHandlerException.beforeInsert(newListA);
        Test.stopTest();
        
    }
}
This was selected as the best answer
Akshay MhetreAkshay Mhetre

Hey Suraj....Great....You're great....100% Done.

Well I am not a coder who used to code on 1000+ line ..but I am learning,trying. There is another class I got to test.Having low coverage.

https://developer.salesforce.com/forums/ForumsMain?id=9062I000000DJXNQA4