BPM to email multiple Sales Reps upon shipment not working

,

I’m trying to figure out why a block of custom code in a BPM to email tracking info upon shipment isn’t working as expected. The block of code in question is supposed to include any Salespeople listed on the Order in the Cc line. If there is only one Salesperson listed on the Order, that salesperson is included in the Cc. If there is more than one salesperson listed, none of the salespeople are included on the Cc line.

Can anyone point out the (probably) obvious error that would cause this?

// Look up each sales rep
      if (orderHed.SalesRepList.IndexOf('~') > -1) {
        foreach (var salesRepId in orderHed.SalesRepList.Split('~')) {
          var salesReps = Db.SalesRep.Where((s) => s.Company.Equals(Session.CompanyID) && s.SalesRepCode.Equals(salesRepId) && s.AlertFlag);
          foreach (var salesRep in salesReps) {
            if (ccSalesReps) {
              if (salesRep.EMailAddress != "" && emailCcAddress.IndexOf(salesRep.EMailAddress) == -1) {
                if (emailCcAddress != "") { emailCcAddress += ";"; }
                emailCcAddress += salesRep.EMailAddress;
              }    
            }
            
            salesRepName = salesRep.Name;
            salesRepPhone = salesRep.OfficePhoneNum;
            salesRepEmail = salesRep.EMailAddress;
          }
        }
      
      } 
	  
	  else {
          var salesReps = Db.SalesRep.Where((s) => s.Company.Equals(Session.CompanyID) && s.SalesRepCode.Equals(orderHed.SalesRepList));
          foreach (var salesRep in salesReps) {
            if (ccSalesReps) {
              if (salesRep.EMailAddress != "" && emailCcAddress.IndexOf(salesRep.EMailAddress) == -1) {
                if (emailCcAddress != "") { emailCcAddress += ";"; }
                emailCcAddress += salesRep.EMailAddress;
              }      
            }
            
            salesRepName = salesRep.Name;
            salesRepPhone = salesRep.OfficePhoneNum;
            salesRepEmail = salesRep.EMailAddress;
          } 
      }

What are your symptoms?

Also I presume the expected Salesreps have AlertFlag = true?
I didnt notice that requirement in the else logic for single rep.

Instead of messing with the tilde field, you could do a GetByID for the order and then iterate the OrderRepComm table in the dataset.

var emailList = new List<string>();
var emails = string.Empty;
this.CallService<Erp.Contracts.SalesOrderSvcContract>(svc => {
  var ds = svc.GetByID(orderNum);  
  foreach (var repComm in ds.OrderRepComm)
  {
    var rep = Db.SalesRep.Where(s => s.Company == repComm.Company && s.SalesRepCode == repComm.SalesRepCode && s.EMailAddress != string.Empty && s.AlertFlag).Select(s => new {s.EMailAddress}).FirstOrDefault();
    if (rep != null)
    {
      emailList.Add(rep.EMailAddress);
    }
  }
});

emails = string.Join(";", emailList);

This also works if you dont want to retrieve or don’t already have the order dataset

var repCodes = ds.OrderHed.First().SalesRepList.Split('~');
var reps = Db.SalesRep.Where(s => repCodes.Contains(s.SalesRepCode) && s.AlertFlag).Select(s => new {s.EMailAddress, s.Name, s.OfficePhoneNum}).ToList();
string emails = string.Join(";", reps.Select(s => s.EMailAddress));

Yeah, I’d take a look to make sure this field is set to true on your reps. Your code could use some cleanup, but it should work, and this validation is the difference.

Just updating this for future readers.

I inherited this code and had to investigate what the AlertFlag was even doing throughout the process. I honestly could not figure it out, so I just removed the AlertFlag condition from the multiple reps scenario and everything worked.

Thank you to all for the review and suggestions!