• Ioannis Sideris 6
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

Hello everyone, 

I am a fresh new developer and recently I wrote my first method in a class of an existing project. I need to write also a unit test for this class but I am getting an error and since I just recently started writind code in general I am struggling to deal with the issue. Any help would be much appreciated. 

Below is my method: 

// update the accounts of Type 'Other'PEM with the PEM of their related 'Constituent' Account
    public static void updateRelatedAccountPEM(Set<String> setAccountId) {
    // query all relevant relationships, where the related person is account type 'Other' and person is 'Constituent'
         Map<Id,Account> accountMap = new Map<Id,Account>();
            for (Relationship__c relationship : [
                SELECT Id, Person__c, Related_Person__c, Person__r.OwnerId, Related_Person__r.OwnerId
                FROM Relationship__c
                WHERE Person__r.RecordType.DeveloperName = :Utils_Constants.ACCOUNT_CONSTITUENT_RT
                AND Related_Person__r.RecordType.DeveloperName = :Utils_Constants.ACCOUNT_OTHER_RT
                AND (Person__c IN :setAccountId
                OR Related_Person__c IN :setAccountId)
            ]) {
                if(relationship.Person__r.OwnerId != relationship.Related_Person__r.OwnerId) {
                   accountMap.put(
                    relationship.Related_Person__c,
                    new Account(Id = relationship.Related_Person__c, OwnerId = relationship.Person__r.OwnerID)
                    );
                }
            }
            if (!accountMap.isEmpty()) {
                Database.SaveResult[] result = Database.update(accountMap.values(), false);
                Utils_Methods.writeErrorLog(
                    accountMap.values(),
                    result,
                    'AccountService.updateRelatedAccountPEM'
                );
            }
     }

For Data Creation I use the following method: 

global static final TestDataFactory.Template PERSONAL_RELATIONSHIP = new TestDataFactory.Template(
        Relationship__c.SObjectType,
        new Map<SObjectField, Object>{
            Relationship__c.Name => 'testRelationship {!index}',
            Relationship__c.Person__c => AccountTestDataTemplates.CONSTITUENT,
            Relationship__c.Related_Person__c => AccountTestDataTemplates.OTHER,
            Relationship__c.RecordTypeId => Utils_Constants.RELATIONSHIP_PERSONAL_RT
        }
    );

My Unit Test is:

@IsTest
public class updatePEMTest {
    @IsTest
    public static void testUpdateRelatedAccountPEM() {
        // Create test data using TestDataFactory
        TestDataFactory factory = new TestDataFactory();
        List<Relationship__c> relationships = (List<Relationship__c>)factory.create(AccountTestDataTemplates.PERSONAL_RELATIONSHIP);
        
               
        // Call the method being tested
        Set<String> setAccountId = new Set<String>();
        for (Relationship__c relationship : relationships) {
            setAccountId.add(relationship.Person__c);
            setAccountId.add(relationship.Related_Person__c);
        }
        
        Test.startTest();
        AccountService.updateRelatedAccountPEM(setAccountId);
        Test.stopTest();
        
        // Verify the results
        Map<Id, Account> updatedAccounts = new Map<Id, Account>([SELECT Id, OwnerId FROM Account WHERE Id IN :setAccountId]);
        for (Relationship__c relationship : relationships) {
            Account relatedAccount = updatedAccounts.get(relationship.Related_Person__c);
            System.assertEquals(relationship.Person__r.OwnerId, relationship.Related_Person__r.OwnerId);
        }
    }
}


The error I receive on the Unit Test is: 

Incompatible types since an instance of SObject is never an instance of List<Relationship__c>

Any feedback/help would be much appreciated!