function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JAckerman09JAckerman09 

Unit Tests When Creating Person Accounts

This is the trigger that the test is being run on:

 

trigger teamMembersFollowRels on Account (after insert, after update) {
    for(Account acc: Trigger.new){
        
//**** INSERT SECTION ****//  
        
        if(Trigger.isInsert){
            if(acc.Portfolio_Manager_Lookup__c != Null){
                EntitySubscription followPM = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Portfolio_Manager_Lookup__c
                );
                insert followPM;
            }
            if(acc.Trust_Officer__c != Null){
                EntitySubscription followTO = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Trust_Officer__c
                );
                insert followTO;
            }
            if(acc.Wealth_Advisor__c != Null){
                EntitySubscription followWA = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Wealth_Advisor__c
                );
                insert followWA;
            }
            if(acc.Other_Team_Member_1__c != Null){
                EntitySubscription followOT = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Other_Team_Member_1__c
                );
                insert followOT;
            }
            if(acc.Other_Team_Member_2__c != Null){
                EntitySubscription followOTT = new EntitySubscription(
                parentId = acc.id,
                subscriberid = acc.Other_Team_Member_2__c
                );
                insert followOTT;
            }
            
//**** UPDATE SECTION ****//            
            
        }
        if (Trigger.isUpdate){
            if(acc.Portfolio_Manager_Lookup__c != Null){
                if(acc.Portfolio_Manager_Lookup__c != Trigger.oldMap.get(acc.ID).Portfolio_Manager_Lookup__c){
                    EntitySubscription followPM = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Portfolio_Manager_Lookup__c
                    );
                    insert followPM;
                }
            }
            if(acc.Trust_Officer__c != Null){
                if(acc.Trust_Officer__c != Trigger.oldMap.get(acc.ID).Trust_Officer__c){
                    EntitySubscription followTO = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Trust_Officer__c
                    );
                    insert followTO;
                }
            }
            if(acc.Wealth_Advisor__c != Null){
                if(acc.Wealth_Advisor__c != Trigger.oldMap.get(acc.ID).Wealth_Advisor__c){
                    EntitySubscription followWA = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Wealth_Advisor__c
                    );
                    insert followWA;
                }
            }
            if(acc.Other_Team_Member_1__c != Null){
                if(acc.Other_Team_Member_1__c != Trigger.oldMap.get(acc.ID).Other_Team_Member_1__c){
                    EntitySubscription followOT = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Other_Team_Member_1__c
                    );
                    insert followOT;
                }
            }
            if(acc.Other_Team_Member_2__c != Null){
                if(acc.Other_Team_Member_2__c != Trigger.oldMap.get(acc.ID).Other_Team_Member_2__c){
                    EntitySubscription followOTT = new EntitySubscription(
                    parentId = acc.id,
                    subscriberid = acc.Other_Team_Member_2__c
                    );
                    insert followOTT;
                }
            }
        }
    }
}

 

Here's the unit test:

 

@isTest
private class testTeamMembersFollowRels{
    static testMethod void insertAccountWithFollowers(){
         system.debug('Start...');
         
         Id RecId = [
            SELECT r.Id, r.Name, r.DeveloperName, r.IsPersonType 
            FROM RecordType r 
            WHERE sObjectType = 'Account' AND IsPersonType=True AND DeveloperName='Individual'
        ].Id;
        
        Id pmId = [
            SELECT u.Id, u.Name
            FROM User u
            WHERE name = 'Jason Ackerman'
        ].Id;
        Id toId = [
            SELECT u.Id, u.Name
            FROM User u
            WHERE name = 'Linell Bigelow'
        ].Id;
        Id waId = [
            SELECT u.Id, u.Name
            FROM User u
            WHERE name = 'Zoie Flaks'
        ].Id;
        Id otId = [
            SELECT u.Id, u.Name
            FROM User u
            WHERE name = 'Aneta Bijak'
        ].Id;
        Id ottId = [
            SELECT u.Id, u.Name
            FROM User u
            WHERE name = 'Marianne Noonan'
        ].Id;
        
        account accs = new account(
            //Name='',
            salutation = 'Mr.',
            firstname = 'John',
            lastname = 'Sandbox1',
            Portfolio_Manager_Lookup__c=pmId ,
            Trust_Officer__c=toId,
            Wealth_Advisor__c=waId,
            Other_Team_Member_1__c=otId,
            Other_Team_Member_2__c=ottId,
            recordtypeid = RecId
        );
        
        insert accs;
        
        //accs.recordtypeid = RecId;
        //update accs;
        
        //contact cons = new contact(accountid=accs.id, salutation='Mr.',firstname='John',lastname='Sandbox1');
        //insert cons;
        

        
        //Find New Chatter Entity//
        EntitySubscription followedPM = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accs.Portfolio_Manager_Lookup__c
            AND parentid = :accs.id
        ];
        EntitySubscription followedTO = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accs.Trust_Officer__c
            AND parentid = :accs.id
        ];
        EntitySubscription followedWA = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accs.Wealth_Advisor__c
            AND parentid = :accs.id
        ];
        EntitySubscription followedOT = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accs.Other_Team_Member_1__c
            AND parentid = :accs.id
        ];
        EntitySubscription followedOTT = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accs.Other_Team_Member_2__c
            AND parentid = :accs.id
        ];
        
        system.debug('BEFORE SYSTEM ASSERTS:: PM: ' + pmId + ', TO: ' + toId + ', WA: ' + waId + ', OT: ' + otId + ', OTT: ' + ottId);
                
        system.assertequals(followedPM.parentid, accs.id);
        system.assertequals(followedPM.subscriberid, accs.Portfolio_Manager_Lookup__c);

        system.assertequals(followedTO.parentid, accs.id);
        system.assertequals(followedTO.subscriberid, accs.Trust_Officer__c);

        system.assertequals(followedWA.parentid, accs.id);
        system.assertequals(followedWA.subscriberid, accs.Wealth_Advisor__c);

        system.assertequals(followedOT.parentid, accs.id);
        system.assertequals(followedOT.subscriberid, accs.Other_Team_Member_1__c);
        
        system.assertequals(followedOTT.parentid, accs.id);
        system.assertequals(followedOTT.subscriberid, accs.Other_Team_Member_2__c);
        
        //TEST UPDATE//
        account accsUp = new account(
            //Name='Mr. John Sandbox2',
            salutation = 'Mr.',
            firstname = 'John',
            lastname = 'Sandbox1',
            recordtypeid = RecId
        );
        insert accsUp;
        
        //contact consUp = new contact(accountid=accsUp.id,salutation='Mr.',firstname='John',lastname='Sandbox2');
        //insert consUp;

        //accsUp.recordtypeid = RecId;
        //update accsUp;
        
        system.debug('AFTER SYSTEM ASSERTS:: PM: ' + pmId + ', TO: ' + toId + ', WA: ' + waId + ', OT: ' + otId + ', OTT: ' + ottId);
        accsUp.Portfolio_Manager_Lookup__c=pmId;
        accsUp.Trust_Officer__c=toId;
        accsUp.Wealth_Advisor__c=waId;
        accsUp.Other_Team_Member_1__c=otId;
        accsUp.Other_Team_Member_2__c=ottId;
        update accsUp;

        EntitySubscription followedPMUP = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accsUp.Portfolio_Manager_Lookup__c
            AND parentid = :accsUp.id
        ];
        EntitySubscription followedTOUP = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accsUp.Trust_Officer__c
            AND parentid = :accsUp.id
        ];
        EntitySubscription followedWAUP = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accsUp.Wealth_Advisor__c
            AND parentid = :accsUp.id
        ];
        EntitySubscription followedOTUP = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accsUp.Other_Team_Member_1__c
            AND parentid = :accsUp.id
        ];
        EntitySubscription followedOTTUP = [
            SELECT id, parentid, subscriberid, parent.name
            FROM EntitySubscription
            WHERE subscriberid = :accsUp.Other_Team_Member_2__c
            AND parentid = :accsUp.id
        ]; 
        
        system.assertequals(followedPMUP.parentid, accsUP.id);
        system.assertequals(followedPMUP.subscriberid, accsUP.Portfolio_Manager_Lookup__c);

        system.assertequals(followedTOUP.parentid, accsUP.id);
        system.assertequals(followedTOUP.subscriberid, accsUP.Trust_Officer__c);

        system.assertequals(followedWAUP.parentid, accsUP.id);
        system.assertequals(followedWAUP.subscriberid, accsUP.Wealth_Advisor__c);

        system.assertequals(followedOTUP.parentid, accsUP.id);
        system.assertequals(followedOTUP.subscriberid, accsUP.Other_Team_Member_1__c);
        
        system.assertequals(followedOTTUP.parentid, accsUP.id);
        system.assertequals(followedOTTUP.subscriberid, accsUP.Other_Team_Member_2__c);
    }
}