3

In Field Service Lightning I have a Trigger on ServiceReport which sends an email with attachment coming from ContentVersion.

This is the Test class I have written.

@isTest

private class testCreateServiceReport{

     static testMethod void validateWO() {

     WorkOrder wo = new WorkOrder();

     wo.subject = 'Testing';

     insert wo;

     ServiceAppointment sa = new ServiceAppointment();

     sa.ParentRecordId = wo.Id;

     sa.EarliestStartTime = DateTime.now();

     sa.DueDate = DateTime.now();

     insert sa;

     List<ServiceReportLayout> srl = [Select Id FROM ServiceReportLayout];

     ServiceReport sr = new ServiceReport();

     sr.ParentId = sa.Id;

     sr.Template = srl[0].Id; //Need to check if there is a way to avoid srl[0] notation

     insert sr; //If I comment this the text result is pass. When I enable it I get too see the below mentioned error.

}

}

This is the error Methods defined as TestMethod do not support Web service callouts I know that we need to write Test Classes for code that involves web service callouts using setMock() but I dont understand in what way inserting ServiceReport is related to making a callout.

Any help on this will be really helpful.

2 Answers 2

5

Here is my theory.

From Salesforce docs here:

A service report is a PDF that summarizes a work order or service appointment and can be signed by customers and your team members. To control what shows up in your service reports, create service report templates.

So basically insertion of ServiceReport creates a PDF.

The way salesforce creates a pdf is via calling getContentAsPDF method.

Now from Winter 17, getContentAsPDF is treated as callout. As its a callout in test context one needs a mock.

The funny part is you cannot call getContentAsPDF in test context as mentioned here. So on top of my head, you wont be able to test insertion of ServiceReport

0

Was able to do this with help of this post:

https://developer.salesforce.com/forums/?id=9062I000000g9k2QAA

Account a = new Account(Name = 'testAccountSA',
                        BillingPostalCode = '83059',
                        BillingCountry = 'Deutschland');
    insert a; 

    Contact c = new Contact(FirstName = 'TESTOS',
                       LastName = 'TEstmann',
                       AccountId = a.id);
    insert c;

    Case cas = new Case (AccountId = a.Id,
                        STATUS = 'New',
                        Origin = 'Homepage',
                        created_by__c = 'bla');
        insert cas;

    WorkOrder WO = new WorkOrder (AccountId = a.Id,
                                 CaseId = cas.Id);
        insert WO;

    ServiceAppointment SA = new ServiceAppointment (Case__c = cas.Id,
                                                    ParentRecordId = WO.Id);
        insert SA;ContentVersion contentVersion = new ContentVersion(


Title = 'Penguins',
  PathOnClient = 'Penguins.jpg',
  VersionData = Blob.valueOf('Test Content'),
  IsMajorVersion = true
);
insert contentVersion;    
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

//create ContentDocumentLink  record 
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = WO.id;
cdl.ContentDocumentId = documents[0].Id;
cdl.shareType = 'V';
insert cdl;


ServiceReport sr = new ServiceReport();
sr.ContentVersionDocumentId = documents[0].Id ;
sr.DocumentBody = Blob.valueOf('Test Content') ; 
sr.DocumentContentType ='application/pdf';
sr.DocumentName='Test';
sr.ParentId = WO.Id ; 
insert sr ;
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .