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 :
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:
<?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>