Related List dosent appear in Export to PDF

Idan Ben Yehuda
Tera Contributor

Hello.

I created a Business Rule that export the form to pdf automatically and attach it to the form.

However my Pricing Speciation Related List doesn't appear in the PDF.

 

What is the problem?

 

Thank you.

1 ACCEPTED SOLUTION

Tushar
Kilo Sage
Kilo Sage

Hi @Idan Ben Yehuda 

 

Please try below code -

(function executeRule(current, previous /*null when async*/) {
    // check if related lists have loaded
    if (current.getValue('loaded_related_lists') == 'true') {
        // Generate PDF and attach
        generateAndAttachPDF(current);
    } else {
        // a delay and then set 'loaded_related_lists' to 'true'
        gs.sleep(5000); // Adjust the sleep time as needed
        current.loaded_related_lists = 'true';
        current.update();
        
        // generate PDF and attach
        generateAndAttachPDF(current);
    }
})(current, previous);

function generateAndAttachPDF(current) {
    var rm = new sn_ws.RESTMessageV2();
    rm.setHttpMethod('GET');
    var url = gs.getProperty("glide.servlet.uri") + current.getTableName() + '.do?PDF&sys_id=' + current.sys_id;
    rm.setEndpoint(url);
    rm.setBasicAuth('idanby@bynet.co.il', '1234');
    
    // Execute the REST message
    var response = rm.execute();

    // Check if the response was successful before attaching
    if (response.getStatusCode() == 200) {
        var attachment = new GlideSysAttachment();
        attachment.addAttachment(current, current.getTableName(), current.sys_id, current.number + '.pdf', 'application/pdf', response.getBody());
    } else {
        gs.error('PDF generation failed. Status Code: ' + response.getStatusCode());
    }
}

 

I think you can also try using a Scheduled Job to run the PDF generation and attachment creation asynchronously, giving the system more time to prepare the data.

 

Thanks,

Tushar

View solution in original post

1 REPLY 1

Tushar
Kilo Sage
Kilo Sage

Hi @Idan Ben Yehuda 

 

Please try below code -

(function executeRule(current, previous /*null when async*/) {
    // check if related lists have loaded
    if (current.getValue('loaded_related_lists') == 'true') {
        // Generate PDF and attach
        generateAndAttachPDF(current);
    } else {
        // a delay and then set 'loaded_related_lists' to 'true'
        gs.sleep(5000); // Adjust the sleep time as needed
        current.loaded_related_lists = 'true';
        current.update();
        
        // generate PDF and attach
        generateAndAttachPDF(current);
    }
})(current, previous);

function generateAndAttachPDF(current) {
    var rm = new sn_ws.RESTMessageV2();
    rm.setHttpMethod('GET');
    var url = gs.getProperty("glide.servlet.uri") + current.getTableName() + '.do?PDF&sys_id=' + current.sys_id;
    rm.setEndpoint(url);
    rm.setBasicAuth('idanby@bynet.co.il', '1234');
    
    // Execute the REST message
    var response = rm.execute();

    // Check if the response was successful before attaching
    if (response.getStatusCode() == 200) {
        var attachment = new GlideSysAttachment();
        attachment.addAttachment(current, current.getTableName(), current.sys_id, current.number + '.pdf', 'application/pdf', response.getBody());
    } else {
        gs.error('PDF generation failed. Status Code: ' + response.getStatusCode());
    }
}

 

I think you can also try using a Scheduled Job to run the PDF generation and attachment creation asynchronously, giving the system more time to prepare the data.

 

Thanks,

Tushar