• Robert Adero
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Hello,
Please assist in the error with the test class. I have the below Apex Classes for Salesforce attachments and a test class;

AttachmentUtils
/**
 * AttachmentUtils Utility class for processing attachments
*/
public class AttachmentUtils {

    public static void saveAttachments(list<attachment> atts){
        
        Set<id>        assignmentIds = new Set<id>();
        Map<id, string> filenames     = new Map<id, string>();
        
        for(Attachment att: atts){

            //Get the SObject type
            String objectAPIName = att.parentId.getSObjectType().getDescribe().getName();
            
            if(objectAPIName == 'Assignment__c'){
                
                //Get all the attachments for this assignment and concatenate names into a string, then update the field
                assignmentIds.add(att.parentId);
                 
            } 
        }
        
        FilesUtils.updateAttachmentNames(assignmentIds);
        
    }
}

FilesUtils Class
/**
 * Utility class for processing files
*/
public class FilesUtils {

    //Update the Attachment_Names__c field on assignments
    public static void updateAttachmentNames(set<id> assignmentIds){
    
        Map<id, string> filenames     = new Map<id, string>();
        
        //Get all attachments for all affected assignments
        list<attachment> atts     =  [SELECT id, parentId, name
                                     FROM   attachment
                                     WHERE  parentId = : assignmentIds];
        
        for(Attachment att: atts){
            string existingFilenames = filenames.get(att.parentId);
           
            if (existingFilenames == null) existingFilenames = '';
                
            filenames.put(att.parentId, existingFilenames + ' '+ att.name);

        }
        
        List<id> contentDocIds = new List<id>();
        
        //Now get all files for all affected assignments       
        for (ContentDocumentLink cdl: [SELECT LinkedEntityId,ContentDocumentId, ContentDocument.Title FROM ContentDocumentLink WHERE LinkedEntityId in :assignmentIds]){
            
            string existingFilenames = filenames.get(cdl.LinkedEntityId);
            
            if (existingFilenames == null) existingFilenames = '';
                
            filenames.put(cdl.LinkedEntityId, existingFilenames + ' '+ cdl.ContentDocument.Title);
        }
        
        
        
        List<id> assIdsWithZeroAtts = new List<id>();
        
        //Check there are no Assignments with zero attachments now
        for(id assId: assignmentIds){
            if(filenames.containsKey(assId) == false){
                assIdsWithZeroAtts.add(assId);
            }
        }
        
        List<Assignment__c> asses = [SELECT id, Attachment_Names__c 
                                     FROM Assignment__c
                                     WHERE id = :assignmentIds];
        
        for (Assignment__c ass: asses){
            ass.Attachment_Names__c = filenames.get(ass.id);
        }
        
        List<Assignment__c> assesWithZeroAtts = [SELECT id, Attachment_Names__c 
                                     FROM Assignment__c
                                     WHERE id = :assIdsWithZeroAtts];
        
        for (Assignment__c ass: assesWithZeroAtts){
            ass.Attachment_Names__c = '';
        }
        
        update asses;
        update assesWithZeroAtts;

    }
}

AttachmentTrigger
 
trigger AttachmentTrigger on Attachment (after insert, after update, after delete) {

    //delegate to utility class
    //
    if(trigger.isDelete){
        AttachmentUtils.saveAttachments(trigger.old);
    }
    else
    {
        AttachmentUtils.saveAttachments(trigger.new);
    }
}


AttachmentUtilsTest
 
/**
 * Test class for attachmentUtils and filesUtils classes
*/
@isTest
public class AttachmentUtilsTest{

    static integer NUMBER_OF_FILES = 5;
    
    //this is a test setup method - will be automatically executed before each test method
    @testSetup static void createAssignmentandAttachments() {
        
        
        
        //Set up facility (account)
        Account fac = new Account();
        fac.name = 'new facility';
        fac.billingCity = 'Weymouth';
        fac.billingCountry = 'Iceland';
        insert fac;
        
        
        Assignment__c ast = new Assignment__c();
        ast.facility__c = fac.id;
        
        insert ast;
        
        //create and insert the attachments
        list<attachment> atts = new list<attachment>();
        
        for(integer i=0;i<NUMBER_OF_FILES;i++){
            attachment att = new attachment();
            att.parentId = ast.id;
            att.name = 'a';
            att.body = Blob.valueOf('Attachment Body');
            atts.add(att);
        }
        
        //this should cause the trigger to fire and concatenate attachment names in the Attachment_Name__c field
        //names should be 300 characters long (200 x with spaces in between, plus trailing space)
        insert atts;
        
        //create and insert files
        list<ContentVersion> contentVersions = new list<ContentVersion>();
        
        for(integer i=0;i<NUMBER_OF_FILES;i++){
            String filesString = 'Binary string of the files';

            ContentVersion conVer = new ContentVersion();
            conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
            conVer.PathOnClient = 'file.txt'; // The files name, extension is very important here which will help the file in preview.
            
            conVer.Title = 'c'; // Display name of the files
            conVer.VersionData = EncodingUtil.base64Decode(filesString); // converting your binary string to Blog
            contentVersions.add(conVer);
        
        }
               
        insert contentVersions;
        
        list<ContentDocumentLink> contentDocLinks = new list<ContentDocumentLink>();
        
        for(ContentVersion conVer: [SELECT contentDocumentId from ContentVersion WHERE Title = 'c']){
            //Create ContentDocumentLink
            ContentDocumentLink cDe = new ContentDocumentLink();
            cDe.ContentDocumentId = conVer.contentDocumentId;
            cDe.LinkedEntityId = ast.Id; // you can use objectId,GroupId etc
            cDe.ShareType = 'I'; // Inferred permission, checkout description of ContentDocumentLink object for more details
            cDe.Visibility = 'AllUsers';
            contentDocLinks.add(cDe);
        }
               
        insert contentDocLinks;
        
    }
    
    //test to see if trigger is correctly modifying field on assignment when 200 attachments are added
    public static testmethod void addAttachments(){
    
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM assignment__c][0].Attachment_Names__c;
                               
        //Each file name is 1 character long
        //And the 'attachment names' field contains each filename with a space in between
        //So length of Attachment Names should = number of files * 2 characters * 2 (attachments and content) - 1 space
        if(names!=null) system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1,names.length());
        
    }
    
    
    //test to see if trigger is correctly modifying field on assignment when one attachment name is changed
    public static testmethod void modifyAttachments(){
    
        //select an attachment and modify its name
        attachment att = [    SELECT id, name
                              FROM attachment
                              ][0];
        att.name = 'aaaa';
        
        contentVersion cv = [SELECT id, Title from ContentVersion][0];
        cv.Title = 'cccc';
        
        test.startTest();
        update att;
        update cv;
        test.stopTest();
        
        //test that names field has increased by 4
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM assignment__c][0].Attachment_Names__c;
                        
        
        //Increases by 6 characters total
        system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1 + 6,names.length());
        
    }
    
    //test to see if trigger is correctly modifying field on assignment when one attachment name is deleted
    public static testmethod void deleteAttachments(){
    
        //select an attachment and delete it
        attachment att = [    SELECT id
                              FROM attachment
                              ][0];
        
        
        test.startTest();
        delete att;
        test.stopTest();
        
        //test that names field is now decreased by 2
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM assignment__c][0].Attachment_Names__c;
                               
        system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1 - 2,names.length());
    }
    
}

Error is on Code Coverage Failure and test failure message as below;

Code Coverage Failure
Your code coverage is 0%. You need at least 75% coverage to complete this deployment.
AttachmentUtils
 
System.AssertException: Assertion Failed: Expected: 17, Actual: 19
Stack Trace: Class.AttachmentUtilsTest.deleteAttachments: line 139, column 1

​​​​​​​
Below are part of the Test Class Code which fails;
Method Name: deleteAttachments
Pass/Fail: Fail
Error Message: System.NullPointerException: Attempt to de-reference a null object
Stack Trace: Class.AttachmentUtilsTest.deleteAttachments: line 137, column 1

Class: AttachmentUtilsTest
Method: NamemodifyAttachments
Pass/Fail: Fail
Error Message: System.NullPointerException: Attempt to de-reference a null object
Stack Trace: Class.AttachmentUtilsTest.modifyAttachments: line 115, column 1

Please help, thanks.
/**
 * Test class for attachmentUtils and filesUtils classes
*/
@isTest
public class AttachmentUtilsTest{

    static integer NUMBER_OF_FILES = 5;
    
    //this is a test setup method - will be automatically executed before each test method
    @testSetup static void createAgriFiReportandAttachments() {
        
        
        
        //Set up AgriFi Application (record)
        AgriFI_Application__c App =new AgriFI_Application__c();
        //App.Name = 'new AgriFI Reference()';
        //App.LeadBusinessName = 'Weymouth';
        //App.LeadContactSurname = 'Ken';
        insert App;
        
        
        AgriFI_Report__c ast = new AgriFI_Report__c();
        ast.AgriFI_Application__c = App.id;
        
        insert ast;
        
        //create and insert the attachments
        list<attachment> atts = new list<attachment>();
        
        for(integer i=0;i<NUMBER_OF_FILES;i++){
            attachment att = new attachment();
            att.parentId = ast.id;
            att.name = 'a';
            att.body = Blob.valueOf('Attachment Body');
            atts.add(att);
        }
        
        //this should cause the trigger to fire and concatenate attachment names in the Attachment_Name__c field
        //names should be 300 characters long (200 x with spaces in between, plus trailing space)
        insert atts;
        
        //create and insert files
        list<ContentVersion> contentVersions = new list<ContentVersion>();
        
        for(integer i=0;i<NUMBER_OF_FILES;i++){
            String filesString = 'Binary string of the files';

            ContentVersion conVer = new ContentVersion();
            conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
            conVer.PathOnClient = 'file.txt'; // The files name, extension is very important here which will help the file in preview.
            
            conVer.Title = 'c'; // Display name of the files
            conVer.VersionData = EncodingUtil.base64Decode(filesString); // converting your binary string to Blog
            contentVersions.add(conVer);
        
        }
               
        insert contentVersions;
        
        list<ContentDocumentLink> contentDocLinks = new list<ContentDocumentLink>();
        
        for(ContentVersion conVer: [SELECT contentDocumentId from ContentVersion WHERE Title = 'c']){
            //Create ContentDocumentLink
            ContentDocumentLink cDe = new ContentDocumentLink();
            cDe.ContentDocumentId = conVer.contentDocumentId;
            cDe.LinkedEntityId = ast.Id; // you can use objectId,GroupId etc
            cDe.ShareType = 'I'; // Inferred permission, checkout description of ContentDocumentLink object for more details
            cDe.Visibility = 'AllUsers';
            contentDocLinks.add(cDe);
        }
               
        insert contentDocLinks;
        
    }
    
    //test to see if trigger is correctly modifying field on assignment when 200 attachments are added
    public static testmethod void addAttachments(){
    
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM AgriFI_Report__c][0].Attachment_Names__c;
                               
        //Each file name is 1 character long
        //And the 'attachment names' field contains each filename with a space in between
        //So length of Attachment Names should = number of files * 2 characters * 2 (attachments and content) - 1 space
        if(names!=null) system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1,names.length());
        
    }
    
    
    //test to see if trigger is correctly modifying field on assignment when one attachment name is changed
    public static testmethod void modifyAttachments(){
    
        //select an attachment and modify its name
        attachment att = [    SELECT id, name
                              FROM attachment
                              ][0];
        att.name = 'aaaa';
        
        contentVersion cv = [SELECT id, Title from ContentVersion][0];
        cv.Title = 'cccc';
        
        test.startTest();
        update att;
        update cv;
        test.stopTest();
        
        //test that names field has increased by 4
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM AgriFI_Report__c][0].Attachment_Names__c;
                        
        
        //Increases by 6 characters total
        system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1 + 6,names.length());
        
    }
    
    //test to see if trigger is correctly modifying field on assignment when one attachment name is deleted
    public static testmethod void deleteAttachments(){
    
        //select an attachment and delete it
        attachment att = [    SELECT id
                              FROM attachment
                              ][0];
        
        
        test.startTest();
        delete att;
        test.stopTest();
        
        //test that names field is now decreased by 2
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM AgriFI_Report__c][0].Attachment_Names__c;
                               
        system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1 - 2,names.length());
    }
    
}



 
I have a formula field on Record A that updates field 1 from  Record B but I would like it to also include a formula that will enable field 1 (or additional field lets say field 2) on Record A to be active and editable when Null Value is shown
Hello,
Please assist in the error with the test class. I have the below Apex Classes for Salesforce attachments and a test class;

AttachmentUtils
/**
 * AttachmentUtils Utility class for processing attachments
*/
public class AttachmentUtils {

    public static void saveAttachments(list<attachment> atts){
        
        Set<id>        assignmentIds = new Set<id>();
        Map<id, string> filenames     = new Map<id, string>();
        
        for(Attachment att: atts){

            //Get the SObject type
            String objectAPIName = att.parentId.getSObjectType().getDescribe().getName();
            
            if(objectAPIName == 'Assignment__c'){
                
                //Get all the attachments for this assignment and concatenate names into a string, then update the field
                assignmentIds.add(att.parentId);
                 
            } 
        }
        
        FilesUtils.updateAttachmentNames(assignmentIds);
        
    }
}

FilesUtils Class
/**
 * Utility class for processing files
*/
public class FilesUtils {

    //Update the Attachment_Names__c field on assignments
    public static void updateAttachmentNames(set<id> assignmentIds){
    
        Map<id, string> filenames     = new Map<id, string>();
        
        //Get all attachments for all affected assignments
        list<attachment> atts     =  [SELECT id, parentId, name
                                     FROM   attachment
                                     WHERE  parentId = : assignmentIds];
        
        for(Attachment att: atts){
            string existingFilenames = filenames.get(att.parentId);
           
            if (existingFilenames == null) existingFilenames = '';
                
            filenames.put(att.parentId, existingFilenames + ' '+ att.name);

        }
        
        List<id> contentDocIds = new List<id>();
        
        //Now get all files for all affected assignments       
        for (ContentDocumentLink cdl: [SELECT LinkedEntityId,ContentDocumentId, ContentDocument.Title FROM ContentDocumentLink WHERE LinkedEntityId in :assignmentIds]){
            
            string existingFilenames = filenames.get(cdl.LinkedEntityId);
            
            if (existingFilenames == null) existingFilenames = '';
                
            filenames.put(cdl.LinkedEntityId, existingFilenames + ' '+ cdl.ContentDocument.Title);
        }
        
        
        
        List<id> assIdsWithZeroAtts = new List<id>();
        
        //Check there are no Assignments with zero attachments now
        for(id assId: assignmentIds){
            if(filenames.containsKey(assId) == false){
                assIdsWithZeroAtts.add(assId);
            }
        }
        
        List<Assignment__c> asses = [SELECT id, Attachment_Names__c 
                                     FROM Assignment__c
                                     WHERE id = :assignmentIds];
        
        for (Assignment__c ass: asses){
            ass.Attachment_Names__c = filenames.get(ass.id);
        }
        
        List<Assignment__c> assesWithZeroAtts = [SELECT id, Attachment_Names__c 
                                     FROM Assignment__c
                                     WHERE id = :assIdsWithZeroAtts];
        
        for (Assignment__c ass: assesWithZeroAtts){
            ass.Attachment_Names__c = '';
        }
        
        update asses;
        update assesWithZeroAtts;

    }
}

AttachmentTrigger
 
trigger AttachmentTrigger on Attachment (after insert, after update, after delete) {

    //delegate to utility class
    //
    if(trigger.isDelete){
        AttachmentUtils.saveAttachments(trigger.old);
    }
    else
    {
        AttachmentUtils.saveAttachments(trigger.new);
    }
}


AttachmentUtilsTest
 
/**
 * Test class for attachmentUtils and filesUtils classes
*/
@isTest
public class AttachmentUtilsTest{

    static integer NUMBER_OF_FILES = 5;
    
    //this is a test setup method - will be automatically executed before each test method
    @testSetup static void createAssignmentandAttachments() {
        
        
        
        //Set up facility (account)
        Account fac = new Account();
        fac.name = 'new facility';
        fac.billingCity = 'Weymouth';
        fac.billingCountry = 'Iceland';
        insert fac;
        
        
        Assignment__c ast = new Assignment__c();
        ast.facility__c = fac.id;
        
        insert ast;
        
        //create and insert the attachments
        list<attachment> atts = new list<attachment>();
        
        for(integer i=0;i<NUMBER_OF_FILES;i++){
            attachment att = new attachment();
            att.parentId = ast.id;
            att.name = 'a';
            att.body = Blob.valueOf('Attachment Body');
            atts.add(att);
        }
        
        //this should cause the trigger to fire and concatenate attachment names in the Attachment_Name__c field
        //names should be 300 characters long (200 x with spaces in between, plus trailing space)
        insert atts;
        
        //create and insert files
        list<ContentVersion> contentVersions = new list<ContentVersion>();
        
        for(integer i=0;i<NUMBER_OF_FILES;i++){
            String filesString = 'Binary string of the files';

            ContentVersion conVer = new ContentVersion();
            conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
            conVer.PathOnClient = 'file.txt'; // The files name, extension is very important here which will help the file in preview.
            
            conVer.Title = 'c'; // Display name of the files
            conVer.VersionData = EncodingUtil.base64Decode(filesString); // converting your binary string to Blog
            contentVersions.add(conVer);
        
        }
               
        insert contentVersions;
        
        list<ContentDocumentLink> contentDocLinks = new list<ContentDocumentLink>();
        
        for(ContentVersion conVer: [SELECT contentDocumentId from ContentVersion WHERE Title = 'c']){
            //Create ContentDocumentLink
            ContentDocumentLink cDe = new ContentDocumentLink();
            cDe.ContentDocumentId = conVer.contentDocumentId;
            cDe.LinkedEntityId = ast.Id; // you can use objectId,GroupId etc
            cDe.ShareType = 'I'; // Inferred permission, checkout description of ContentDocumentLink object for more details
            cDe.Visibility = 'AllUsers';
            contentDocLinks.add(cDe);
        }
               
        insert contentDocLinks;
        
    }
    
    //test to see if trigger is correctly modifying field on assignment when 200 attachments are added
    public static testmethod void addAttachments(){
    
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM assignment__c][0].Attachment_Names__c;
                               
        //Each file name is 1 character long
        //And the 'attachment names' field contains each filename with a space in between
        //So length of Attachment Names should = number of files * 2 characters * 2 (attachments and content) - 1 space
        if(names!=null) system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1,names.length());
        
    }
    
    
    //test to see if trigger is correctly modifying field on assignment when one attachment name is changed
    public static testmethod void modifyAttachments(){
    
        //select an attachment and modify its name
        attachment att = [    SELECT id, name
                              FROM attachment
                              ][0];
        att.name = 'aaaa';
        
        contentVersion cv = [SELECT id, Title from ContentVersion][0];
        cv.Title = 'cccc';
        
        test.startTest();
        update att;
        update cv;
        test.stopTest();
        
        //test that names field has increased by 4
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM assignment__c][0].Attachment_Names__c;
                        
        
        //Increases by 6 characters total
        system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1 + 6,names.length());
        
    }
    
    //test to see if trigger is correctly modifying field on assignment when one attachment name is deleted
    public static testmethod void deleteAttachments(){
    
        //select an attachment and delete it
        attachment att = [    SELECT id
                              FROM attachment
                              ][0];
        
        
        test.startTest();
        delete att;
        test.stopTest();
        
        //test that names field is now decreased by 2
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM assignment__c][0].Attachment_Names__c;
                               
        system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1 - 2,names.length());
    }
    
}

Error is on Code Coverage Failure and test failure message as below;

Code Coverage Failure
Your code coverage is 0%. You need at least 75% coverage to complete this deployment.
AttachmentUtils
 
System.AssertException: Assertion Failed: Expected: 17, Actual: 19
Stack Trace: Class.AttachmentUtilsTest.deleteAttachments: line 139, column 1

​​​​​​​
I have a formula field on Record A that updates field 1 from  Record B but I would like it to also include a formula that will enable field 1 (or additional field lets say field 2) on Record A to be active and editable when Null Value is shown