Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 918
  • Last Modified:

ColdFusion Insert Today's Date if Not Inputted

The code below checks if data has been entered - if yes then update records.

How can I add today's date if the date has not been entered. (And only enter date if the other two fields have data.)


Code:



<cfset isEverythingValidated = false>
         <cfif isNumeric(variables.REC_QTY)  
                    and len(variables.INVOICE)
                and isDate(variables.REC_DATE)>
                <cfset isEverythingValidated = true>
            </cfif>      

            <cfif isEverythingValidated>
                   RUN UPDATE
            <cfelse>
                  SKIP UPDATE
            </cfif>
0
DJPr0
Asked:
DJPr0
  • 2
2 Solutions
 
nathansCommented:
<cfif variables.REC_QTY neq "" and variables.INVOICE neq "">
   <cfif variables.REC_DATE eq "">
      <cfset variables.REC_DATE = now()>
      <!--- If you want the date formated use the commented out code --->
      <!--- <cfset variables.REC_DATE = DateFormat(now(),'mm/dd/yyyy')> ---
   </cfif>
</cfif>

Is this want you are meaning or did I misunderstand?
0
 
_agx_Commented:
<cfset isEverythingValidated = false>
<!--- if the other two fields are populated --->
<cfif isNumeric(variables.REC_QTY) and len(variables.INVOICE)>
        <!--- valid if a date was entered OR is empty --->
        <cfset variables.isDateEmpty = len(trim(variables.REC_DATE)) eq 0>
        <cfif isDate(variables.REC_DATE) OR variables.isDateEmpty>
              <cfset isEverythingValidated = true>
        </cfif>        
        <!--- if it's empty, substitute date and time now --->
        <cfif variables.isDateEmpty>
               <cfset variables.REC_DATE = now()>
        </cfif>
</cfif>
0
 
_agx_Commented:
<cfset variables.REC_DATE = now()>

One note about the code above. I assumed REC_DATE is a datetime column *not* a string:
        ie   UPDATE yourtable
              SET      REC_DATE = <cfqueryparam value="#variables.REC_DATE#"
                                                 cfsqltype="cf_sql_date">
              , ....

If it's actually a string/varchar (hopefully not ..) you'll have to dateformat #now()# value first to get the "mm/dd/yyyy" part only.
0
 
DJPr0Author Commented:
Thanks _agx_!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now