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
Uma PrabhakarUma Prabhakar 

How to resolve this error in the test class

Hello Salesforce Expertrs 

i am new to the salesforce and i am trying to write test class for the Content document trigger, So when partner user tries a Upload a document through files, automatically he gets email saying that document has been uploaded with the document link, the trigger was successful and with great difficulty i wrote a test class with 100% code coverage, but when i see the test result i see that there is erro "INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY" i have checked in the profile, as well as the OWD settings, still i was not able to resolve this issue, any help will be highly appreciated

User-added image
trigger ContentDocumentTrigger on ContentDocument (after insert) {
    static boolean firstRun = true; 
    if(Trigger.isInsert && Trigger.isAfter){
        for(ContentDocument cd: trigger.new){ 
            if(firstRun){
                if(cd.ownerid!=null){
                    List<user> userlist = [SELECT id,Profile.UserLicense.Name from user WHERE id=:cd.ownerid AND Profile.UserLicense.Name='Partner Community'];
                    if(!userlist.isempty()){
                        List<Partner_Upload_Email_Addresses__mdt> custmeta = [select id,Email_Addresses__c from Partner_Upload_Email_Addresses__mdt];
                        String[] toAddresses = new String[]{};
                            for(Partner_Upload_Email_Addresses__mdt custemailvales: custmeta){
                                toAddresses.add(custemailvales.Email_Addresses__c);
                            }
                        Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
                        email.setReplyTo('manjuzmail053@gmail.com');
                        email.setSenderDisplayName('Partner Upload File ');
                        //String[] toAddresses = new String[] {'manjuzmail053@gmail.com'};
                        if(!toAddresses.isempty()){
                            email.setToAddresses(toAddresses);
                            String body = '<html><body>Please note: A new document has been added\n \n ';
                            body += 'File name: '+ cd.title+'\n\n' ;
                            body += 'Created By: '+ cd.createdbyid+ '\n\n';
                            body += 'Created Date: '+ cd.CreatedDate+ '\n\n';
                            body += 'link to file: https://moengage--partnersf.lightning.force.com/lightning/r/ContentDocument/'+cd.id+'/view </body></html>';
                            email.setSubject('A new file is uploaded by your Partner with file name :  '+cd.Title);
                            email.setHtmlBody(body);
                            Messaging.sendEmail(new Messaging.SingleEmailmessage[] {email});
                            firstRun = false;
                        }
                    }
                }
            }
        }
    }
}

@isTest
public class ContentDocumentTestclass {
    public static testmethod void ContentDocumentInsertTest()
    {
        List<user> userlist = [SELECT id,Profile.UserLicense.Name,isactive from user WHERE Profile.UserLicense.Name='Partner Community' AND isactive=true];
        
        System.runAs(userlist[1]) {
            // The following code runs as user 'u' 
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId());

            ContentVersion contentVersionInsert = new ContentVersion(
                Title = 'Tes11t',
                PathOnClient = 'Test1.jpg',
                VersionData = Blob.valueOf('Test 11Content Data'),
                IsMajorVersion = true
            );
            insert contentVersionInsert;
            system.debug('User' + UserInfo.getUserName());
            List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        }
    }
}

 
Leanbridge TechnologiesLeanbridge Technologies
e have an immediate need to resolve the errors in the test class for a trigger.
The trigger, test class, and any supporting metadata is included. Edit the test class only to ensure the tests complete successfully, and ensure coverage is above 90%.
Below are the error messages we are seeing:
Test failure, method: Test_ZM_User_Sales_Org_Hierarchy_Handler.testUSOHierarchy 
-- System.DmlException: Delete failed. First exception on row 0 with id a12F0000 
002ItksIAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ZM_User_Sales_Org 
_Hierarchy_Trigger: execution of BeforeDelete 

 
AkshaySFDCAkshaySFDC
You can use real data in test class if really necessary by using @isTest(SeeAllData=true)
Uma PrabhakarUma Prabhakar
@Akshay

i tried,  its not working