Well, I had an error report from a client of my ex-company. As I have an agreement with them to cover these situations I looked at it and thought "Oh no! The damn 'null null' error!".
So what was going on? Follow me:
I have a report that permits the client to find specific information about who receives referral fee over their contracts. So the client selects an user to search for and submits.
The ColdFusion template then search for referral fees this user receive in an referential table, which returns the contracts IDs. Then I search the contracts for details and loop over the contracts to mark which of them has already paid some referral to that user, do some formatting on the client's name and re-sort all data by paid, expiration date, finished date and then contract title.
To clarify, some code (I use "SELECT *" just for simplicity, don't do that on your code):
<cfquery datasource="dsn" name="qReferrals">So what the heck am I doing here? Well, I search for the referrals, verify if there is any, then I search for my contracts, creating two "to-be-filled" fields:
SELECT DISTINCT(contract_id) AS contract
FROM contract_referrals
WHERE user_id =
<cfqueryparam cfsqltype="cf_sql_integer" value="#form.user_id#" />
</cfquery>
<cfif qReferrals.recordCount>
<cfquery datasource="dsn" name="qContracts">
SELECT *,
0 AS paid,
'' AS clientName
FROM contract
WHERE contract_id IN
<cfqueryparam cfsqltype="cf_sql_integer" list="yes"
value="#valueList(qReferrals.contract)#" />
</cfquery>
</cfif>
0 AS paid,I thought the first one (paid) was being created as a NUMBER, a INT or TINYINT, I really doesn't care - but a damn number. The second field doesn't matter for this example. Alright? Let's go on:
'' AS clientName
<cfloop query="qContracts">Here I go through all the contracts found and search one by one for paid movements on the movement table. If I found one or more, I change the "paid" flag from 0 to 1. Pretty simple, uh?
<cfquery datasource="dsn" name="qPaid">
SELECT movement_id
FROM movement
WHERE contract_id = <cfqueryparam cfsqltype="cf_sql_integer"
value="#contract_id#" />
AND
movement_paid = 1
</cfquery>
<cfif qPaid.recordCount>
<cfset qContracts["paid"][currentRow] = 1 />
</cfif>
</cfloop>
<cfquery dbtype="query" name="qContracts">Again, pretty simple. I'm just reordering the query, using a query of queries and the already filled field that I created within the first run of the query.
SELECT * FROM qContracts ORDER BY
paid, expirationDate, finishedAt, title
</cfquery>
<cfquery datasource="dsn" name="qReferrals">And this is the code that works, but just 'cause the type of my data doesn't matter at all (I can sort from numeric or string types easily), but if that wasn't the case, I would be in trouble.
SELECT DISTINCT(contract_id) AS contract
FROM contract_referrals
WHERE user_id =
<cfqueryparam cfsqltype="cf_sql_integer" value="#form.user_id#" />
</cfquery>
<cfif qReferrals.recordCount>
<cfquery datasource="dsn" name="qContracts">
SELECT *,
'0' AS paid,
'' AS clientName
FROM contract
WHERE contract_id IN
<cfqueryparam cfsqltype="cf_sql_integer" list="yes"
value="#valueList(qReferrals.contract)#" />
</cfquery>
</cfif>
<cfloop query="qContracts">
<cfquery datasource="dsn" name="qPaid">
SELECT movement_id
FROM movement
WHERE contract_id = <cfqueryparam cfsqltype="cf_sql_integer"
value="#contract_id#" />
AND
movement_paid = 1
</cfquery>
<cfif qPaid.recordCount>
<cfset qContracts["paid"][currentRow] = "1" />
</cfif>
</cfloop>
<cfquery dbtype="query" name="qContracts">
SELECT * FROM qContracts ORDER BY
paid, expirationDate, finishedAt, title
</cfquery>
Labels: adobe, cfml, coldfusion, work
« Back to Tecnologia |