3 Replies Latest reply on May 15, 2009 5:29 AM by EvolvedDSM

    Need this explained: Two objects reporting back same values...

    EvolvedDSM Level 2

      Hello,

       

      I have a datagrid and a form binded to the selectedItem record of the datagrid.  I need to update the values according to whatever is typed into the form fields.

       

      I use a CFC to handle the update.  I have a newBean class which holds the new values from the form and also an oldBean object which holds old values to verify I'm updating the correct record.

       

      In my actionscript I have two objects, newObj and oldObj.  Here is how I set them up in my update function.

      var oldObj:Object = new Object;

      oldObj = dg.selectedItem;

      var newObj:Object = newObject;

      newObj = dg.selectedItem;

      newObj.value1 = field1.text;

      newObj.value2 = field2.text;

      newObj.value3 = field3.text;

      remoteObject.update(oldObj, newObj);

       

      So, I set the oldObj to the selectedItem and then I set the newObj to that same item, which initially should give them the current values of the selectedItem in the datagrid.  Then I set the values of newObj to match the form fields.  Lastly I have a remoteObject method "update()" send the two objects to the CFC I mentioned above.  When I found out it wasn't updating properly, I set up a cfdump to my email to list the values for both oldObj and newObj.

       

      Results:

      newObj properly takes all the new values as intended.  oldObj strangely also takes on the new values, yet I never assigned oldObj to accept the new values.  So why is oldObj also accepting the values in the form fields when there's no visible connection between oldObj and any of the fields?

        • 1. Re: Need this explained: Two objects reporting back same values...
          EvolvedDSM Level 2

          Oh, and if you are wondering why I even bother setting newObj to dg.selectedItem, it's because I need the object to hold additional record values that do not get changed in the form (values like recordID, which should never be modified).

           

          What does work:

          If I choose not to set newObj to the selectedItem and instead just set the unchanged values to dg.selectedItem.value.

           

          So now I'm doing this....

                  var oldObj:Object = new Object;
                  oldObj = dg.selectedItem;

                  var newObj:Object = new Object;
                  newObj.ID = dg.selectedItem.ID;
                  newObj.value1 = field1.text;
                  newObj.value2 = field2.text;
                  newObj.value3 = field3.text;

                  remoteObject.update(oldObj,upObj);

          When I do this, oldObj holds on to the selectedItem values rather than taking on the new values I've given newObj.

           

          I still don't know why oldObj took on the new values in my original design.  This is what I'd like to know.  thanks

          • 2. Re: Need this explained: Two objects reporting back same values...
            Michael Borbor Level 4

            Could you upload a sample app including CFCs to do some testing?

             

            Sincerely,

             

            Michael

            • 3. Re: Need this explained: Two objects reporting back same values...
              EvolvedDSM Level 2

              I don't have an external site I can load this on.  It's a component of an application as well, so I'd have to publish the whole thing.  Instead, I'll just post all relevent code.  Notes: cfc is generated from the ColdFusion wizard.  apptTable is a valueObject that contains the structure of the record data.

               

              The Remote Object:

                  <mx:RemoteObject id="apptRO" endpoint="http://10.118.40.100:85/flex2gateway/"
                      destination="ColdFusion" source="cms.cfc.apptTableDAO" showBusyCursor="true" fault="Alert.show(event.fault.faultString, 'Error')">
                      <mx:method name="read" result="apptsReceived(event)"/>
                      <mx:method name="create" result="apptCreated(event)"/>
                      <mx:method name="update" result="apptUpdated(event)"/>
                      <mx:method name="deleted" result="apptDeleted(event)"/>
                  </mx:RemoteObject>

               

              The original update function in Flex:

                  private var upObj:apptTable;
                  private var oldObj:Object;

                  private function updateAppt():void{
                      oldObj = new Object;
                      oldObj = dg.selectedItem;
                      upObj = new apptTable;
                      upObj = apptTable(dg.selectedItem);
                      upObj.clientName = clientNameInput.text;
                      upObj.topic = topicInput.text;
                      upObj.info = infoInput.text;
                      upObj.apptDate = dateInput.text;
                      upObj.apptTime = timeInput.text + ' ' + ampmCbx.value.toString();
                      apptRO.update(oldObj,upObj);
                  }

               

              The UPDATE function in the CFC:

                  <cffunction name="update" output="false" access="public">
                      <cfargument name="oldBean" required="true" type="cms.cfc.apptTable">
                      <cfargument name="newBean" required="true" type="cms.cfc.apptTable">
                      <cfset var qUpdate="">

               

                      <cfquery name="qUpdate" datasource="cmsdb" result="status">
                          update dbo.apptTable
                          set clientName = <cfqueryparam value="#arguments.newBean.getclientName()#" cfsqltype="CF_SQL_VARCHAR" />,
                              topic = <cfqueryparam value="#arguments.newBean.gettopic()#" cfsqltype="CF_SQL_VARCHAR" />,
                              info = <cfqueryparam value="#arguments.newBean.getinfo()#" cfsqltype="CF_SQL_VARCHAR" />,
                              dateSubmitted = <cfqueryparam value="#arguments.newBean.getdateSubmitted()#" cfsqltype="CF_SQL_VARCHAR" />,
                              apptDate = <cfqueryparam value="#arguments.newBean.getapptDate()#" cfsqltype="CF_SQL_VARCHAR" />,
                              apptTime = <cfqueryparam value="#arguments.newBean.getapptTime()#" cfsqltype="CF_SQL_VARCHAR" />,
                              userID = <cfqueryparam value="#arguments.newBean.getuserID()#" cfsqltype="CF_SQL_BIGINT" null="#iif((arguments.newBean.getuserID() eq ""), de("yes"), de("no"))#" />,
                              userName = <cfqueryparam value="#arguments.newBean.getuserName()#" cfsqltype="CF_SQL_VARCHAR" />
                          where apptID = <cfqueryparam value="#arguments.oldBean.getapptID()#" cfsqltype="CF_SQL_BIGINT" />
                            and clientName = <cfqueryparam value="#arguments.oldBean.getclientName()#" cfsqltype="CF_SQL_VARCHAR" />
                            and topic = <cfqueryparam value="#arguments.oldBean.gettopic()#" cfsqltype="CF_SQL_VARCHAR" />
                            and info = <cfqueryparam value="#arguments.oldBean.getinfo()#" cfsqltype="CF_SQL_VARCHAR" />
                            and dateSubmitted = <cfqueryparam value="#arguments.oldBean.getdateSubmitted()#" cfsqltype="CF_SQL_VARCHAR" />
                            and apptDate = <cfqueryparam value="#arguments.oldBean.getapptDate()#" cfsqltype="CF_SQL_VARCHAR" />
                            and apptTime = <cfqueryparam value="#arguments.oldBean.getapptTime()#" cfsqltype="CF_SQL_VARCHAR" />
                            and userID = <cfqueryparam value="#arguments.oldBean.getuserID()#" cfsqltype="CF_SQL_BIGINT" />
                            and userName = <cfqueryparam value="#arguments.oldBean.getuserName()#" cfsqltype="CF_SQL_VARCHAR" />
                      </cfquery>
                      <!--- if we didn't affect a single record, the update failed --->
                      <cfquery name="qUpdateResult" datasource="cmsdb"  result="status">
                          select apptID
                          from dbo.apptTable
                          where apptID = <cfqueryparam value="#arguments.newBean.getapptID()#" cfsqltype="CF_SQL_BIGINT" />
                            and clientName = <cfqueryparam value="#arguments.newBean.getclientName()#" cfsqltype="CF_SQL_VARCHAR" />
                            and topic = <cfqueryparam value="#arguments.newBean.gettopic()#" cfsqltype="CF_SQL_VARCHAR" />
                            and info = <cfqueryparam value="#arguments.newBean.getinfo()#" cfsqltype="CF_SQL_VARCHAR" />
                            and dateSubmitted = <cfqueryparam value="#arguments.newBean.getdateSubmitted()#" cfsqltype="CF_SQL_VARCHAR" />
                            and apptDate = <cfqueryparam value="#arguments.newBean.getapptDate()#" cfsqltype="CF_SQL_VARCHAR" />
                            and apptTime = <cfqueryparam value="#arguments.newBean.getapptTime()#" cfsqltype="CF_SQL_VARCHAR" />
                            and userID = <cfqueryparam value="#arguments.newBean.getuserID()#" cfsqltype="CF_SQL_BIGINT" />
                            and userName = <cfqueryparam value="#arguments.newBean.getuserName()#" cfsqltype="CF_SQL_VARCHAR" />
                      </cfquery>
                     
                      <cfif status.recordcount EQ 0>
                         
                          <cfthrow type="conflict" message="Unable to update record">
                      </cfif>
                      <cfreturn arguments.newBean />
                  </cffunction>

               

              If you look at the update code from the CFC, you can see all my table columns, some of which are not present in my form fields because they should never change.

               

              Fields in form: clientNameInput, topicInput, infoInput, dateInput, timeInput, and a combo box for am/pm.

              Columns not in form: apptID, dateSubmitted, userID, userName

               

              Working code:

              private function updateAppt():void{
                      oldObj = new Object;
                      oldObj = dg.selectedItem;
                      upObj = new apptTable;
                      upObj.apptID = dg.selectedItem.apptID;
                      upObj.clientName = clientNameInput.text;
                      upObj.topic = topicInput.text;
                      upObj.info = infoInput.text;
                      upObj.dateSubmitted = dg.selectedItem.dateSubmitted;
                      upObj.apptDate = dateInput.text;
                      upObj.apptTime = timeInput.text + ' ' + ampmCbx.value.toString();
                      upObj.userID = dg.selectedItem.userID;
                      upObj.userName = dg.selectedItem.userName;
                      apptRO.update(oldObj,upObj);
                  }

              This code does not set upObj to the selectedItem record of the dg.  Instead it just accepts the values of the form fields and any values in the datagrid that do not change.