You need to sign in to do that
Don't have an account?
john2
Help needed to write test class for trigger
Hi,
i am a beginner i managed to create the below trigger to update the primary address after address update. .
can any one help me writing a test class for it.
thanks in advance for your help.
trigger updatePrimaryAddress on Address__c (after insert, after update) {
List <String> ContactIds= new List<String>();
For(Address__c ad : trigger.new)
{
ContactIds.add(ad.Contact__c);
}
List<contact>contacts = [Select Id, Name, Primary_address__c From contact Where id IN:contactIds];
Map<String,contact>contactMap = New Map<String,contact>();
For(contact c : contacts)
{
contactMap.Put(c.id, c);
}
List<contact> ctUpdates = New List<contact>();
For(address__c ad : Trigger.new)
{
system.debug('Entered for loop');
if(ad.Is_Primary_address__c==TRUE)
{
contact ct = contactMap.get(ad.contact__c);
system.debug(ct.Id);
ct.Primary_address__c=ct.id;
ctUpdates.add(ct);
}else
{
contact ct = contactMap.get(ad.contact__c);
system.debug(ct.Id);
ct.Primary_address__c=null;
ctUpdates.add(ct);
}
}
update ctUpdates;
}
thanks for reply with solution.. i tried this code in my sandbox, i am getting an error saying that
Error: Compile Error: Illegal assignment from String to Boolean at line 8 column 9
@isTest
private class TestupdatePrimaryAddress
{
static testMethod void testCase1()
{
Address__c adobj = new Address__c();
adobj.Name = 'address1';
adobj.Is_Primary_address__c='TRUE';
insert adobj;
Test.startTest();
contact cObj = contactInsert();
cObj.Primary_address__c=adobj.ID;
update cObj;
Test.stopTest();
}
public static contact contactInsert()
{
//create an contact
contact cObj = new contact();
cObj.LastName = 'test';
cObj.FIrstName = 'test';
cObj.Email = 'test@test.com'
cObj.phone = '1234567890';
insert cObj;
return cObj;
}
}
once again thanks,
All Answers
Hi,
See below test case... hopes this works for you...........
@isTest
private class TestupdatePrimaryAddress
{
static testMethod void testCase1()
{
Address__c adobj = new Address__c();
adobj.Name = 'address1';
adobj.Is_Primary_address__c='TRUE';
insert adobj;
Test.startTest();
contact cObj = contactInsert();
cObj.Primary_address__c=adobj.ID;
update cObj;
Test.stopTest();
}
public static contact contactInsert()
{
//create an contact
contact cObj = new contact();
cObj.LastName = 'test';
cObj.FIrstName = 'test';
cObj.Email = 'test@test.com'
cObj.phone = '1234567890';
insert cObj;
return cObj;
}
}
thanks for reply with solution.. i tried this code in my sandbox, i am getting an error saying that
Error: Compile Error: Illegal assignment from String to Boolean at line 8 column 9
@isTest
private class TestupdatePrimaryAddress
{
static testMethod void testCase1()
{
Address__c adobj = new Address__c();
adobj.Name = 'address1';
adobj.Is_Primary_address__c='TRUE';
insert adobj;
Test.startTest();
contact cObj = contactInsert();
cObj.Primary_address__c=adobj.ID;
update cObj;
Test.stopTest();
}
public static contact contactInsert()
{
//create an contact
contact cObj = new contact();
cObj.LastName = 'test';
cObj.FIrstName = 'test';
cObj.Email = 'test@test.com'
cObj.phone = '1234567890';
insert cObj;
return cObj;
}
}
once again thanks,
To write a test method for trigger just need to insert record or update record and your trigger will be executed( tested).
Find below a sample test method :
static testMethod void myTestMethod()
{
Contact con= new Contact(lastName='testName',email='abc@gmail.com');
insert con;
Address__c ad1=new Address__c(contact__c=con.id,is_Primary_address__c=true); // positive case
insert ad1;
Address__c ad2=new Address__c(contact__c=con.id,is_Primary_address__c=false); // Negative case
insert ad2;
// do the same for update record
}
Hi,
i used this code for writing test class for trigger.
i got saved and it covered 85%.
when i am trying to deploy this is code other Developer sanbox to TESTING sandbox, i am getting an error saying that "INVALID CROSS REFERENCE KEY"
I use the sfdc id in the test class, i marked that code where i am getting the error with red color.
can anyone help me to solve this issue.
thanks in advance
@isTest
private class TestupdatePrimaryAddress
{
static testMethod void testCase1()
{
Address__c adobj = new Address__c();
adobj.Name = 'address1';
adobj.Is_Primary_address__c='TRUE';
insert adobj;
Test.startTest();
contact cObj = contactInsert();
cObj.Primary_address__c=adobj.ID;
update cObj;
Test.stopTest();
}
public static contact contactInsert()
{
//create an contact
contact cObj = new contact();
cObj.LastName = 'test';
cObj.FIrstName = 'test';
cObj.Email = 'test@test.com'
cObj.phone = '1234567890';
insert cObj;
return cObj;
}
}
Hi, John,
can u please check Your Object's Fields are proper created ...
thanks for the reply,
i have completed that task.
i have inserted the contact before the address is updated and put con.id in address object instead of sfdc id..
thanks