You need to sign in to do that
Don't have an account?
Chris Bettini
Needing help with Apex test class
This trigger creates an opposing relationship between contacts in a juntion object when an initial one is created. eg. i create a link from Bob to Jon, and the trigger creates the link Jon to Bob. It also creates a comma separated string of the contact ids on each of the respective contacts each time a junction record is created or deleted. I cant seem to get past 41% coverage and not having any luck with the string concatenation part in the test class.
trigger teamLinkUpdate on TeamMemberLink__c (before insert, after insert, after delete) { if(Trigger.isAfter){ if(Trigger.isInsert){ try { for (TeamMemberLink__c t : Trigger.new){ Contact c = [SELECT Id, TeamMemberLinks_List__c FROM Contact WHERE Id = :t.ContactLink1__c]; List<TeamMemberLink__c> l_co = [SELECT ContactLink2__c FROM TeamMemberLink__c WHERE ContactLink1__c = :c.Id]; String[] stringList = new String[0]; for(TeamMemberLink__c lstr : l_co) { String strId = String.valueOf(lstr.ContactLink2__c); stringList.add(strId); } String result = String.join(stringList, ', '); c.TeamMemberLinks_List__c = result; update c; } } catch (Exception e) { System.debug(e); } for (TeamMemberLink__c t : Trigger.new){ List<TeamMemberLink__c> tchk = [Select Id from TeamMemberLink__c where ContactLink2__c = :t.ContactLink1__c]; if(tchk.isEmpty()){ List<TeamMemberLink__c> rel = new List<TeamMemberLink__c>(); TeamMemberLink__c tnew = new TeamMemberLink__c(ContactLink1__c = t.ContactLink2__c, ContactLink2__c = t.ContactLink1__c); rel.add(tnew); insert rel; } } } if(Trigger.isDelete){ try { for (TeamMemberLink__c t : Trigger.old){ Contact c = [SELECT Id, TeamMemberLinks_List__c FROM Contact WHERE Id = :t.ContactLink1__c]; List<TeamMemberLink__c> l_co = [SELECT ContactLink2__c FROM TeamMemberLink__c WHERE ContactLink1__c = :c.Id]; String[] stringList = new String[0]; for(TeamMemberLink__c lstr : l_co) { String strId = String.valueOf(lstr.ContactLink2__c); stringList.add(strId); } String result = String.join(stringList, ', '); c.TeamMemberLinks_List__c = result; update c; } } catch (Exception e) { System.debug(e); } } } }test class
@isTest public class teamLinkUpdateTest { static testMethod void testUpdate() { TeamMemberLink__c tm = new TeamMemberLink__c(); tm.ContactLink1__c = '0034A00002lPY13QAG'; tm.ContactLink2__c = '003G000002DpiytIAB'; insert tm; } }
2. There are major design flaws in your trigger, which will almost surely fail your test class anyway (or worse, later in production). You need to remove all the SOQL queries you put inside loops (as they are guarenteed to exceed the 100 SOQL queries per transaction governor limit), and rediesign your entire code.
You also need to restructure that piece of code you are working o.