Search code examples
coldfusionrailo

ColdFusion Object to String


I'm really new to ColdFusion and am trying to pull data out of the database and send it to an email function I have created.

myQry = new Query();
myQry.setSQL("select * from stocknotifications LEFT JOIN options ON stocknotifications.stocknotification_id = options.option_id  ORDER BY stocknotification_id DESC LIMIT 1 "); //set query


qryRes = myQry.execute();

writedump(qryRes.getResult(), false);

Mail = variables.NotificationService.newMail();
Mail.setTo("[email protected]");
Mail.setSubject( "New Stock Request" );

// render body with layout which uses the rc.emailView
Mail.setBody(ToString(qryRes.getResult()));
variables.NotificationService.sendMail( Mail );

My writeDump() works and shows the last item in table. The only problem is I can't pass it into setBody() without it throwing an error.

Can't cast Complex Object Type Query to String Use Built-In-Function "serialize(Query):String" to create a String from Query

Any ideas?


Solution

  • Per the error message you received, you could actually replace toString with serialize and you'd be good to go.

    If all you want is the data going to e-mail without caring about presentation, quick and dirty, you might want to try SerializeJSON(); it will convert the query into a JSON string with COLUMNS and DATA:

    Mail.setBody(SerializeJSON(qryRes.getResult()));