You need to sign in to do that
Don't have an account?
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(){
}
}
Hi Akshay,
Maybe Try this Test class. Maybe it will work properly as excepted.
All Answers
This class with 100% coverage and take reference from this below code.
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.
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)
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
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)
Please tell me PinCode__c field is Lookup with Which object?
and tell me Object Api name.
Try this test class:-
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
Check your apex code you have put the custom validation rule That's why you are getting error.
AgentMappingList.get(0).addError( 'User Has already Mapped to the same pincode')----- The error is getting for this line
Thanks
Hey Suraj,
Tell me the ways how to overcome on this issue if you have.Thanks for help. :)
Hi Akshay,
Maybe Try this Test class. Maybe it will work properly as excepted.
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