can some one help me to figure out what changes i need to make on this trigger
Hello ,
The intention of this trigger is to associate Contacts to different accounts depending on the contacts record type.
Contacts with Record Type 'US' should be related to the Account whose name is 'US', Contacts with Record Type 'Canada' should be related to the Account whose name is 'Canada'
Contact Trigger mentioned below:
trigger ContactTrigger on Contact (before insert, after insert, before update, after update, before delete, after delete) {
private static final String CANADA= 'Canada';
private static final String US = 'US';
if(Trigger.isafter){
if((trigger.isInsert || Trigger.isUpdate) && Trigger.isafter){
for (Contact c : trigger.new) {
if (c.AccountId == null || c.AccountId == ''){
if (c.RecordType.DeveloperName == 'US') {
c.AccountId = getDefaultAccountIdUS();
} else {
c.AccountId = getDefaultAccountCN();
}
}
}
}
}
private static Id getDefaultAccountCN(){
return [SELECT Id FROM Account WHERE Name = :CANADA LIMIT 1].Id;
}
private static Id getDefaultAccountIdUS(){
return [select Id from Account where Name = :US limit 1].Id;
}
}
Contact Trigger Test:
@isTest
public class ContactTrigger_Test {
@isTest static void createContact() {
Contact theContact = new Contact(LastName = 'Smith');
try {
insert theContact;
} catch (Exception e) {
}
}
}