Hybris / SAP Commerce Cloud Groovy Scripting Job to Generate CSV/Excel Reports and copy to Commerce cloud Blob Storage

Below are the steps to create scripting Cronjob from Back office:

1. Create Script of type Groovy

2. Create Scripting Job.

3. Create Cronjob.

refer some already exist or OOB scripting job e.g. evalCustomRefundAmount,evalOrigRefundAmount to get more details 


Example Groovy Script:

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;

import java.text.SimpleDateFormat;

flexibleSearchService = spring.getBean 'flexibleSearchService';

//put here the spring  bean specific to your project/implementation which reference to blob storage

dataContainer =  spring.getBean 'dataContainer';

def fileContent = new StringBuilder();

//File header

def FILE_HEADER = 'InvoiceNum,ActNum,Status';

def NEW_LINE_SEPARATOR = '\n';

def COMMA_DELIMITER = ',';

fileContent.append(FILE_HEADER);

fileContent.append(NEW_LINE_SEPARATOR);

//File Name format and location in Azure Blob Storage

def date = new Date();

def sdf = new SimpleDateFormat('MM_dd_yyyy_HH:mm')

def fileName = 'marketingReports/' + 'Invoices_'  + sdf.format(date) + '.csv';

// Flexi Query

def fq = new FlexibleSearchQuery("select  {inv.invoicenumber},{act.accountnumber},{st.code} from { invoice as inv JOIN bdiaccount as act ON {inv.account}={act.pk} JOIN Invoicestatus as st ON {st.pk}={inv.status}} where {st.code} ='PAST_DUE' OR {st.code} ='UNPAID'" );

fq.setResultClassList(Arrays.asList(String.class,String.class,String.class))

def resultset = flexibleSearchService.search(fq).result;

println 'Total no. of resultset: ' + resultset.size();

for (final List<Object> row : resultset){

    fileContent.append(row.get(0));

    fileContent.append(COMMA_DELIMITER);

    fileContent.append(row.get(1));

    fileContent.append(COMMA_DELIMITER);

fileContent.append(row.get(2));

    fileContent.append(COMMA_DELIMITER);

    fileContent.append(NEW_LINE_SEPARATOR);


}

//writing file to Blob storage

dataContainer.write(fileContent.toString(), fileName , 'text/csv; charset=utf-8');


println 'Report generated successfully';








Comments

Popular posts from this blog

Emma's dream - a kids story - By Kavya

Hybris/ SAP commerce Cloud: Retry failed/not sent emails due to SMTP connection issue.