Struggling with Line 22.trigger UKCheck on Domain__c (before insert, before update) {
Map<String,Country_Codes__c> getCodes = Country_Codes__c.getAll();
for (Domain__c dom : Trigger.new) {
if(dom.Country__c != NULL) {
for(String s : getCodes.keySet()) {
if(s.containsIgnoreCase(dom.Country__c)) {
dom.UK_Team_Checkbox__c = TRUE;
}
}
}
if(Trigger.isUpdate) {
if (Trigger.oldMap.get(dom.Id).Company__c != Trigger.newMap.get(dom.Id).Company__c) {
if(dom.Company__c != NULL) {
if(dom.Company__r.OwnerId != NULL) {
dom.OwnerId = dom.Company__r.OwnerId;
System.debug(dom.Owner);
System.debug(dom.Company__r.Owner.Id);
System.debug(dom.Company__c);
}
}
}
}
}
}
Hi Darrell,Make your trigger run on 'after update' instead of before update. On before update ownerid is not visible in apex trigger, therefore its not covering the highlighted line.Please like then answer and mark it as best if this helps.Thanks,Aman Please try below code i hope that will help you
Let us know if this will help you@isTest
public class UKCheck_Test {
@TestSetup
static void createUser() {
UserRole r = new UserRole(DeveloperName = 'SovrnCustomRole', Name = 'Sovrn Role');
insert r;
User u1 = new User();
u1.LastName = 'Test1';
u1.Alias = 'alias1';
u1.Email = 'test1@fake.com';
u1.Username = 'testsovrn2018@fake.com';
u1.CompanyName = 'Test1';
u1.TimeZoneSidKey ='America/Los_Angeles';
u1.EmailEncodingKey ='UTF-8';
u1.LocaleSidKey = 'en_US';
u1.UserRoleId = r.Id;
u1.LanguageLocaleKey = 'en_US';
u1.ProfileId = [SELECT Id FROM Profile WHERE Name = 'Publisher Advocate'].Id;
insert u1;
}
static testmethod void validateUKCheck () {
List<Account> accts = new List<Account>();
List<Domain__c> domList = new List<Domain__c>();
//Create Companies(Account)
Account acct = new Account();
acct.Name = 'TestCompany1';
acct.OwnerId = UserInfo.getUserId();
accts.add(acct);
System.debug('Acct equals ' + acct.Name);
System.debug('Acct Owner is ' + acct.OwnerId);
Account acct1 = new Account();
acct1.Name = 'TestCompany2';
acct1.OwnerId = [SELECT Id FROM User WHERE UserName = 'testsovrn2018@fake.com'].Id;
accts.add(acct1);
System.debug('Acct1 equals ' + acct1.Name);
System.debug('Acct1 Owner is ' + acct1.OwnerId);
Test.startTest();
insert accts;
Test.stopTest();
//Create Custom Setting
Country_Codes__c cS = new Country_Codes__c();
cS.Name = 'GB';
cS.Country__c = 'GB';
insert cS;
//Create Custom Setting
Country_Codes__c cS1 = new Country_Codes__c();
cS1.Name = 'USA';
cS1.Country__c = 'USA';
insert cS1;
//Create Domain records
Domain__c UKDom = new Domain__c();
UKDom.Name = 'testdomain.com';
UKDom.Website__c = 'www.testdomain.com';
UKDom.Country__c = 'GB';
UKDom.Uk_Team__c = TRUE;
UKDom.Company__c = acct.Id;
UKDom.OwnerId = UserInfo.getUserId();
System.debug('UKDom domain Company__c is ' + acct.Name);
System.debug('UKDom domain ownerID is ' + UKDom.OwnerId);
domList.add(UKDom);
Domain__c noUK = new Domain__c();
noUK.Name = 'testdomainnoUK.com';
noUK.Website__c = 'www.testdomainnoUK.com';
noUK.Country__c = 'US';
noUK.OwnerId = acct1.OwnerId;
noUk.Company__c = acct1.Id;
noUK.UK_Team__c = FALSE;
System.debug('noUK domain Company__c is ' + acct1.Name);
System.debug('noUK domain Company__c is ' + acct1.Id);
System.debug('noUK domain ownerID is ' + noUK.OwnerId);
domList.add(noUK);
insert domList;
System.debug('noUK domain ID ' + noUk.Id);
Domain__c domainToUpdate = [SELECT Id, Name, Company__c, Company__r.OwnerId, OwnerId FROM Domain__c WHERE Id = :noUK.Id];
domainToUpdate.Company__c = acct.Id;
domainToUpdate.OwnerId = acct.OwnerId;
update domainToUpdate;
System.debug(domainToUpdate);
System.debug('NOW noUK domain Company__c is ' + domainToUpdate.Company__c);
System.debug('NOW noUK domain ownerID is ' + domainToUpdate.OwnerId);
System.assertEquals(UKDom.UK_Team__c, True);
System.assertEquals(noUK.UK_Team__c, FALSE);
System.assertEquals(domainToUpdate.OwnerId, acct.OwnerId);
UKDom.Country__c ='USA';
update UKDom;
}
}
Finally got it through changing my Trigger thanks all. trigger UKCheck on Domain__c (before insert, before update) {
Map<String,Country_Codes__c> getCodes = Country_Codes__c.getAll();
for (Domain__c dom : Trigger.new) {
if(dom.Country__c != NULL) {
for(String s : getCodes.keySet()) {
if(s.containsIgnoreCase(dom.Country__c)) {
dom.UK_Team_Checkbox__c = TRUE;
}
}
}
System.debug('dom.Owner is ' + dom.Owner);
System.debug(dom.Company__r.Owner.Id);
System.debug('dom.Company__c is ' + dom.Company__c);
}
Map<Id, Account> a = new Map<Id, Account>();
for(Domain__c domain : Trigger.new) {
a.put(domain.Company__c, null);
}
System.debug('Before putAll ' + a);
a.putAll([SELECT OwnerId FROM Account WHERE Id IN :a.keySet()]);
System.debug('After putAll ' + a);
for(Domain__c domain : Trigger.new) {
if(domain.Company__c != NULL) {
domain.OwnerId = a.get(domain.Company__c).OwnerId;
}
}
}
@isTest
public class UKCheck_Test {
@TestSetup
static void createUser() {
UserRole r = new UserRole(DeveloperName = 'SovrnCustomRole', Name = 'Sovrn Role');
insert r;
User u1 = new User();
u1.LastName = 'Test1';
u1.Alias = 'alias1';
u1.Email = 'test1@fake.com';
u1.Username = 'testsovrn2018@fake.com';
u1.CompanyName = 'Test1';
u1.TimeZoneSidKey ='America/Los_Angeles';
u1.EmailEncodingKey ='UTF-8';
u1.LocaleSidKey = 'en_US';
u1.UserRoleId = r.Id;
u1.LanguageLocaleKey = 'en_US';
u1.ProfileId = [SELECT Id FROM Profile WHERE Name = 'Publisher Advocate'].Id;
insert u1;
}
static testmethod void validateUKCheck () {
List<Account> accts = new List<Account>();
List<Domain__c> domList = new List<Domain__c>();
//Create Companies(Account)
Account acct = new Account();
acct.Name = 'TestCompany1';
acct.OwnerId = UserInfo.getUserId();
accts.add(acct);
System.debug('Acct equals ' + acct.Name);
System.debug('Acct Owner is ' + acct.OwnerId);
Account acct1 = new Account();
acct1.Name = 'TestCompany2';
acct1.OwnerId = [SELECT Id FROM User WHERE UserName = 'testsovrn2018@fake.com'].Id;
accts.add(acct1);
System.debug('Acct1 equals ' + acct1.Name);
System.debug('Acct1 Owner is ' + acct1.OwnerId);
Test.startTest();
insert accts;
Test.stopTest();
//Create Custom Setting
Country_Codes__c cS = new Country_Codes__c();
cS.Name = 'GB';
cS.Country__c = 'GB';
insert cS;
//Create Domain records
Domain__c UKDom = new Domain__c();
UKDom.Name = 'testdomain.com';
UKDom.Website__c = 'www.testdomain.com';
UKDom.Country__c = 'GB';
UKDom.Uk_Team__c = TRUE;
UKDom.Company__c = acct.Id;
UKDom.OwnerId = UserInfo.getUserId();
System.debug('UKDom domain Company__c is ' + acct.Name);
System.debug('UKDom domain ownerID is ' + UKDom.OwnerId);
domList.add(UKDom);
Domain__c noUK = new Domain__c();
noUK.Name = 'testdomainnoUK.com';
noUK.Website__c = 'www.testdomainnoUK.com';
noUK.Country__c = 'US';
noUK.OwnerId = acct1.OwnerId;
noUk.Company__c = acct1.Id;
noUK.UK_Team__c = FALSE;
System.debug('noUK domain Company__c is ' + acct1.Name);
System.debug('noUK domain Company__c is ' + acct1.Id);
System.debug('noUK domain ownerID is ' + noUK.OwnerId);
domList.add(noUK);
insert domList;
System.debug('noUK domain ID ' + noUk.Id);
Domain__c domainToUpdate = [SELECT Id, Name, Company__c, Company__r.OwnerId, OwnerId FROM Domain__c WHERE Id = :noUK.Id];
domainToUpdate.Company__c = acct.Id;
domainToUpdate.OwnerId = acct.OwnerId;
update domainToUpdate;
System.debug(domainToUpdate);
System.debug('NOW noUK domain Company__c is ' + domainToUpdate.Company__c);
System.debug('NOW noUK domain ownerID is ' + domainToUpdate.OwnerId);
System.assertEquals(UKDom.UK_Team__c, True);
System.assertEquals(noUK.UK_Team__c, FALSE);
System.assertEquals(domainToUpdate.OwnerId, acct.OwnerId);
}
}
@Suraj Tripathi Here is my Test Class. I have been modifying it a lot so forgive all the code, hahaha.
I will create a test User to complete this as opposed to hard coding Record ID, but I am concerned with not meeting the 100% coverage right now.@isTest
public class UKCheck_Test {
static testmethod void validateUKCheck () {
List<Account> accts = new List<Account>();
List<Domain__c> domList = new List<Domain__c>();
Account acct = new Account();
acct.Name = 'TestCompany1';
acct.OwnerId = UserInfo.getUserId();
accts.add(acct);
System.debug('Acct equals ' + acct.Name);
System.debug('Acct Owner is ' + acct.OwnerId);
Account acct1 = new Account();
acct1.Name = 'TestCompany2';
acct1.OwnerId = '005G0000007qVef';
accts.add(acct1);
System.debug('Acct1 equals ' + acct1.Name);
System.debug('Acct1 Owner is ' + acct1.OwnerId);
insert accts;
Country_Codes__c cS= new Country_Codes__c();
cS.Name = 'GB';
cS.Country__c = 'GB';
insert cS;
Domain__c UKDom = new Domain__c();
UKDom.Name = 'testdomain.com';
UKDom.Website__c = 'www.testdomain.com';
UKDom.Country__c = 'GB';
UKDom.Uk_Team__c = TRUE;
UKDom.Company__c = acct.Id;
UKDom.OwnerId = UserInfo.getUserId();
System.debug('UKDom domain Company__c is ' + acct.Name);
System.debug('UKDom domain ownerID is ' + UKDom.OwnerId);
domList.add(UKDom);
Domain__c noUK = new Domain__c();
noUK.Name = 'testdomainnoUK.com';
noUK.Website__c = 'www.testdomainnoUK.com';
noUK.Country__c = 'US';
noUK.OwnerId = acct1.OwnerId;
noUk.Company__c = acct1.Id;
noUK.UK_Team__c = FALSE;
System.debug('noUK domain Company__c is ' + acct1.Name);
System.debug('noUK domain ownerID is ' + noUK.OwnerId);
domList.add(noUK);
insert domList;
System.debug('noUK domain is ' + noUk.Id);
//Domain__c domainToUpdate = [SELECT Id, Name, Company__c, Company__r.OwnerId, OwnerId FROM Domain__c WHERE Id = :noUK.Id];
//domainToUpdate.Company__c = acct.Id;
//domainToUpdate.OwnerId = acct.OwnerId;
noUK.Company__c = [SELECT Id, OwnerId FROM Account Limit 1].Id;
update noUK;
//update domainToUpdate;
//System.debug(domainToUpdate.Id);
//System.debug('noUK Company is now ' + domainToUpdate.Company__c);
//System.debug('noUK Owner is now ' + domainToUpdate.OwnerId);
System.assertEquals(UKDom.UK_Team__c, True);
System.assertEquals(noUK.UK_Team__c, FALSE);
}
}
Aman and Suraj - THank you both for commenting. I am going to try Aman Malik's suggestion first. If I cannot figure it out I will post my current Test Class. So stay tuned as I will post later today. Hi Darrell,
Can you please send your Test class code here: So we can just some edits in your code if there is need anything.
Regards,
Suraj