You need to sign in to do that
Don't have an account?
Lakis
Test class to Update Contact record based on specific Account record type
Hey everyone,
It's my first attempt to write a test class. I have the following trigger and I'm struggling to write a test big time. Can anyone assist?
The case is pretty simple... If one of the picklist values on the Account with the specific record type 'Exhibitor' is changed, corresponding picklists on related Contact records (Contact record type is called 'Exhibitor') must update.
***
trigger Contact_Importance_Update on Account (before update) {
Id recTypeId = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Exhibitor').getRecordTypeId(); //using it like this you won't have problems to deploy (and it'll save you a query)
List<Account> accountsToVerify = new List<Account>();
for(account a : trigger.new){
if(a.RecordTypeId == recTypeId //checks if the account has the correct RecordType
&& (a.PURE_Importance__c != trigger.oldMap.get(a.Id).PURE_Importance__c
|| a.WAA_Importance__c != trigger.oldMap.get(a.Id).WAA_Importance__c
|| a.LE_Miami_Importance__c != trigger.oldMap.get(a.Id).LE_Miami_Importance__c
|| a.PURE_Agent__c != trigger.oldMap.get(a.Id).PURE_Agent__c
|| a.LE_Miami_agent__c != trigger.oldMap.get(a.Id).LE_Miami_agent__c
|| a.WAA_agent__c != trigger.oldMap.get(a.Id).WAA_agent__c)){ //checks if the picklist is changed
accountsToVerify.add(a);
}
}
if(!accountsToVerify.isEmpty()){ //will only process if there's a need to.
List<Contact> contactsToUpdate = [SELECT Id, AccountId, FirstName FROM Contact WHERE AccountId IN :accountsToVerify];
for(Contact c : contactsToUpdate){
Account a = trigger.newMap.get(c.AccountId);
c.PURE_Importance_contact__c=a.PURE_Importance__c;
c.We_Are_Africa_Importance_contact__c=a.WAA_Importance__c;
c.LE_Miami_Importance_contacts__c=a.LE_Miami_Importance__c;
c.PURE_Agent__c=a.PURE_Agent__c;
c.LE_Miami_agent__c=a.LE_Miami_agent__c;
c.WAA_agent__c=a.WAA_agent__c;
}
update contactsToUpdate;
}
}
***
I got as far as this and I guess faaaaar away. Not sure how to relate records and how to make sure correct record type is called.
@isTest
private class Contact_Update
{
static testMethod void Contact_Importance_Update()
{
Account acc = new Account(name = 'testaccount',Street__c = 'teststreet', Country_area_code__c = 'Afghanistan (GMT +4:30; area code +93)', Region__c = 'Asia - South-Central (and India Sub-Cont.)');
insert acc;
Contact con = new Contact(AccountId = acc.id,lastname = 'testcontact' , firstname ='testdata1',OwnerId = Userinfo.getUserid(), Country__c = 'Afghanistan (GMT +4:30; area code +93)', Region__c = 'Asia - South-Central (and India Sub-Cont.)');
insert con;
Account a = new Account();
a.PURE_Importance__c = 'Leader';
a.PURE_Agent__c = 'Name1';
a.LE_Miami_Importance__c = 'In hand';
a.LE_Miami_Agent__c = 'Name2';
a.WAA_Importance__c = 'Suspect';
a.WAA_Agent__c = 'Name3';
update a;
}
}
It's my first attempt to write a test class. I have the following trigger and I'm struggling to write a test big time. Can anyone assist?
The case is pretty simple... If one of the picklist values on the Account with the specific record type 'Exhibitor' is changed, corresponding picklists on related Contact records (Contact record type is called 'Exhibitor') must update.
***
trigger Contact_Importance_Update on Account (before update) {
Id recTypeId = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Exhibitor').getRecordTypeId(); //using it like this you won't have problems to deploy (and it'll save you a query)
List<Account> accountsToVerify = new List<Account>();
for(account a : trigger.new){
if(a.RecordTypeId == recTypeId //checks if the account has the correct RecordType
&& (a.PURE_Importance__c != trigger.oldMap.get(a.Id).PURE_Importance__c
|| a.WAA_Importance__c != trigger.oldMap.get(a.Id).WAA_Importance__c
|| a.LE_Miami_Importance__c != trigger.oldMap.get(a.Id).LE_Miami_Importance__c
|| a.PURE_Agent__c != trigger.oldMap.get(a.Id).PURE_Agent__c
|| a.LE_Miami_agent__c != trigger.oldMap.get(a.Id).LE_Miami_agent__c
|| a.WAA_agent__c != trigger.oldMap.get(a.Id).WAA_agent__c)){ //checks if the picklist is changed
accountsToVerify.add(a);
}
}
if(!accountsToVerify.isEmpty()){ //will only process if there's a need to.
List<Contact> contactsToUpdate = [SELECT Id, AccountId, FirstName FROM Contact WHERE AccountId IN :accountsToVerify];
for(Contact c : contactsToUpdate){
Account a = trigger.newMap.get(c.AccountId);
c.PURE_Importance_contact__c=a.PURE_Importance__c;
c.We_Are_Africa_Importance_contact__c=a.WAA_Importance__c;
c.LE_Miami_Importance_contacts__c=a.LE_Miami_Importance__c;
c.PURE_Agent__c=a.PURE_Agent__c;
c.LE_Miami_agent__c=a.LE_Miami_agent__c;
c.WAA_agent__c=a.WAA_agent__c;
}
update contactsToUpdate;
}
}
***
I got as far as this and I guess faaaaar away. Not sure how to relate records and how to make sure correct record type is called.
@isTest
private class Contact_Update
{
static testMethod void Contact_Importance_Update()
{
Account acc = new Account(name = 'testaccount',Street__c = 'teststreet', Country_area_code__c = 'Afghanistan (GMT +4:30; area code +93)', Region__c = 'Asia - South-Central (and India Sub-Cont.)');
insert acc;
Contact con = new Contact(AccountId = acc.id,lastname = 'testcontact' , firstname ='testdata1',OwnerId = Userinfo.getUserid(), Country__c = 'Afghanistan (GMT +4:30; area code +93)', Region__c = 'Asia - South-Central (and India Sub-Cont.)');
insert con;
Account a = new Account();
a.PURE_Importance__c = 'Leader';
a.PURE_Agent__c = 'Name1';
a.LE_Miami_Importance__c = 'In hand';
a.LE_Miami_Agent__c = 'Name2';
a.WAA_Importance__c = 'Suspect';
a.WAA_Agent__c = 'Name3';
update a;
}
}
All Answers
Line 53 should read:
Id = :conId];
Line 56 should read:
System.assertEquals(acc.We_Are_Africa_Importance__c,result.WAA_Importance__c);
Hi James, thank you so much for taking time and explaining this. That's excatly what I needed (I mean //explanations).
I'm trying to test this and I'm getting the folloowing error: expecting a colon, found 'con.Id' at line 53 column 21 - I don't understand why colon is expected?
Also, with regards to System assert equals I'm getting: Compile Error: Method does not exist or incorrect signature: System.assertEquals(String) at line 55 column 9?
I think it should be:
Id = :con.Id]; ?
I hope this finds you well... I have a question on the trigger above:
Id recTypeId = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Exhibitor').getRecordTypeId()
This method is getting "Exhibitor" account record types. How can I add additional record types i.e. "Allied Industry" or "Tourist Board"?
Many thanks in advance.
Lakis