• Deviprasad Hegde 30
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
APEX CLASS----
public class LGTN_eArkivInvoiceFiles_LCC {  
    @AuraEnabled 
    public static String getInvoiceContentDocument(String invoiceNumber){
        String contentDocumentId=null;
        String versionIdentifier='Invoice_'+invoiceNumber;
        List<ContentVersion> versions = [Select Id,ContentDocumentId from ContentVersion where eArkivDocumentIdentifier__c=:versionIdentifier];
        if(versions.isEmpty()){
            List< ApexCalloutLog__c > calloutlogsToUpsert = new List< ApexCalloutLog__c > ();
            LGTN_eArkivInvoiceFile_WS eArkiv_ws= new LGTN_eArkivInvoiceFile_WS (invoiceNumber,Label.LGTN_eArkiv_Invoice_IntegrationName,false);
            eArkiv_ws.setEndPoint(eArkiv_ws.endpoint+'='+invoiceNumber);
            eArkiv_ws.send(false);
            calloutlogsToUpsert.addAll(eArkiv_ws.callOutLogs);
            if(eArkiv_ws.callout.error == null) { 
                if(!UTIL_Apex.empty(eArkiv_ws.eArkivInvoiceFileLink)){
                    LGTN_eArkivInvoiceFile_WS eArkiv_File_ws= new LGTN_eArkivInvoiceFile_WS (invoiceNumber,Label.LGTN_eArkiv_Invoice_IntegrationName,true);
                    eArkiv_File_ws.setEndPoint(eArkiv_ws.eArkivInvoiceFileLink); 
                    eArkiv_File_ws.send(false);  
                    if(eArkiv_File_ws.callout.error == null) { 
                        contentDocumentId=eArkiv_File_ws.documentFileId; 
                    }
                    calloutlogsToUpsert.addAll(eArkiv_File_ws.callOutLogs);   
                }
            }
            if(!calloutlogsToUpsert.isEmpty())DataBase.upsert(calloutlogsToUpsert);
        }else{
            contentDocumentId=versions[0].ContentDocumentId;
        }
        return contentDocumentId;
    }
}


I HAVE TRIED THIS TEST CLASS AND GOT CODE CODE COVERAGE OF 71% BUT THE LINES WHICH I HAVE MARKED AS "BOLD" ARE NOT COVERING AND METHOD IS NOT PASSING.PLEASE HELP ME IN THIS.
TEST CLASS----
public class LGTN_eArkivInvoiceFiles_LCC_TEST {
    
    @isTest static void TestgetInvoiceContentDocument()
    {
        UTIL_TestFactory.setUpOrg();
        User tempUser4 = UTIL_TestFactory.createUserWithoutSetup('testsetupOrgUser'+DateTime.now().millisecond()+'@Test14.com','System Administrator');
        System.runAs(tempUser4){
            StaticResourceCalloutMock successfulMock = new StaticResourceCalloutMock();
            successfulMock.setStaticResource('TestInvoiceFiles');
            successfulMock.setStatusCode(200);
            successfulMock.setHeader('Content-Type', 'text/json');
            Test.startTest();
            Test.setMock(HttpCalloutMock.class, successfulMock); 
            LGTN_eArkivInvoiceFiles_LCC.getInvoiceContentDocument('Test');
            Test.stopTest();
            
        }
    }
}
I have a custom Auth. Provider and a couple of Named Credential which use it.
The auth. provider metadata requires "Execute As" field which is represented as username in metadata and as you know,
usernames are globally unique in Salesforce.

So the deployment will always fail, because user will never exist on another sandbox and at the same time, the username field cannot be left 
empty.

What is the proper approach in this case in source driven development?

For now, I will have to run a script which replaces username when new scratch org is created and when source is deployed to staging environments. It's obviously far from perfect solution, because my teamies will have to use custom script instead of sfdx force:org:create and because the username change will be visible in VCS every time someone creates a new scratch org.

I really hope someone from Salesforce DX development team will see this.