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
steve456steve456 

Test Class for After Update Trigger

Here is the Trigger

*****************************************

trigger AfterUpdateAssociatePeople on Associate__c (after update) {
Set<Id> assocIdSet = new Set<Id>();
Map<Id, Associate__c> assocMap = new Map<Id, Associate__c>();

List<Associate__c> assocList = [Select Id, Name, Current_Address__c, Current_City__c,Current_State__c,Current_Zip__c,from Associate__c where ID IN: assocIdSet];
for(Associate__c assoc: Trigger.new) {
   assocIdSet.add(assoc.Id);

    assocMap.put(assoc.Id, assoc);


}
List<People__c> peopleList =[Select Address_1__c,CID__c,Associate__c,City__c,State__c,Zip__c FROM People__c WHERE Associate__c IN: assocIdSet];
if(peopleList != null && peopleList.size() > 0) {
for(People__c people: peopleList) {
Associate__c assoc = assocMap.get(people.Associate__c);
people.Address_1__c = assoc.Current_Address__c;
people.City__c = assoc.Current_City__c;

people.State__c=assoc.Current_State__c;
people.Zip__c=assoc.Current_Zip__c;
people.CID__c=assoc.Associate_ID__c;
}
}

update peopleList;
}

 

I wrote a Test Class and when i run the test it s not showing my trigger or its code coverage

 

****************************************************************************************

 

@isTest
private class AfterUpdateAssociatePeopleTest{
public static testMethod void AfterUpdateAssociatePeopleStaticTestMethod()
{

Associate__c aa =new Associate__c(Name='TestAss' ,Associate_ID__c='ASSID' ,First_Name__c='AssFName', Current_Address__c='TestAddress',Current_City__c='TestCity',Current_Zip__c='45678',Current_State__c='TX');
insert aa;

List<People__c> peopleList = new List<People__c>{new people__c(
Name='TestUser',Associate__c=aa.id,CID__c='ASSID',Address_1__c='3445street',City__c='',State__c='XX',Zip__c='75423', )};

insert peopleList;

List<People__c> peopleList1 = new List<People__c>{ [select id from People__c where id in :peopleList]};
for(People__c ppdd:peopleList1)
ppdd.Address_1__c = aa.Current_Address__c;


update peopleList1;
}

}

 

 

 

******************************************

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MattLacey.ax1065MattLacey.ax1065

Your trigger is on Associate__c but your test method only performs an update operation on People__c. Perform an update using your variable aa at the end:

 

update aa;

 

And you should be sorted! 

 

Tip: People isn't a particularly good name for the object you've defined (judging by the fields), it should really be called Person with People as the plural label, and taking that one stage further, it sounds like you just use Contact instead of a custom object.

All Answers

MattLacey.ax1065MattLacey.ax1065

Your trigger is on Associate__c but your test method only performs an update operation on People__c. Perform an update using your variable aa at the end:

 

update aa;

 

And you should be sorted! 

 

Tip: People isn't a particularly good name for the object you've defined (judging by the fields), it should really be called Person with People as the plural label, and taking that one stage further, it sounds like you just use Contact instead of a custom object.

This was selected as the best answer
steve456steve456

due to security issues i have modfied the names.....could you just say me where should i write

 

update aa

steve456steve456

man o man that worked

MattLacey.ax1065MattLacey.ax1065

It worries me that you need to ask that, but you want to do something like this:

 

@isTest
private class AfterUpdateAssociatePeopleTest{
public static testMethod void AfterUpdateAssociatePeopleStaticTestMethod()
{
  Associate__c aa =new Associate__c(Name='TestAss' ,Associate_ID__c='ASSID' ,First_Name__c='AssFName', Current_Address__c='TestAddress',Current_City__c='TestCity',Current_Zip__c='45678',Current_State__c='TX');
  insert aa;

  List<People__c> peopleList = new List<People__c>{new people__c(Name='TestUser',Associate__c=aa.id,CID__c='ASSID',Address_1__c='3445street',City__c='',State__c='XX',Zip__c='75423', )};

  insert peopleList;

  List<People__c> peopleList1 = new List<People__c>{ [select id from People__c where id in :peopleList]};
  for(People__c ppdd:peopleList1)
    ppdd.Address_1__c = aa.Current_Address__c;

  update peopleList1;
  aa.City__c = 'New CIty';
  update aa
}

 

Something else I spotted: your first query in the trigger is redunant as the set of ids you're checking against is empty at that stage!

steve456steve456

aa.City='New City';

 

I think this is not required i guess just we can write update aa it would take .I tested it and i got the code coverage needed