Question about setting a variable in a directive to use as the 'TO' in an email

Hello,

I am trying to do the PO approval email alert they have been asking about for forever.

I created a data directive on the Erp.POApvMsg Standard directive.

It checks for a condition of “1” on the MsgType and goes to a set argument/variable.

I created a variable called ‘EmailAddress’.

This is where I am getting hung up.

I need to pull the EMailAddress field from PurAgent.EMailAddress and feed it to the send email through the variable I created.

What I put in there is:

(from row in Db.PurAgent where row.Company == ttPOApvMsgRow.Company && row.BuyerID == ttPOApvMsgRow.MsgTo
select new {row.EMailAddress}).FirstOrDefault().ToString()

I then passed it to the email in the To field but it didn’t work.

I then put my email in the To field and put the result of the variable in the body so I can see what it has in it.

This is what it spits out: { EMailAddress = shobdy@fakeEmailAddress.com }

That is with the curly brackets.

I then created a variable and just put this in it: shobdy@fakeEmailAddress.com

I put that in the body next to the other and sent it. I got this: shobdy@fakeEmailAddress.com

It doesn’t have the Curly brackets.

What am I doing wrong?

Any help is appreciated.

You are creating a new anonymous object in your LINQ query. Change it to say just select row.EMailAddress without the new. The new keyword in C# instantiates a new object.

Some sample code, might help you a bit.

    // Email Notification
    //
    // 08/01/17 HK: Initial Implementation, since this is considered a temporary, lets keep it simple
    //

    // Initialize Actions
    Func<string, string> GetCompanyAddressAction = (CompanyID) => {

    	var Company_Row =
    		(from sc in Db.SysCompany.With(LockHint.NoLock)
    		where sc.Company == CompanyID
    		select new { sc.EmailFromAddr, sc.EmailFromLabel }).FirstOrDefault();

    	if (Company_Row != null) {
    		return string.Format(@"""{0}"" <{1}>", Company_Row.EmailFromLabel.Trim(), Company_Row.EmailFromAddr.Trim());
    	}

    	return string.Empty;
    };

    Func<string, string> GetCostPerCode = (CostPerCode) => {

    	switch (CostPerCode)
    	{
    		case "E":
    			return @"/ 1";

    		case "C":

    			return @"/ 100";

    		case "M":
    			return @"/ 1000";
    	}

    	return string.Empty;
    };


    // Initialize Variables
    string EmailTO = "haso.keric@xyz.com";
    string EmailCC = "";
    string EmailBCC = "";
    string EmailSubject = "";
    string EmailBody = "";

    // Get POHeader
    var ttPOHeader_Row = ttPOHeader.FirstOrDefault();

    if (ttPOHeader_Row != null)
    {
    	// Get Vendor ID and Name
    	var Vendor_Row =
    		(from v in Db.Vendor.With(LockHint.NoLock)
    		where
    			v.Company == ttPOHeader_Row.Company &&
    			v.VendorNum == ttPOHeader_Row.VendorNum
    		select new { v.Name, v.VendorID }
    		).FirstOrDefault();

    	// Set Subject
    	EmailSubject = "[ Alert ] PO #: " + ttPOHeader_Row.PONum + " was issued from Company " + ttPOHeader_Row.Company;

    	// Start Email Body
    	EmailBody += string.Format("Purchase Order <b>{0}</b> was issued from Company <b>{1}</b> on <b>{2}</b> to Vendor ID {3}, <b>{4}</b>, for:",
    								ttPOHeader_Row.PONum, ttPOHeader_Row.Company, ttPOHeader_Row.OrderDate.Value.ToShortDateString(), Vendor_Row.VendorID, Vendor_Row.Name );

      	EmailBody += "<BR><BR>";


    	EmailBody +=
    		@"<table width='100%' align='center' border='1' cellspacing='1' cellpadding='1' style='font-family: Arial; font-size: 10pt;' bordercolor='#CCCCCC'>"
    		+ "<tr>"
    		+ "<th>Line</th>"
    		+ "<th>Part</th>"
    		+ "<th>Our Qty</th>"
    		+ "<th>Vend Qty</th>"
    		+ "<th>Unit Cost</th>"
    		+ "</tr>";


    	// Get PO Lines
    	var ttPODtl_Rows =
    		from pod in Db.PODetail.With(LockHint.NoLock)
    		where
    			pod.Company == ttPOHeader_Row.Company &&
    			pod.PONUM == ttPOHeader_Row.PONum
    		select new { pod.POLine, pod.PartNum, pod.XOrderQty, pod.OrderQty, pod.IUM, pod.PUM, pod.LineDesc, pod.DocUnitCost, pod.CostPerCode };

    	foreach (var ttPODtl_Row in ttPODtl_Rows)
    	{
    		EmailBody +=
    		  "<tr>"
    		+ @"<td align='center'>" + ttPODtl_Row.POLine + "</td>"
    		+ @"<td align='center'><b>" + ttPODtl_Row.PartNum + @"</b><br><span style='font-size: 8pt'>" + ttPODtl_Row.LineDesc + "</span></td>"
    		+ @"<td align='center'>" + Math.Round(ttPODtl_Row.XOrderQty, 2) + " " + ttPODtl_Row.IUM + "</td>"
    		+ @"<td align='center'>" + Math.Round(ttPODtl_Row.OrderQty, 2) + " " + ttPODtl_Row.PUM + "</td>"
    		+ @"<td align='center'>" + Math.Round(ttPODtl_Row.DocUnitCost, 2) + " " + GetCostPerCode(ttPODtl_Row.CostPerCode) + "</td>"
    		+ "</tr>";
    	}


    	EmailBody += "</table>";

    	// Send Email
    	var mailer = this.GetMailer(async:true);
    	var message = new Ice.Mail.SmtpMail();
    	message.SetFrom( GetCompanyAddressAction(ttPOHeader_Row.Company) );
    	message.SetTo(EmailTO);
    	message.SetCC(EmailCC);
    	message.SetBcc(EmailBCC);
    	message.SetSubject(EmailSubject);
    	message.SetBody(EmailBody);
    	message.IsBodyHtml = true;
    	mailer.Send(message);
    }
1 Like

Thank you Jose!
That was exactly my problem.
I have no background in C#, so I didn’t recognize the issue.
Hopefully I won’t be caught on that one again!
I also want to thank you hasokeric.
I will be pouring over the code and see what I can learn from it.

Everyone’s help is definitely appreciated.