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.


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.


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


Was able to do this with help of this post:


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.ParentId = WO.Id ; 
insert sr ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.