Page 1 of 1

Settlement Date format in a code snippet

Posted: Fri Jan 26, 2024 6:14 am
by cklahr
Hi,

I'm trying to get the settlement date to appear in an email rendered via automation, but I'm getting an error. Any idea what the correct format would be?

Also, how can I get the format of the text to match the rest of the text?

Thank you!

Code snippet:

Code: Select all

from System import *
import clr
clr.AddReference('System.Net.Http')
from System.Net.Http import HttpClient, StringContent
from System.Text import Encoding
clr.AddReference('System.Web.Extensions')
from System.Web.Script.Serialization import JavaScriptSerializer
from SoftPro.ClientModel import *
from SoftPro.Select.Client import *

def SendEmail(fromAddress, toAddress):
	email = {
		'to': toAddress,
		'from': fromAddress,
		'body': '<hl>Settlement Date has been added/updated in the file to </hl><pre>' + Context.SettlementDate + ' .',
#		+ JavaScriptSerializer().Serialize(Context.FileHeld.HeldByUser)
		'subject': 'Settlement Date update for ' + Context.EmailSubjectLine
	}
	js = JavaScriptSerializer()
	params = js.Serialize(email)
	content = StringContent(params, Encoding.UTF8, 'application/json')	
	client = HttpClient()
	response = client.PostAsync('http://sharedapi.madisoncres.com/functions/email', content).Result
	print(response)

for contact in Context.Contacts:
	if hasattr(contact, 'OtherType') and contact.OtherType == 'Funder':
		if contact.Email:
			SendEmail('selectnotifications@madisoncres.com', contact.Email)	
if Context.FileHeld.HeldByUser.EmailAddress:
	SendEmail('selectnotifications@madisoncres.com', Context.FileHeld.HeldByUser.EmailAddress)	

Re: Settlement Date format in a code snippet

Posted: Fri Jan 26, 2024 6:15 am
by cklahr
The issue appears to be with the "Context.SettlementDate" field. When I remove that or try another field, it works.

Thank you very much!

Re: Settlement Date format in a code snippet

Posted: Fri Jan 26, 2024 11:54 am
by PaulMcCullough
To format SettlementDate you need to call ToString(). If you don't pass any parameters it will print the date and time including the seconds. You can also pass any .NET DateTime standard or custom format string into the ToString() call to change the format.

Here are some examples:

Code: Select all

	# output looks like 1/31/2024 10:15:00 AM
	Context.SettlementDate.ToString()
	
	# output looks like 1/31/2024 10:15 AM
	Context.SettlementDate.ToString('g')
	
	# only the date is output 1/31/2024
	Context.SettlementDate.ToString('d')
For more formatting options you can visit the Microsoft web site to see Standard date and time format strings or if you need a more customized format, Custom date and time format strings

Re: Settlement Date format in a code snippet

Posted: Mon Jan 29, 2024 8:08 am
by cklahr
That worked. Thank you so much!!