Month: February 2014

How to retrieve multiple selected records from the Grid through X++ in AX 2012

    int                recordsCount;
    SalesLine    salesline;
    recordsCount = salesline_ds.recordsMarked().lastIndex();  // Total no of marked records.
    salesline = salesline_ds.getFirst(1);
    while (salesline)
    {
         info(strFmt(“%1 : %2”,_salesLine.ItemId, _salesLine.LineNum));
        salesline = salesline_ds.getNext();
    }

how to send an email from AX 2012

/// <summary>
/// Call this static method to send Email to the user along with the Attachment file
/// </summary>
/// <param name=”_completeFilePath”>
/// File Path for the attachment
/// </param>
/// <param name=”_toEmail”>
/// To Email
/// </param>
/// <param name=”_subject”>
/// Subject of Email
/// </param>
/// <param name=”_body”>
/// Body of Email
/// </param>
/// <returns>
/// Boolean
/// </returns>
/// <remarks>
///
/// </remarks>

public static boolean sendEmail(Email _fromEmail,str 60 _completeFilePath, Email _toEmail, str 30 _subject, str 1000 _body)
{
str sender;

List toList;

List ccList;
ListEnumerator le;

Set permissionSet;
System.Exception e;

str mailServer;
int mailServerPort;
System.Net.Mail.SmtpClient mailClient;
System.Net.Mail.MailMessage mailMessage;
System.Net.Mail.MailAddress mailFrom;
System.Net.Mail.MailAddress mailTo;
System.Net.Mail.MailAddressCollection mailToCollection;
System.Net.Mail.MailAddressCollection mailCCCollection;
System.Net.Mail.AttachmentCollection mailAttachementCollection;
System.Net.Mail.Attachment mailAttachment;
;

try
{
sender = _fromEmail;
toList = strSplit(_toEmail, ‘;’);
// ccList = strSplit(cc, ‘;’);

permissionSet = new Set(Types::Class);
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new FileIOPermission(_completeFilePath, ‘rw’));
CodeAccessPermission::assertMultiple(permissionSet);

mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName;
mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber;
mailClient = new System.Net.Mail.SmtpClient(mailServer, mailServerPort);

le = toList.getEnumerator();
le.moveNext();

mailFrom = new System.Net.Mail.MailAddress(sender);
mailTo = new System.Net.Mail.MailAddress(strLTrim(strRTrim(le.current())));
mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);

mailToCollection = mailMessage.get_To();
while (le.moveNext())
{
mailToCollection.Add(strLTrim(strRTrim(le.current())));
}

// le = ccList.getEnumerator();
//// mailCCCollection = mailMessage.get_CC();
// while (le.moveNext())
// {
// mailCCCollection.Add(strLTrim(strRTrim(le.current())));
// }

mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
mailMessage.set_Subject(_subject);
mailMessage.set_Body(_body);

if(strLen(_completeFilePath) > 1)
{
mailAttachementCollection = mailMessage.get_Attachments();
mailAttachment = new System.Net.Mail.Attachment(_completeFilePath);
mailAttachementCollection.Add(mailAttachment);
}
mailClient.Send(mailMessage);
mailMessage.Dispose();

CodeAccessPermission::revertAssert();

info(“Email sent.”);
}
catch (Exception::CLRError)
{
e = ClrInterop::getLastException();
while (e)
{
info(e.get_Message());
e = e.get_InnerException();
}
CodeAccessPermission::revertAssert();
}
return true;
}