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
Laurie DrewLaurie Drew 

More help with a test class

Hello,

I am having issue getting 100% code coverage on the following function:

@AuraEnabled
    public static string grabDetails(){
        User toReturn = [SELECT Id, ContactId,FirstName, LastName FROM User WHERE Id = :UserInfo.getUserId() LIMIT 1];
        Contact communityContact = [Select id,AccountId from contact where id = :toReturn.ContactId];
        // DML operation to save account Details
        Account a = [Select id,name,phone,website,billingStreet,billingCity,billingState,billingpostalcode,billingcountry,FEIN__c,
                     BusinessOrganization__c, progress__c from Account where id =:communityContact.AccountId];
        
        // DML operation to save account Details
        AttachmentStatus ss = new AttachmentStatus();
        
        try
        {
            List<attachment> attchs =[SELECT name FROM Attachment WHERE ParentID=: a.id]; 
            
            for(Attachment at : attchs)
            {
                if(at.name.contains('Contract'))
                {
                    ss.doesRequired1Exist = true;
                    ss.attachment1 = at.name;
                }
                else  if(at.name.contains('Liability'))
                {
                    ss.doesRequired2Exist = true;
                    ss.attachment2 = at.name;
                }
                
            }
            ss.SetDefaults();
        }
        catch(exception e){}
        
        ss.CustomerAccount = a;
        System.debug(a);
        return JSON.serializePretty(ss);
        
    }

Here is my test class thus far:

static testmethod void grabDetailsTest()
    {
        Profile p=[select id from profile where name='Contracting Owner'];
        User toReturn = new User(alias='test123', email='test123@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
        
        Contact c= new Contact(LastName='Test');
        
        String pdfContent = 'This is a test string';
        Account a = new Account(Name='Contract',billingStreet='123 Test Street',billingCity='Springfield',billingpostalcode='12345',billingcountry='US',FEIN__c='123456');
        insert a;
               
        
           Attachment attachmentPDF = new Attachment();
        attachmentPdf.parentId = a.id;
        attachmentPdf.name = a.name + '.pdf';
        attachmentPdf.body = blob.toPDF(pdfContent);
        insert attachmentPDF;
        
        test.startTest();
        
        SaveUpdateAccountDetails.grabDetails();
        
        test.stopTest();
    }

I would really appreciate any help that can be provided on this, thank you in advance.
Best Answer chosen by Laurie Drew
Сергей Жугин 7Сергей Жугин 7
Hi, I quickly looked through the code, and I have two questions for you: why you didn t insert user in grabDetailsTest() and why you created Contact? Maybe If you insert user your test getting 100% code coverage.

All Answers

Сергей Жугин 7Сергей Жугин 7
Hi, I quickly looked through the code, and I have two questions for you: why you didn t insert user in grabDetailsTest() and why you created Contact? Maybe If you insert user your test getting 100% code coverage.
This was selected as the best answer
Laurie DrewLaurie Drew
I updated my code to include an insert for the user and receive error System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: []
So I added a role to the the user that I am setting as the account owner, no go.
Then I verified that the user I am running the test as has a profile assigned to it, same error.
Then I added System.RunAs(u) to run as the user I create in the test class, still same error.

Here is my updated code:

static testmethod void grabDetailsTest()
    {
        Profile p=[select id from profile where name='Contracting Owner'];
        UserRole r = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
        User pau = new User(alias='portacc', email='portacc@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = [SELECT Id FROM Profile WHERE Name ='System Administrator'].Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='portacc@noemail.com',UserRoleId=r.Id);
        insert pau;
        Account a = new Account(Name='Contract',billingStreet='123 Test Street',billingCity='Springfield',billingpostalcode='12345',billingcountry='US',FEIN__c='123456',OwnerId=pau.Id);
        insert a;
        Contact c= new Contact(LastName='Test', Account=a);
        insert c;
        
        User u = new User(alias='test123', email='test123@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test123@noemail.com', ContactId=c.Id,UserRoleId=r.Id);
        insert u;
        
        
        String pdfContent = 'This is a test string';
       
        Attachment attachmentPDF = new Attachment();
        attachmentPdf.parentId = a.id;
        attachmentPdf.name = a.name + '.pdf';
        attachmentPdf.body = blob.toPDF(pdfContent);
        insert attachmentPDF;
        
        System.RunAs(u){
        
        test.startTest();
        
        SaveUpdateAccountDetails.grabDetails();
        
        test.stopTest();
        }      
    }

Can anyone please tell me what it is I am missing on this, growing very confused... Thank you in advance!