Skip to main content

JavaScript SDK Samples - Bulk Read Operations

Create a Bulk Read Job
Get Bulk Read Job Details
              
              
class Bulkread
{
    /**
     *  Get BulkRead Job Details
     * This method is used to get the details of a bulk read job performed previously.
     * @param {BigInt} jobId The unique ID of the bulk read job.
     */
    static async getBulkReadJobDetails(jobId) {
        //example
        // let jobId = 34096432461001n;

        //Get instance of BulkReadOperations Class
        let bulkReadOperations = new ZCRM.BulkRead.Operations();

        //Call getBulkReadJobDetails method that takes jobId as parameter
        let response = await bulkReadOperations.getBulkReadJobDetails(jobId);

        if (response != null) {
            //Get the status code from response
            console.log("Status Code: " + response.getStatusCode());

            if ([204, 304].includes(response.getStatusCode())) {
                console.log(response.getStatusCode() == 204 ? "No Content" : "Not Modified");

                return;
            }

            //Get object from response
            let responseObject = response.getObject();

            if (responseObject != null) {
                //Check if expected ResponseWrapper instance is received.
                if (responseObject instanceof ZCRM.BulkRead.Model.ResponseWrapper) {
                    //Get the array of obtained jobDetail instances
                    let jobDetails = responseObject.getData();

                    jobDetails.forEach(jobDetail => {
                        //Get the Job ID of each jobDetail
                        console.log("Bulk read Job ID: " + jobDetail.getId());

                        //Get the Operation of each jobDetail
                        console.log("Bulk read Operation: " + jobDetail.getOperation());

                        //Get the State of each jobDetail
                        console.log("Bulk read State: " + jobDetail.getState().getValue());

                        //Get the Result instance of each jobDetail
                        let result = jobDetail.getResult();

                        //Check if Result is not null
                        if (result != null) {
                            //Get the Page of the Result
                            console.log("Bulkread Result Page: " + result.getPage().toString());

                            //Get the Count of the Result
                            console.log("Bulkread Result Count: " + result.getCount().toString());

                            //Get the Download URL of the Result
                            console.log("Bulkread Result Download URL: " + result.getDownloadUrl());

                            //Get the Per_Page of the Result
                            console.log("Bulkread Result Per_Page: " + result.getPerPage().toString());

                            //Get the MoreRecords of the Result
                            console.log("Bulkread Result MoreRecords: " + result.getMoreRecords().toString());

                        }

                        // Get the Query instance of each jobDetail
                        let query = jobDetail.getQuery();

                        if (query != null) {
                            //Get the Module Name of the Query
                            console.log("Bulk read Query Module: " + query.getModule());

                            //Get the Page of the Query
                            console.log("Bulk read Query Page: " + query.getPage().toString());

                            //Get the cvid of the Query
                            console.log("Bulk read Query cvid: " + query.getCvid());

                            //Get the fields List of each Query
                            let fields = query.getFields();

                            //Check if fields is not null
                            if (fields != null) {
                                fields.forEach(fieldName => {
                                    //Get the Field Name of the Query
                                    console.log("Bulk read Query Fields: " + fieldName);
                                });
                            }

                            // Get the Criteria instance of each Query
                            let criteria = query.getCriteria();

                            //Check if criteria is not null
                            if (criteria != null) {
                                this.printCriteria(criteria);
                            }
                        }

                        //Get the CreatedBy User instance of each jobDetail
                        let createdBy = jobDetail.getCreatedBy();

                        //Check if createdBy is not null
                        if (createdBy != null) {
                            //Get the ID of the CreatedBy User
                            console.log("Bulkread Created By User-ID: " + createdBy.getId());

                            //Get the Name of the CreatedBy User
                            console.log("Bulkread Created By user-Name: " + createdBy.getName());
                        }

                        //Get the CreatedTime of each jobDetail
                        console.log("Bulkread CreatedTime: " + jobDetail.getCreatedTime());

                        //Get the FileType of each jobDetail
                        console.log("Bulkread File Type: " + jobDetail.getFileType());
                    });
                }
                //Check if the request returned an exception
                else if (responseObject instanceof ZCRM.BulkRead.Model.APIException) {
                    //Get the Status
                    console.log("Status: " + responseObject.getStatus().getValue());

                    //Get the Code
                    console.log("Code: " + responseObject.getCode().getValue());

                    console.log("Details");

                    //Get the details map
                    let details = responseObject.getDetails();

                    if (details != null) {
                        Array.from(details.keys()).forEach(key => {
                            console.log(key + ": " + details.get(key));
                        });
                    }

                    //Get the Message
                    console.log("Message: " + responseObject.getMessage().getValue());
                }
            }
        }
    }
}
 
Download Result
              
              
class Bulkread
{
    /**
     *  Download Result
     * This method is used to download the result of Bulk Read operation
     * @param {BigInt} jobId The unique ID of the bulk read job.
     */
    static async downloadResult(jobId) {
        //example
        //String jobId = 34096432461001n;

        //Get instance of BulkReadOperations Class
        let bulkReadOperations = new ZCRM.BulkRead.Operations();

        //Call downloadResult method that takes jobId as parameter
        let response = await bulkReadOperations.downloadResult(jobId);

        if (response != null) {
            //Get the status code from response
            console.log("Status Code: " + response.getStatusCode());

            if ([204, 304].includes(response.getStatusCode())) {
                console.log(response.getStatusCode() == 204 ? "No Content" : "Not Modified");

                return;
            }

            //Get object from response
            let responseObject = response.getObject();

            if (responseObject != null) {
                //Check if expected FileBodyWrapper instance is received.
                if (responseObject instanceof ZCRM.BulkRead.Model.FileBodyWrapper) {

                    //Get StreamWrapper instance from the returned FileBodyWrapper instance
                    let streamWrapper = responseObject.getFile();

                    //Get name from StreamWrapper instance
                    let fileName = streamWrapper.getName();

                    //Get the stream from StreamWrapper instance
                    let readStream = streamWrapper.getStream();

                    var url = URL.createObjectURL(readStream);

                    var ttt = document.createElement('a');

                    ttt.href = url;

                    ttt.download = fileName;

                    ttt.click();
                }
                //Check if the request returned an exception
                else if (responseObject instanceof ZCRM.BulkRead.Model.APIException) {
                    //Get the Status
                    console.log("Status: " + responseObject.getStatus().getValue());

                    //Get the Code
                    console.log("Code: " + responseObject.getCode().getValue());

                    console.log("Details");

                    //Get the details map
                    let details = responseObject.getDetails();

                    if (details != null) {
                        Array.from(details.keys()).forEach(key => {
                            console.log(key + ": " + details.get(key));
                        });
                    }

                    //Get the Message
                    console.log("Message: " + responseObject.getMessage().getValue());
                }
            }
        }
    }
}