public class Cls_RecordsAsCSVExporter {
public static void mainmethod(){
List<Account> extractAccountMemList = new List<Account>();
List<Account> AccList = [select id,name,(select id from contacts) from Account];
for(Account acc: AccList){
List<contact> conlist = acc.contacts;
if(conlist.size() == 0){
extractAccountMemList.add(acc);
}
}
String generatedCSVFile ='';
String fileRow ='';
for(Account acc: extractAccountMemList){
fileRow ='';
fileRow = fileRow +','+ acc.Id;
fileRow = fileRow +','+ acc.Name;
fileRow = fileRow.replaceFirst(',','');
generatedCSVFile = generatedCSVFile + fileRow + '\n';
}
Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
Blob csvBlob = blob.valueOf(generatedCSVFile);
String csvName = 'acc details which doesn ot have contacts.csv';
csvAttachment.setFileName(csvName);
csvAttachment.setBody(csvBlob);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'XXXXXXXXX@gmail.com'};
String subject = 'acc details which does not have contacts CSV';
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setPlainTextBody('acc details which does not have contacts CSV');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttachment});
Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
}
}
Open Developer Console and call the mainmethod() it will send all your Accounts which don't have Contacts to mail id which is mentioned in the program.
public static void mainmethod(){
List<Account> extractAccountMemList = new List<Account>();
List<Account> AccList = [select id,name,(select id from contacts) from Account];
for(Account acc: AccList){
List<contact> conlist = acc.contacts;
if(conlist.size() == 0){
extractAccountMemList.add(acc);
}
}
String generatedCSVFile ='';
String fileRow ='';
for(Account acc: extractAccountMemList){
fileRow ='';
fileRow = fileRow +','+ acc.Id;
fileRow = fileRow +','+ acc.Name;
fileRow = fileRow.replaceFirst(',','');
generatedCSVFile = generatedCSVFile + fileRow + '\n';
}
Messaging.EmailFileAttachment csvAttachment = new Messaging.EmailFileAttachment();
Blob csvBlob = blob.valueOf(generatedCSVFile);
String csvName = 'acc details which doesn ot have contacts.csv';
csvAttachment.setFileName(csvName);
csvAttachment.setBody(csvBlob);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'XXXXXXXXX@gmail.com'};
String subject = 'acc details which does not have contacts CSV';
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setPlainTextBody('acc details which does not have contacts CSV');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttachment});
Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
}
}
Open Developer Console and call the mainmethod() it will send all your Accounts which don't have Contacts to mail id which is mentioned in the program.
No comments:
Post a Comment