Activity Forums Salesforce® Discussions How can I test this class in Salesforce?

  • How can I test this class in Salesforce?

    Posted by Prachi on November 12, 2019 at 12:17 pm

    I am creating a new Content Link on Service Appointment inserting from the parent work order and I am struggling on how I would write a test against this.

    trigger LinkWorkOrderContentToServiceAppointment on ServiceAppointment (after insert) {

    List<ContentDocumentLink> CDLtoUpdate = new List<ContentDocumentLink>();
    Set<ID> TicketIDs = new Set<ID>();

    for (ServiceAppointment SA : Trigger.new) {
    TicketIDs.add(SA.Master_Ticket__c);
    }

    List<ContentDocumentLink> ContentDocumentLinks = new List<ContentDocumentLink>([select id,ContentDocumentId,LinkedEntityId from ContentDocumentLink where LinkedEntityId in:TicketIDs]);

    for (ContentDocumentLink CDL : ContentDocumentLinks){
    for (ServiceAppointment SA : Trigger.new){
    if(CDL.LinkedEntityId == SA.Master_Ticket__c){
    if(CDLtoUpdate.size()>199){
    insert CDLtoUpdate;
    CDLtoUpdate.clear();
    }
    ContentDocumentLink NCDL = new ContentDocumentLink(ContentDocumentId = CDL.ContentDocumentId, LinkedEntityId = SA.Id, ShareType ='V', Visibility = 'AllUsers');
    CDLtoUpdate.add(NCDL);
    }
    }
    }
    if(CDLtoUpdate.size() > 0){
    insert CDLtoUpdate;
    CDLtoUpdate.clear();
    }

    }

    Yogesh replied 4 years, 5 months ago 2 Members · 1 Reply
  • 1 Reply
  • Yogesh

    Member
    November 12, 2019 at 3:33 pm

    Hello,

    simply insert  instances of a contentVersion and  a  contentDocumentLink in a test class .

    public class fileViewerCtrl {

    @AuraEnabled
    public static string insertDocument(string parentId, string dataContent) {
    ContentVersion firstVersion = new ContentVersion();
    firstVersion.versionData = blob.valueOf(dataContent);
    firstVersion.title = parentId;
    firstVersion.pathOnClient ='/testPath.txt';
    insert firstVersion;

    ContentVersion fetchFirstVersion = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: firstVersion.Id];
    ContentDocumentLink firstDocLink = new ContentDocumentLink();
    firstDocLink.ContentDocumentId = fetchFirstVersion.ContentDocumentId;
    firstDocLink.LinkedEntityId = parentId;
    firstDocLink.ShareType = 'V';
    insert firstDocLink;
    string file = fetchContentDocument(fetchFirstVersion.ContentDocumentId);
    system.debug('test'+file);
    return file;
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos