Discussion:
ajax checing for duplicte records
(too old to reply)
Lithcause
2009-02-06 21:09:18 UTC
Permalink
i am trying to have a form in ajx that will also insert a new contact... i
havit working with the insert part but i want to have it check for duplicate
records before inserting the contact... here is the form ... and i also have a
cfc for the functions

<cfcomponent output="false">
<cfset this.dsn="myserver">
<!--- Populates the grower list Select --->
<cffunction name="getCompany" access="remote" returntype="array">
<cfset var rsData="">
<cfset var myReturn=ArrayNew(2)>
<cfset var i=0>
<cfquery name="rsData" datasource="myserver">
SELECT cid ,Company
FROM Contacts
order by Company asc
</cfquery>
<cfloop query="rsData">
<cfset myReturn[rsData.currentrow] [1]=rsdata.cid>
<cfset myReturn[rsData.currentrow] [2]=rsdata.Company>
</cfloop>
<cfreturn myReturn>
</cffunction>

<!--- Populates list related to grower --->
<cffunction name="getcontacts" access="remote" returntype="array">
<cfargument name="cid" type="string" required="no">
<cfset var rsData="">
<cfset var myReturn=Arraynew(2)>
<cfset var i=0>
<cftry>
<cfquery name="rsdata" datasource="myserver">
SELECT cid, Company, FullName, Lname, Fname, Address1, Address2, City, State,
zip, country, Phone, ext, cell, Fax, tollfree, Web, Email, Title, ExecutiveTitle
FROM Contacts
WHERE Contacts.Company = '#arguments.cid#'
</cfquery>
<cfcatch type="any">
<cfset returnStruct.success = false />
<cfset returnStruct.message = cfcatch.message />
</cfcatch>
</cftry>
<cfloop query="rsdata">
<cfif rsdata.recordcount gt 0>
<cfset myReturn[rsData.currentrow] [1]=rsdata.cid>
<cfset myReturn[rsData.currentrow] [2]=rsdata.FullName>
<!--- <cfelse>
<cfset rsdata.cid = 999999>
<cfset rsdata.FullName = 'none'>
<cfset myReturn[rsData.currentrow] [1]=rsdata.cid>
<cfset myReturn[rsData.currentrow] [2]=rsdata.FullName> --->
</cfif>
</cfloop>
<cfreturn myReturn>
</cffunction>

<!--- Gets contact info for 2page
contacts!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! --->
<cffunction name="getcontactsinfo2" access="remote" returnType="struct">
<cfargument name="growerName" type="string" required="no">
<cfset var data="">
<cfset var c = "">
<cfset var s = structNew()>

<cftry>
<cfquery name="data" datasource="myserver">
SELECT cid, Company, FullName, Lname, Fname, Address1, Address2, City, State,
zip, country, Phone, ext, cell, Fax, tollfree, Web, Email, Title, ExecutiveTitle
FROM Contacts
WHERE Company= <cfqueryparam cfsqltype="cf_sql_varcar"
value="#arguments.growerName#">
</cfquery>

<cfloop list="#data.columnlist#" index="c">
<cfset s[c] = data[c][1]>
</cfloop>
<cfcatch type="any">
<cfset returnStruct.success = false />
<cfset returnStruct.message = cfcatch.message />
</cfcatch>
</cftry>
<cfreturn s>
</cffunction>

<cffunction name="getcontactsinfo" access="remote" returnType="struct">
<cfargument name="cid" type="numeric" required="true">
<cfset var data="">
<cfset var c = "">
<cfset var s = structNew()>
<cftry>
<cfquery name="data" datasource="myserver">
SELECT cid, Company, FullName, Lname, Fname, Address1, Address2, City, State,
zip, country, Phone, ext, cell, Fax, tollfree, Web, Email, Title, ExecutiveTitle
FROM Contacts
WHERE contacts.cid= <cfqueryparam cfsqltype="cf_sql_integer"
value="#arguments.cid#">
</cfquery>
<!--- --->
<cfloop list="#data.columnlist#" index="c">
<cfset s[c] = data[c][1]>
</cfloop>
<cfcatch type="any">
<cfset returnStruct.success = false />
<cfset returnStruct.message = cfcatch.message />
</cfcatch>
</cftry>
<cfreturn s>
</cffunction>

<!--- Updates the database of grower contacts --->
<cffunction name="markTaskComplete" output="false" returntype="struct"
access="remote" hint="i mark a task complete">
<cfargument name="cid2" type="numeric" required="true" />
<cfargument name="company2" type="string" required="true" />
<cfargument name="address2" type="string" required="true" />
<cfargument name="city2" type="string" required="true" />
<cfargument name="state2" type="string" required="true" />
<cfargument name="zip2" type="string" required="true" />
<cfset var qMarkTaskComplete = "" />
<cfset var returnStruct = structNew() />
<cfset returnStruct.success = true />
<cfset returnStruct.taskID = arguments.cid2 />
<cftry>
<cfquery name="qMarkTaskComplete" datasource="myserver">
UPDATE
Contacts
SET
Company = <cfqueryparam value="#arguments.company2#" cfsqltype="cf_sq_varcar"
/> ,
Address1 = <cfqueryparam value="#arguments.address2#"
cfsqltype="cf_sq_varcar" /> ,
City = <cfqueryparam value="#arguments.city2#" cfsqltype="cf_sq_varcar" />,
State = <cfqueryparam value="#arguments.state2#" cfsqltype="cf_sq_varcar"
/>,
zip = <cfqueryparam value="#arguments.zip2#" cfsqltype="cf_sq_varcar" />
WHERE
cid = <cfqueryparam value="#arguments.cid2#" cfsqltype="cf_sq_int" />
</cfquery>
<cfcatch type="Database">
<cfset returnStruct.success = false />
<cfset returnStruct.message = cfcatch.message />
</cfcatch>
</cftry>
<cfdump var="#returnStruct#"/>

<cfreturn returnStruct />

</cffunction>

<cffunction name="lookupGrower" access="remote" returntype="array">
<cfargument name="search" type="any" required="false" default="">

<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(1)>

<!--- Do search --->
<cfquery name="data" datasource="myserver">
SELECT cid, Company, FullName, Lname, Fname, Address1, Address2, City, State,
zip, country, Phone, ext, cell, Fax, tollfree, Web, Email, Title, ExecutiveTitle
FROM Contacts
WHERE (Company LIKE '#ARGUMENTS.search#%')

</cfquery>

<!--- Build result array --->
<cfloop query="data">
<cfset ArrayAppend(result, Company)>
</cfloop>

<!--- And return it --->
<cfreturn result>
</cffunction>


<cffunction name="markTaskComplete2" output="false" returntype="struct"
access="remote" hint="i mark a task complete">

<cfargument name="company3" type="string" required="true" />
<cfargument name="address3" type="string" required="true" />
<cfargument name="city3" type="string" required="true" />
<cfargument name="state3" type="string" required="true" />
<cfargument name="zip3" type="string" required="true" />
<cfset var qMarkTaskComplete = "" />
<cfset var returnStruct = structNew() />
<cfset returnStruct.success = true />
<cfset returnStruct.taskID = arguments.cid />
<cftry>
<cfquery name="qMarkTaskComplete" datasource="myserver">
INSERT INTO KYIntranet.dbo.Contacts
(Company
,Address1
,City
,State
,zip)
VALUES
(<cfqueryparam value="#arguments.company3#" cfsqltype="cf_sq_varcar" /> ,
<cfqueryparam value="#arguments.address3#" cfsqltype="cf_sq_varcar" />
,<cfqueryparam value="#arguments.city3#" cfsqltype="cf_sq_varcar"
/>,<cfqueryparam value="#arguments.state3#" cfsqltype="cf_sq_varcar"
/>,<cfqueryparam value="#arguments.zip3#" cfsqltype="cf_sq_varcar" />)
</cfquery>
<cfcatch type="any">
<cfset returnStruct.success = false />
<cfset returnStruct.message = cfcatch.message />
</cfcatch>
</cftry>
<cfreturn returnStruct />
</cffunction>

</cfcomponent>


<!--- populates indv fields by calling the getcontactsinfo funcion on
grower.cfc using the value of select filed "cids" and calling javascript
showDetail --->
<cfajaxproxy bind="cfc:Contacts.getcontactsinfo({cid.value})"
onSuccess="showDetail" onError="showError">
<cfajaxproxy bind="cfc:Contacts.getcontactsinfo2({growername.value})"
onSuccess="showDetail2">

<script>
//Populates indiv fields called from the cfajaxproxy
function showDetail(r) {
document.getElementById("Company").value = r.COMPANY;
document.getElementById("FullName").value = r.FULLNAME;
document.getElementById("Address1").value = r.ADDRESS1;
document.getElementById("City").value = r.CITY;
document.getElementById("State").value = r.STATE;
document.getElementById("zip").value = r.ZIP;
document.getElementById("cid").value = r.CID;

}


function showError(r) {
document.getElementById("Company").value ="";
document.getElementById("FullName").value = "";
document.getElementById("Address1").value = "";
document.getElementById("City").value = "";
document.getElementById("State").value = "";
document.getElementById("zip").value = "";
document.getElementById("cid").value = "";

}


function showDetail2(s) {
document.getElementById("company2").value = s.COMPANY;
document.getElementById("address2").value = s.ADDRESS1;
document.getElementById("city2").value = s.CITY;
document.getElementById("state2").value = s.STATE;
document.getElementById("zip2").value = s.ZIP;
document.getElementById("cid2").value = s.CID;
}


//sends form fields to markTaskComplete method on grower.cfc
//called from button.
function whatever(){
ColdFusion.Ajax.submitForm('growers2', 'Contacts.cfc?method=markTaskComplete');
//alert("Record has been updated");
}
function whatever2(){
ColdFusion.Ajax.submitForm('growers3',
'Contacts.cfc?method=markTaskComplete2');
document.getElementById("company3").value = '';
document.getElementById("address3").value = '';
document.getElementById("city3").value = '';
document.getElementById("state3").value = '';
document.getElementById("zip3").value = '';
}



</script>

<div align="center">
<br />
<br />
<br />

<cflayout type="tab" align="left" style="width:500">

<cflayoutarea Title="Search/Update Member">
<cfpod title = "North Carolina Poultry Auto Suggest" height = "350" Width =
"500">
<cfform name="growers2" format="html">
<table><tr><td>Company</td></tr><tr>
<td>
<cfinput type="text"
name="growername"
ajithman
2009-02-13 04:47:29 UTC
Permalink
From my understanding your table that gets inserted is KYIntranet.dbo.Contacts
. And i guess company field in that table needs to be checked for duplicates.
So you can put the company field as primary key for that table. Inserting the
same company name can result in a Primary key exception meaning that a
duplicate has been entered. By this way you can check for duplicates.
<cftry>
<cfquery>Insert SQL code</cfquery>
<cfcatch>
<--Code to handle primary key exception-->
</cfcatch>
<cftry>
Lithcause
2009-02-16 14:21:48 UTC
Permalink
ok i know how to check for duplicates on a flash/html/coldfusion form but would
this work with ajax? considering it updates on the fly? and what is <--Code
to handle primary key exception-->? can you give an example?
Lithcause
2009-03-12 16:16:01 UTC
Permalink
nevermind got it working another way

Loading...