SyncMadeEasy/force-app/main/default/classes/BatchForSendErrorMail.cls

122 lines
7.8 KiB
OpenEdge ABL

/**
* ─────────────────────────────────────────────────────────────────────────────────────────────────┐
* This Batch class is use for send error email notifications to the users.
* Shopify API Documentation: https://shopify.dev/docs/admin-api/rest/reference
* Create Shopify Store: https://www.shopify.com/signup?itcat=identity&itterm=create-new-store
* Shopify API Release 2020: https://www.shopify.in/partners/blog/shopify-api-release-january-2020
* What is Person Account in Salesforce ? https://help.salesforce.com/articleView?id=account_person.htm&type=5
* ──────────────────────────────────────────────────────────────────────────────────────────────────
* @author Sumit <sumit@hicShopify.com>
* ──────────────────────────────────────────────────────────────────────────────────────────────────
*/
global with sharing class BatchForSendErrorMail implements Database.Batchable<HIC_ShopifySync__Error_Message__c>,Database.Stateful{
global String finalCsvHeader = 'Shofify ID,Object Name, Error Message \n';
global String finalCsv = '';
//global String headerValue = 'Shofify ID,Object Name, Error Message \n';
//global void BatchForSendErrorMail(){
// finalCsv = headerValue;
//}
global boolean sended = true;
global set<string> setofidfordeleteErrorMessage = new set<string>();
global Iterable<HIC_ShopifySync__Error_Message__c> start (Database.BatchableContext bc){
/*list<HIC_ShopifySync__Error_Message__c> listoferrors = new list<HIC_ShopifySync__Error_Message__c>();
String query = 'Select '+CheckAccessibility.isAccessible('id,name,HIC_ShopifySync__Delivered__c,HIC_ShopifySync__Error_id__c,HIC_ShopifySync__Message__c,HIC_ShopifySync__Shopify_id__c,HIC_ShopifySync__Important_Field__c','HIC_ShopifySync__Error_Message__c')+' from HIC_ShopifySync__Error_Message__c where '+CheckAccessibility.isAccessible('HIC_ShopifySync__Delivered__c','HIC_ShopifySync__Error_Message__c')+' = false and '+CheckAccessibility.isAccessible('HIC_ShopifySync__IsWebhook__c','HIC_ShopifySync__Error_Message__c')+'= true Order bY Name';
listoferrors = database.query(query);
if(!listoferrors.isempty()){
return listoferrors;
}else{
list<HIC_ShopifySync__Error_Message__c> emptylist = new List<HIC_ShopifySync__Error_Message__c>();
return emptylist;
}*/
return null;
}
global void execute(Database.BatchableContext bc ,list<HIC_ShopifySync__Error_Message__c> scope){
if(!scope.isEmpty() ){
//String csvBody = '';
// String headerValue = 'Shofify ID,Object Name, Error Message \n';
//string errorMsg = '';
// finalCsv = headerValue;
/* for(HIC_ShopifySync__Error_Message__c err : scope){
if(err.HIC_ShopifySync__Shopify_id__c != null && err.HIC_ShopifySync__Shopify_id__c != '' && (err.HIC_ShopifySync__Shopify_id__c).containsIgnoreCase('T')) {
err.HIC_ShopifySync__Shopify_id__c = (err.HIC_ShopifySync__Shopify_id__c).removeEnd('T');
}
if(err.HIC_ShopifySync__Shopify_id__c != null && err.HIC_ShopifySync__Shopify_id__c != '' && (err.HIC_ShopifySync__Shopify_id__c).containsIgnoreCase('F')) {
err.HIC_ShopifySync__Shopify_id__c = (err.HIC_ShopifySync__Shopify_id__c).removeEnd('F');
}
string recordString = err.HIC_ShopifySync__Shopify_id__c+','+ err.Name +','+err.HIC_ShopifySync__Message__c +'\n';
finalCsv = finalCsv + recordString;
setofidfordeleteErrorMessage.add(String.valueof(err.id));
}*/
}
}
global void finish(Database.BatchableContext bc){
/* if( finalCsv != null && finalCsv != ''){
finalCsv = finalCsvHeader + finalCsv;
List <String> toAddresses = new List <String>();
List <HIC_ShopifySync__Email_Addresses_to_send_error_CSV__c> getEmailIdsToSendErrorEmails = new List <HIC_ShopifySync__Email_Addresses_to_send_error_CSV__c>();
String getEmailIds = 'Select '+CheckAccessibility.isAccessible('id,name ','HIC_ShopifySync__Email_Addresses_to_send_error_CSV__c') +' from HIC_ShopifySync__Email_Addresses_to_send_error_CSV__c Limit 49000';
getEmailIdsToSendErrorEmails = database.query(getEmailIds);
system.debug('***Emailidlist---*'+getEmailIdsToSendErrorEmails);
if(getEmailIdsToSendErrorEmails.isempty()) return;
for(HIC_ShopifySync__Email_Addresses_to_send_error_CSV__c emailIds : getEmailIdsToSendErrorEmails){
if(emailIds.name != null && emailIds.name != '')
toAddresses.add(emailIds.name);
}
system.debug('**finalCsv*-->'+finalCsv);
String csvname = 'Error File.csv';
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
csvAttc.setFileName(csvname);
csvAttc.setBody(Blob.valueOf(finalCsv));
String subject ='Shopify Sync Error';
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
email.setSubject(subject);
if(!toAddresses.isEmpty() ){
System.debug('addrress****_-'+toAddresses);
email.setToAddresses(toAddresses );
PageReference pageRef = new PageReference('/apex/HIC_ShopifySync__ShopifyConfigurationResultTemplate?result=failWebhook');
if(!Test.isRunningTest()){
email.setHtmlBody(pageRef.getContent().toString());
}
email.setPlainTextBody('');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
try{
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
system.debug('***SendEmailResult'+r);
}catch(Exception e){
sended = false;
system.debug('Error: '+ e.getMessage() +'at Line '+e.getLineNumber()+' Cause '+e.getCause());
}
if(!setofidfordeleteErrorMessage.isEmpty() && sended){
list<HIC_ShopifySync__Error_Message__c> listoferrorstodelete = new list<HIC_ShopifySync__Error_Message__c>();
String query = 'Select '+CheckAccessibility.isAccessible('name,HIC_ShopifySync__Delivered__c,HIC_ShopifySync__Error_id__c,HIC_ShopifySync__Message__c,HIC_ShopifySync__Shopify_id__c,HIC_ShopifySync__Important_Field__c','HIC_ShopifySync__Error_Message__c')+' from HIC_ShopifySync__Error_Message__c where id in: setofidfordeleteErrorMessage ';
listoferrorstodelete = database.query(query);
if(!listoferrorstodelete.isEmpty()){
if(CheckAccessibility.isDeletable('HIC_ShopifySync__Error_Message__c')){
database.delete(listoferrorstodelete,false);
}
}
}
}
}*/
}
}