You need to sign in to do that
Don't have an account?
ashu 6112
Need your help guys
Hi, There are 2 custom objects, Abc and Def. There is a field on Def object, named number. The values in Number field must be unique(Not duplicate) for each the Abc object. I wrote the below code and it is working fine, now I need to write the test class for this trigger. Below is the trigger code:
trigger duplicatenumberWithSameAbc on Def__c (before insert, before update)
{
if(Trigger.isBefore && Trigger.isInsert)
{
duplicatenumberWithSameAbc dup=new duplicatenumberWithSameAbc();
dup.insertDef(trigger.new);
}
if(Trigger.isBefore && Trigger.isUpdate)
{
for(Def__c tb: Trigger.new)
{
if(Trigger.oldMap.get(tb.id).number__c != Trigger.newMap.get(tb.Id).number__c)
{
duplicatenumberWithSameAbc dup=new duplicatenumberWithSameAbc();
dup.insertDef(trigger.new);
}
}
}
}
Apex class for this trigger:
public class duplicatenumberWithSameAbc
{
public void insertDef(List<Def__c> tab1)
{
set<Id> stTemp=new set<Id>();
for(Def__c t:tab1)
{
stTemp.add(t.Abc__c);
}
Map<id,List<Def__c>> mpDb=new Map<id,List<Def__c>>();
for(Def__c t : [select id,name,Abc__c,number__c from Def__c where Abc__c IN :stTemp])
{
List<Def__c> tab = new List<Def__c>();
if(mpDb.get(t.Abc__c) != null)
{
tab = mpDb.get(t.Abc__c);
tab.add(t);
mpDb.put(t.Abc__c , tab);
}
else{
List<Def__c> newList = new List<Def__c>();
newList.add(t);
mpDb.put(t.Abc__c , newList);
}
}
for(Def__c t:tab1)
{
if(mpDb.containskey(t.Abc__c))
{
List<Def__c> tabs = mpDb.get(t.Abc__c);
for(Def__c t1 : tabs)
{
if(t.number__c == t1.number__c )
{
t.addError('You cannot add duplicate Value in Number Field');
}
}
}
}
}
}
trigger duplicatenumberWithSameAbc on Def__c (before insert, before update)
{
if(Trigger.isBefore && Trigger.isInsert)
{
duplicatenumberWithSameAbc dup=new duplicatenumberWithSameAbc();
dup.insertDef(trigger.new);
}
if(Trigger.isBefore && Trigger.isUpdate)
{
for(Def__c tb: Trigger.new)
{
if(Trigger.oldMap.get(tb.id).number__c != Trigger.newMap.get(tb.Id).number__c)
{
duplicatenumberWithSameAbc dup=new duplicatenumberWithSameAbc();
dup.insertDef(trigger.new);
}
}
}
}
Apex class for this trigger:
public class duplicatenumberWithSameAbc
{
public void insertDef(List<Def__c> tab1)
{
set<Id> stTemp=new set<Id>();
for(Def__c t:tab1)
{
stTemp.add(t.Abc__c);
}
Map<id,List<Def__c>> mpDb=new Map<id,List<Def__c>>();
for(Def__c t : [select id,name,Abc__c,number__c from Def__c where Abc__c IN :stTemp])
{
List<Def__c> tab = new List<Def__c>();
if(mpDb.get(t.Abc__c) != null)
{
tab = mpDb.get(t.Abc__c);
tab.add(t);
mpDb.put(t.Abc__c , tab);
}
else{
List<Def__c> newList = new List<Def__c>();
newList.add(t);
mpDb.put(t.Abc__c , newList);
}
}
for(Def__c t:tab1)
{
if(mpDb.containskey(t.Abc__c))
{
List<Def__c> tabs = mpDb.get(t.Abc__c);
for(Def__c t1 : tabs)
{
if(t.number__c == t1.number__c )
{
t.addError('You cannot add duplicate Value in Number Field');
}
}
}
}
}
}
1) https://trailhead.salesforce.com/en/apex_testing/apex_testing_data
2) https://developer.salesforce.com/forums/?id=906F000000090j3IAA
3) https://github.com/dhoechst/Salesforce-Test-Factory
Let us know if this will help you
All Answers
Thanks for this, but I have to create first factory data class and then use it in my test class. Thats the actualt requiremtn. Please help me in this as I dont hav e any idea of factory data class.
1) https://trailhead.salesforce.com/en/apex_testing/apex_testing_data
2) https://developer.salesforce.com/forums/?id=906F000000090j3IAA
3) https://github.com/dhoechst/Salesforce-Test-Factory
Let us know if this will help you