Emailing Attachments from a BPM

Hello, I’ve pieced this together from various posts around here,
The problem I’m having is in the foreach, the attachments in the foreach don’t attach and I get an exception error

Does anyone have any tips??

    var mailer = this.GetMailer(async: true);
	var message = new Ice.Mail.SmtpMail();
    message.SetFrom("Purchasing | Treadwell Group<do-not-reply@test.com.au>");
    message.SetTo(sendTo);
    message.SetBcc("lrb@test.com.au; ");
    message.SetSubject(eSubject);
    message.SetBody(eBody);
    
    Dictionary<string, string> attachments = new Dictionary<string, string>();
    
    attachments.Add(fileName, filePath); // << this one attaches
    
    if(sender.CheckBox05 == true)
    {
   
    var atts = Db.XFileRef.Where(f => f.Company == Session.CompanyID && f.DocTypeID == "Supplier" && Db.XFileAttch.Any(x => x.XFileRefNum == f.XFileRefNum && x.Key1 == poNum.ToString() && (x.RelatedToFile == "PODetail" || x.RelatedToFile == "POHeader"))).ToList();
    
    try
      {
        if(atts != null)
        {
          foreach(var att in atts)
          {
            filePath = att.XFileName;
            fileName = att.XFileDesc;
          attachments.Add(fileName, filePath); //<< these ones don't attach
          }
        }
      }
    
    catch (Exception ex)
      {
      this.PublishInfoMessage(ex.ToString(),Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual,"","");
      
      }
      
    }  
     mailer.Send(message, attachments);
  } 

Information Message Caught:

Information Message Detail:

Version: 0
Program:
Method:
User:
Company:
Site:

Message: System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Epicor.Customization.Bpm.DB78580E63AE594432ADF76882F6685F7D.PostTranDirective_PO_Send_C7AA3314718B4A789A98A74563B7F83B.A001_CustomCodeAction()
Severity: Error

1 Like

So the error message is saying you can’t add that filename to the dictionary as it is already in the dictionary. You can’t add the same thing twice.

You will need to check if the dictionary already has the filename before calling Add.

Brett

You are correct… the test document that I was attaching to the PO Header was in fact a copy of the same PO :rofl: :rofl:

image

including me

I added this in to catch any duplicates and add a counter on the end of the file

if(atts != null)
        {
        decimal count = 0;
          foreach(var att in atts)
          {
          try
            {
              filePath = att.XFileName;
              fileName = Path.GetFileName(filePath);
              attachments.Add(fileName, filePath); 
            }
          catch
            {        
              count++;
              string cntr = count.ToString("000");
              filePath = att.XFileName;   
              string s = Path.GetFileName(filePath);
              int idx = s.LastIndexOf('.');
              fileName = s.Substring(0, idx) + " - " + cntr + "." + s.Substring(idx + 1);
              attachments.Add(fileName, filePath);
            }
          }
        }
1 Like