Welcome, Guest. Please login or register.
Did you miss your activation email?
31 Jul 2010, 11:41:03 UTC
Forum home
+  flexdeveloper.eu Forum
|-+  Flex and ActionScript 3.0
| |-+  Tools, Examples & Tutorials (Moderators: JMWhittaker, thewarpedcoder, James)
| | |-+  populate flex Tree control from salesforce using Arraycollection
« previous next »
Pages: [1] Print
Author Topic: populate flex Tree control from salesforce using Arraycollection (Read 2060 times)
ktdsm
Newbie FD
*
Posts: 5


« on: 26 Oct 2009, 02:41:19 UTC »

Hi All, I want to share my code with everyone.I have a business object in Salesforce whose  parent is the same Custom Object.I am populating my tree control from salesforce data through Arraycollection.
I have a action script class :
Code:
public class clsTreeNode
{   import mx.utils.StringUtil;
import mx.collections.ArrayCollection;
import mx.controls.listClasses.ListData;
import mx.controls.treeClasses.TreeListData;
import mx.controls.Alert;

import com.salesforce.results.QueryResult;
import com.salesforce.results.LoginResult;
import com.salesforce.objects.SObject;
 import mx.utils.ObjectUtil;
import com.salesforce.AsyncResponder;
import com.salesforce.objects.LoginRequest;
<salesforce1:Connection id="forceTest" serverUrl="https://test.salesforce.com/services/Soap/u/16.0">    
   </salesforce1:Connection>
public function clsTreeNode(id :String,name:String,force:Connection)
{
  // Make a SOQL
id = id; // Set the ID
Name =   name; // Set the Name

force.query("Select g.Folder_Name__c, g.Id, g.Parent_Folder__c from Gene_Document_Folder__c g where g.Parent_Folder__c in ( '" + id + "')",new AsyncResponder(function(qrNode:QueryResult):void
       {    
        var childNodes:ArrayCollection = new ArrayCollection();
        if (qrNode.records != null) {
          for(var j:int =0; j < qrNode.records.length ; j++){
         
          // Alert.show("Alert after Name" + qrNode.records[j].Folder_Name__c );
          childNodes.addItem(new clsTreeNode(qrNode.records[j].Id,qrNode.records[j].Folder_Name__c,force));
                     
                  }
          children = childNodes;
         // Alert.show('The current Node is : ' + Name + ' the number of child nodes are ' +  childNodes.length);        
        } else {
               
        }
         }));
     }                
       
        



     public var id:String;
     public  var Name:String;
     public var children : ArrayCollection=new ArrayCollection();

     public function addChild(node : clsTreeNode) : void {
              children.addItem(node);
     }
  
     public function setChildren(nodes : ArrayCollection) : void {
      if (this.children == null)
          this.children = new ArrayCollection();
          children = nodes;
     }

  
     public function getChildren():ArrayCollection {
  
       return children;
  
     }



}
here is my Mxml code:

Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
applicationComplete="init()"
 themeColor="#E7ECEF" cornerRadius="3" width="1084" height="640"
  xmlns:salesforce1="http://www.salesforce.com/">
 
  <salesforce1:Connection id="force" serverUrl="https://test.salesforce.com/services/Soap/u/16.0">
 
 
  </salesforce1:Connection>



<mx:Script >
<![CDATA[
import mx.states.AddChild;

import mx.controls.List;
import mx.controls.listClasses.ListData;
import mx.controls.treeClasses.TreeListData;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import com.salesforce.results.QueryResult;
import com.salesforce.results.LoginResult;
import com.salesforce.objects.SObject;
import mx.utils.ObjectUtil;
import com.salesforce.AsyncResponder;
import com.salesforce.objects.LoginRequest;
import mx.controls.treeClasses.*;
import mx.collections.CursorBookmark;
import mx.collections.ICollectionView;
import mx.collections.IViewCursor;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;

[Bindable]
            private var arrGeneFolder:ArrayCollection=new ArrayCollection();
            [Bindable]          
private var arrGeneDoc:ArrayCollection=new ArrayCollection();
[Bindable]
private var fIds:ArrayCollection=new ArrayCollection();


private function init():void
{  
var lr:LoginRequest = new LoginRequest();
lr.username= "salesforceusername@yahoo.com";
lr.password = "pwd+securitytoken";
lr.callback = new AsyncResponder(loginHandler,handleFault);
force.login(lr);

}

private function loginHandler(result:LoginResult):void
{  
// Alert.show('Apex:');
var mNode:clsTreeNode;
force.query("Select g.Folder_Name__c, g.Id, g.Parent_Folder__c from Gene_Document_Folder__c g where g.Parent_Folder__c =null ",new AsyncResponder(function(qrNode:QueryResult):void
{

for(var j:int =0; j < qrNode.records.length ; j++){


mNode=new clsTreeNode(qrNode.records[j].Id,qrNode.records[j].Folder_Name__c,force);

arrGeneDoc.addItem(mNode);



}
 


geneTree.dataProvider=Object(arrGeneDoc);

}));

}


private function handleFault(fault:Object):void {
                Alert.show(ObjectUtil.toString(fault));
              }
              
              
          
]]>
</mx:Script>


<mx:Tree x="10" y="33" id="geneTree"    showRoot="false"  labelField="Name"  width="330" height="408">

</mx:Tree>








<mx:Label x="10" y="423" text="Label" id="lbl" width="1040"/>
</mx:Application>
« Last Edit: 26 Oct 2009, 10:45:50 UTC by flexy » Logged
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,155


Recovering Coffee Addict & Adobe Expert


WWW
« Reply #1 on: 26 Oct 2009, 10:45:59 UTC »

Many thanks for sharing  Smiley
Logged

Pages: [1] Print
« previous next »
Share this on: Twitter Twitter Del.icio.us del.icio.us Digg Digg
Jump to:

©2006-2010 Flexdeveloper.eu/Jodie O'Rourke. All rights reserved.
Adobe®, Adobe® Flash™, Adobe® AIR™ and Adobe® Flex™ are registered trademarks of Adobe Systems Incorporated in the United States and other countries. All rights reserved.

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC