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
Anil_SinghAnil_Singh 

"Create Service Report" FSL

In Field Service Lightning (FSL) - Can I apply condition in "Create Service Report" button. I need this button to work based on condition.
When I click this button it generates a pdf view and save this Report in Related List of "Service Report."

Also can we have a custom code before this button is Clicked.

Thanks 
Anil Singh
sfdcBahusfdcBahu
You cannot add validation before the button is clicked.

But There are 3 ways to do it.

1. You can write before Insert trigger on Service Report Object and add your validations in the trigger using .adderror method.

2.  I believe you would generate service report only once the work is completed. if yes - You can use two different record types. once the work is completed and all the validations are done, the user would set the status to 'Completed' which will flip the Record type to a X_completed. You can show the Create Service Report only in the pagelayout for the Completed Record type. 

3. you can write whole new custom lightning component, use whatever custom logic needed and then genrate the Service Report from the component programatically. reference to generate service report from apex http://www.salesforceenthusiast.com/Pages/code9.php



 
Evgeniya Zaneva 19Evgeniya Zaneva 19
@sfdcBahu this is not possible with the first option, we can not overwrite the Standard error message even with the apex .addError method.
User-added image
The trigger is before and the code is as follows, but still not possible to different error message.
public static void preventServiceReportCreation(List < ServiceReport > newReports) {

        Map < Id,Id > serviceAppointmentsToServiceReportMap = getServiceAppointmentsToServiceReportMap(newReports);
        // <AppointmentId, Appointment>
        Map < Id,ServiceAppointment > serviceAppointmentsMap = getServiceAppointments(serviceAppointmentsToServiceReportMap.keySet());
        System.debug('serviceappointment'+serviceAppointmentsMap.values());
        Set<Id> workORderSet = new Set<Id>(); 
        
        for (ServiceReport serviceReport : newReports) {
            ServiceAppointment relatedAppointment = serviceAppointmentsMap.get(serviceReport.ParentId);
            if (relatedAppointment.ParentRecord instanceOf WorkOrder) {
             WorkOrder workOrder = relatedAppointment.ParentRecord;
             workORderSet.add(workOrder.Id);
            }
        }
        List<ProductConsumed> productConsumed = IV_DMProductConsumed.getProductConsumed(workORderSet);
        System.debug('productconsumed'+productConsumed);
        apexpages.message newmessage;
        if (productConsumed.size()>0) {
            for (ServiceAppointment serviceReport : serviceAppointmentsMap.values()) {
            //newmessage = new apexpages.message(apexpages.severity.ERROR,'You are not allowed to create a Service Report if the Product is Batch Managed');
              serviceReport.addError('You are not allowed to create a Service Report if the Product is Batch Managed');   
            }         
        }
        //System.debug('workorder'+workOrder);
    }