1

I've been working as an admin for my org for the last few months. After my move to Lightning, we needed to use the AccountContactRelation object to be able to mark a contact as the primary contact on the account. I needed a trigger to be able to prevent multiple primary contacts from being checked.

I was able to use the code below and it functions perfectly in my Sandbox environment when I tested it, however it's receiving a 0% code coverage, thus I can't deploy it to Prod. Based on reading and messing around, I was able to sort-of create a test-class, but I'm stuck on where to go from here as I have zero dev experience (except HTML/CSS).

I'd appreciate any help I can get! Thank you!

Trigger code that works in Sandbox:

 Trigger PreventMultiplePrimaryContacts on AccountContactRelation (Before Insert, Before Update) {

    List<AccountContactRelation> recordsToProcess = New List<AccountContactRelation>();
    List<Id> accountIds = New List<Id>();
    List<Id> contactIds = New List<Id>();


    If(Trigger.isBefore && (Trigger.IsInsert || Trigger.IsUpdate)) {
        For(AccountContactRelation EveryRelationshipRecord : Trigger.New) {
            If(EveryRelationshipRecord.Primary__c == TRUE) {
                recordsToProcess.add(EveryRelationshipRecord);
                accountIds.add(EveryRelationshipRecord.AccountId);
                contactIds.add(EveryRelationshipRecord.ContactId);
            }
        }
    }

    List<AccountContactRelation> existingRecords = [
        Select id, ContactId, AccountId, Primary__c
        FROM AccountContactRelation
        WHERE Primary__c = TRUE
            AND Id !=: recordsToProcess
            AND AccountId =: accountIds
    ];


    If(recordsToProcess.isEmpty())    return;

    For(AccountContactRelation junctionObj :recordsToProcess ) {
        For(AccountContactRelation EveryExistingRecord : existingRecords) {
            If(junctionObj.AccountId == EveryExistingRecord.AccountId) {
                junctionObj.addError('There is already a Primary Contact for this Account. Please uncheck the other contact as primary before continuing.');
            }
        }
    }
}

Test Class code that doesn't work:

@isTest
private class TestAccountContactRelation {
     @isTest static void TestAccountContactRelation() {
         Account acct = new Account(
             Name='HelloWorld'
         );

         insert acct;

         Contact con = new Contact(
             AccountId = acct.id,
             lastname = 'testdata', 
             firstname ='testdata1'
         );

         insert con; 

         Contact con1 = new Contact(
             AccountId = acct.id,
             lastname = 'testdata2', 
             firstname ='testdata2'
         );

         insert con1; 

         AccountContactRelation acctcr = new AccountContactRelation(
             AccountId = acct.id, 
             ContactId = con.id, 
             Primary__c = TRUE
         );

         insert acctcr;

         Test.startTest();

         acctcr.Primary__c = True;
         update con;

         Test.stopTest();

         system.asserts(acctcr.AccountId,acct.Id);
     }
}
3
  • 1
    There is no way this code compiled. Did it save successfully? Usually, you will get better help from the community if you paste any error messages verbatim into your post.
    – Adrian Larson
    Dec 28, 2018 at 16:41
  • I agree with Adrian, you're most likely getting 0% coverage because you have another error somewhere else. If a test class fails it won't provide the coverage you need. I think you need to re-word your question to be more inline with your actual issue.
    – gNerb
    Dec 28, 2018 at 16:47
  • Specifically, this code should have failed to save with a fairly obvious error message like Method does not exist or incorrect signature: void asserts(Id, Id) from the type System.
    – Adrian Larson
    Dec 28, 2018 at 16:48

2 Answers 2

-3

You can use the below test method. It will give you the code coverage as well as cover the intention of the code(which is very important).

@isTest
public class TestAccountContactRelation {
    @isTest 
    static void TestAccountContactRelation() {

        Account acct = new Account(Name='HelloWorld');
        insert acct;

        Contact con = new Contact(AccountId = acct.id,lastname = 'testdata' , firstname ='testdata1');
        insert con; 

        Contact con1 = new Contact(AccountId = acct.id,lastname = 'testdata2' , firstname ='testdata2');
        insert con1; 

        AccountContactRelation acctcr = new AccountContactRelation(AccountId = acct.id, ContactId = con.id, Primary__c = TRUE);
        insert acctcr;

        List<AccountContactRelation> all = [SELECT ID, AccountId, ContactId, Primary__c FROM AccountContactRelation];
        System.assertEquals(1, all.size());
        System.assertEquals(acct.id, all.get(0).AccountId);
        System.assertEquals(con.id, all.get(0).ContactId);
        System.assertEquals(True, all.get(0).Primary__c);

        AccountContactRelation acctcr1 = new AccountContactRelation(AccountId = acct.id, ContactId = con1.id, Primary__c = TRUE);
        insert acctcr1;
        all = [SELECT ID, AccountId, ContactId, Primary__c FROM AccountContactRelation];
        System.assertEquals(2, all.size());
        /* check for AccountContactRelation record with Primary__c = false and also check for the error message */     

    }
}

Few comments

4
  • 3
    We have a teach a man to fish philosophy here, and you're just giving the OP a completed solution while teaching them nothing. It's not a free consulting site, and code dumps are frowned upon. Additionally, your assertions entirely neglect useful error messages.
    – Adrian Larson
    Dec 28, 2018 at 16:51
  • Sorry for this. I tried to help him giving him the direction on how to write test classes and intentionally neglecting error message. Covering that will give him 100% code coverage. That is why I have given few links at the bottom of my post so that he can learn and implement the rest of the stuff. Anyway thank you @AdrianLarson for your feedback. Dec 28, 2018 at 16:58
  • Thank you! What exactly does the all.get(0) and all.size() syntax actually doing? Dec 28, 2018 at 17:07
  • all.size() will return the size of the list and all.get(0) will return the first element from the list which in this case your first AccountContactRelation record. Dec 28, 2018 at 17:11
4

When you update a record in the database, the in-memory record is not automatically updated. You need to query the record back again to see how it looks in the database.

acctcr = [select AccountId from accountcontactrelation where id = :acctcr.Id];
system.assertequals(acctcr.AccountId,acct.Id);

Please note that the method is System.assertEquals, not System.asserts. There are several different versions of assert methods you can call, which you can read about in the System namespace.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .