You need to sign in to do that
Don't have an account?
sonali verma
Update contact description field
Hi friends
I am using a child relationship query to update contact description fields whenever an account record is created or updated.
Apex class code & trigger code is workign fine but I am having an issue in test class. The code coverage is showing 72% .
Apex class code;
public class Account_ContactDescription
{
public void Update_Description(Set<Id> accountIds)
{
list<Account> accountsWithContacts =[select Id,Name, (select Id,FirstName,LastName from Contacts) from Account
where Id IN : accountIds];
list<Contact> contactsToUpdate=new list<Contact>();
for(Account a:accountsWithContacts) //loop through all queries
{
for(Contact convals:a.Contacts)
{
convals.Description=convals.FirstName+' '+convals.LastName;
contactsToUpdate.add(convals);
}
}
update contactsToUpdate;
}
}
Apex Trigger code:
trigger trg_UpdateContactDescription on Account (before Update)
{
Account_ContactDescription c=new Account_ContactDescription();
Map<Id,Account> amap = new Map<Id,Account>([select Id from Account]);
Set<Id> accountIds = amap.KeySet();
c.Update_Description(accountIds);
}
Test class code:
@istest(SeeAllData=false)
private class AccountContactDescription_Test
{
private static testmethod void validatetest()
{
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u = new User(Alias = 'sample', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='sampleLname', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='testnamesss@testorg.com');
system.runAs(u)
{
Account acn = new Account();
acn.Name='test1';
Database.Insert(acn);
Contact con = new Contact();
con.LastName='test3';
con.FirstName='test4';
Database.Insert(con);
acn.Name='test2';
Database.Update(acn);
}
}
}
How to update the description field but not sure how to go about it.
Can anyone pls le tme know:?
regards
sonali verma
I am using a child relationship query to update contact description fields whenever an account record is created or updated.
Apex class code & trigger code is workign fine but I am having an issue in test class. The code coverage is showing 72% .
Apex class code;
public class Account_ContactDescription
{
public void Update_Description(Set<Id> accountIds)
{
list<Account> accountsWithContacts =[select Id,Name, (select Id,FirstName,LastName from Contacts) from Account
where Id IN : accountIds];
list<Contact> contactsToUpdate=new list<Contact>();
for(Account a:accountsWithContacts) //loop through all queries
{
for(Contact convals:a.Contacts)
{
convals.Description=convals.FirstName+' '+convals.LastName;
contactsToUpdate.add(convals);
}
}
update contactsToUpdate;
}
}
Apex Trigger code:
trigger trg_UpdateContactDescription on Account (before Update)
{
Account_ContactDescription c=new Account_ContactDescription();
Map<Id,Account> amap = new Map<Id,Account>([select Id from Account]);
Set<Id> accountIds = amap.KeySet();
c.Update_Description(accountIds);
}
Test class code:
@istest(SeeAllData=false)
private class AccountContactDescription_Test
{
private static testmethod void validatetest()
{
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u = new User(Alias = 'sample', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='sampleLname', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='testnamesss@testorg.com');
system.runAs(u)
{
Account acn = new Account();
acn.Name='test1';
Database.Insert(acn);
Contact con = new Contact();
con.LastName='test3';
con.FirstName='test4';
Database.Insert(con);
acn.Name='test2';
Database.Update(acn);
}
}
}
How to update the description field but not sure how to go about it.
Can anyone pls le tme know:?
regards
sonali verma
Check out the below modified class and test class. This will give 100% code coverage, In your test class while inserting contact record you missed to add account to that contact. This is the reason you have not get coverage.
Trigger:
Apex Class:
Test Class:
All Answers
Check out the below modified class and test class. This will give 100% code coverage, In your test class while inserting contact record you missed to add account to that contact. This is the reason you have not get coverage.
Trigger:
Apex Class:
Test Class:
Thanks!
There is a simple mistake in your test class. you have not put the Account Id into your Contact. The only thing you need to change is ,
Thanks :)
Thanks, I just releaised my mistake.
Thanks
sonali