We could successfully send mail messages using SupportCase record (update operation) and Message record (add operation).
As a result we can see entry in the support case activity with SentMail flag yes and attachment but actual mail is not sent to end customer.
But through Netsuite UI we could send mail to end customer.
We would like to know that, when does port.add(messageRecord) will send the mail to end customer when all required fields are set or is it a separate acticity to send mail to customer and outside of web service API functionality?
We tried to send a mail using JavaMail API after Message Record is updated using Web Service API.But we could not set proper from field of mail, which is used in case of reply. Here is expample of this value [ "APL Pvt. Ltd." <cases.721906.28_53_4.9e61720f4e@cases.netsuite.com> ] which is generated when UI is used to send mail.
please advise us if we missed set any values to tigger to send a mail to end customer. Please find the code attached.
Code has few queries commented inline the code of MailSender.java
public class MailSender {
private static String caseId = "28";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LoginHandler loginHandler = new LoginHandler();
loginHandler.login();
NetSuitePortType port = loginHandler.port;
ReadResponse response = null;
SupportCase supportCase = new SupportCase();
supportCase.setInternalId(caseId);
RecordRef status = new RecordRef();
status.setType(RecordType.supportCaseStatus);
status.setInternalId("2"); // 5 is close
//status.setName("Closed"); // Closed
supportCase.setStatus(status);
// Below 3 lines of code send the mail to customer, but attachment can not be sent
// so commented to used Message record to send attachments
//supportCase.setEmailForm(true);
//supportCase.setOutgoingMessage("Hi this is UPDATED outgoing test message to Vinay");
//supportCase.setMessageNew(true);
try {
WriteResponse updateResponse = port.update(supportCase);
printStatus(updateResponse.getStatus());
if(updateResponse.getStatus().isIsSuccess()){
System.out.println("Support case is updated");
}else{
System.out.println("Support case FAILED to update");
return;
}
System.out.println("Retrieving again");
RecordRef supportCaseRetrieve = new RecordRef();
supportCaseRetrieve.setType(RecordType.supportCase);
supportCaseRetrieve.setInternalId(caseId);
response = port.get(supportCaseRetrieve);
System.out.println("Support case is retrieved");
supportCase = (SupportCase) response.getRecord();
System.out.println("Case start date is " + supportCase.getStartDate().toString());
System.out.println("Internal id is " + supportCase.getInternalId());
System.out.println("Status is " + supportCase.getStatus().getName());
Message message = createMessage(port, supportCase);
updateResponse = port.add(message);
if(updateResponse.getStatus().isIsSuccess()){
System.out.println("Message is added to SupportCase");
String fromMail = "\"APL \" <"+ supportCase.getInboundEmail() + ">";
//System.out.println("Sending mail to " + fromMail);
//Below TestMail uses Java Mail API to sent mail with attachment
// But from field (2nd param) is not as per format that UI uses
TestMail.sendMail(null, supportCase.getInboundEmail(), supportCase.getEmail(), "/root/install/LittleBookOfRuby.pdf");
}else{
System.out.println("Message is not sent");
}
printStatus(updateResponse.getStatus());
//System.out.println("Message is sent..Bingo");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Error update / re-retrieve");
e.printStackTrace();
}
loginHandler.logout();
System.out.println("Done");
}
/**
* @throws IOException
*
*/
private static Message createMessage(NetSuitePortType port, SupportCase supportCase) throws IOException {
Message message = new Message();
RecordRef activity = new RecordRef();
activity.setInternalId(supportCase.getInternalId());
activity.setType(RecordType.supportCase);
message.setActivity(activity);
message.setRecipient(supportCase.getCompany());
RecordRef loginUser = new RecordRef();
loginUser.setType(RecordType.employee);
// Query : How to retrieve internal id of loged in user from the current session
// for now it is hardcoded
loginUser.setInternalId("-5");
message.setAuthor(loginUser);
message.setIncoming(false);
message.setEmailed(true);
message.setMessage("Hi this is test " +
"message set from webservice client " +
"updating support case");
message.setSubject("Test mail from webservice client");
addAttachment(message, "/root/install/LittleBookOfRuby.pdf");
return message;
}
/**
* @param message
* @throws FileNotFoundException
* @throws IOException
* @throws IllegalArgumentException
*/
private static void addAttachment(Message message, String fileName) throws FileNotFoundException, IOException, IllegalArgumentException {
File cabinetFile = new File();
java.io.File file = new java.io.File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
fis.read(b);
fis.close();
cabinetFile.setName("test.pdf");
cabinetFile.setFileSize(new Double(file.length()));
cabinetFile.setFileType(MediaType.fromString(MediaType.__PDF));
RecordRef folder = new RecordRef();
folder.setType(RecordType.file);
folder.setName("test");
// Query: How to set proper folder value here?
cabinetFile.setFolder(folder);
cabinetFile.setContent(b);
MessageMediaItemList list = new MessageMediaItemList();
list.setMediaItem(new File[]{cabinetFile });
message.setMediaItemList(list);
}
public static void printStatus(Status status){
if (status != null) {
StatusDetail[] details = status.getStatusDetail();
for (int i = 0; details != null && i < details.length; i++) {
System.out.println(details[i].getMessage());
}
}
}
}