0

Let's say we've got formula field on Account like this

IF (IsPersonAccount , CASESAFEID( PersonContactId ) ,"")

Is it possible to create somehow an Account record with a particular PersonContactId inside @TestSetup class? I know we are able to apply

RecordTypeID = [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id,

for a new created Account but there is a need to cover this code

 Account[] personAccountIds = [SELECT Personcontact_Id__c FROM  account WHERE id = :accountId];
        if(personAccountIds.size() == 0) return null;
        Id personAccountId = personAccountIds[0].Personcontact_Id__c;
        String token = getToken();

of cuz i can provide a null value but need to cover non-empty as well

Actually we need to get person contact id to retrieve data from an external service. It wasnt my decision to compose a formula field named Personcontact_Id__c to store this id. So question is - how can i test my soql with positive PersonContactId field

UPDATED In customer's org there are both Business and Person Account. Prior to me they decided to include new formula field named Personcontact_Id with formula

IF (IsPersonAccount , CASESAFEID( PersonContactId ) ,"")

Here is an APEX side method

@AuraEnabled(cacheable=true)
public static String getNext(Integer page, Integer pageSize, String accountId){
    try {
        Account[] personAccountIds = [SELECT Personcontact_Id__c FROM  account WHERE id = :accountId];
        if(personAccountIds.size() == 0) return null;
        Id personAccountId = personAccountIds[0].Personcontact_Id__c;
        String token = getToken();
        HttpResponse response = new MCCallout('MCCredentialGetData')
            .getHistory(new PayloadWrapper(token, personAccountId));
        return(String.valueOf(response.getBody()));    
    } catch (Exception e) {
        throw new AuraHandledException(e.getMessage());
    }
}

There is need to compose test class. My attempt covers the only empty (false positive test)

 @IsTest
static void getNextTestFalsePositive() {
    Assert.isNull(CXDLazyLoadingController.getNext(0, 0, '0012z00000TjF2bFFFF'));
}

So my question is how to create (or convert contact to Person Account) to be able to provide PersonContactId to test class

2
  • 1
    There's not enough context in this question to answer it concisely. It's unclear why you need PersonContact_Id__c to begin with, why you need to query it, why you need a specific value, and so on. At minimum, I'd recommend including the significant part of the method you're trying to cover and the unit test logic you've written so far. It's better to have too much code in a question than too little. Please edit your question to include more code, and an explanation of what this code is doing.
    – sfdcfox
    Nov 19 at 17:41
  • I see you made an edit, but it's still not enough. We need to see more code or a better explanation. For this question, I'd expect to see 20-30 lines of code, minimum, to provide the context we need.
    – sfdcfox
    Nov 19 at 21:14

0

You must log in to answer this question.

Browse other questions tagged .