You need to sign in to do that
Don't have an account?
Guru 91
Help in writing test class?
Can anyone help with test class
public with sharing class AccountStructure extends Hierarchy
{
/**
* Return ObjectStructureMap to page
* @return asm
*/
public List<AccountHierarchyNode> getObjectStructure()
{
if ( currentId == null ) {
currentId = System.currentPageReference().getParameters().get( 'id' );
}
System.assertNotEquals( currentId, null, 'sObject ID must be provided' );
return formatObjectStructure( CurrentId );
}
/**
* Query Account from top down to build the AccountHierarchyNode
* @param currentId
* @return asm
*/
public List<AccountHierarchyNode> formatObjectStructure( String currentId )
{
List<AccountHierarchyNode> accountNodes = new List<AccountHierarchyNode>();
return (List<AccountHierarchyNode>) generateHierarchy(accountNodes);
}
protected override HierarchyNode getAllNodes()
{
idToNode.clear();
List<Account> accounts = new List<Account>();
List<Id> currentParents = new List<Id>();
HierarchyNode topNode = null;
Integer level = 0;
Boolean noChildren = false;
//Find highest level obejct in the structure
currentParents.add( GetTopElement( currentId ) );
//Loop though all children
while ( !noChildren ){
if( level == 0 ){
//Change below
accounts = [SELECT Type, Site, ParentId, OwnerId, Name, Industry FROM Account WHERE Id IN :currentParents ORDER BY Name];
}
else {
//Change below
accounts = [SELECT Type, Site, ParentId, OwnerId, Name, Industry FROM Account WHERE ParentID IN :currentParents ORDER BY Name];
}
if( accounts.size() == 0 )
{
noChildren = true;
}
else
{
currentParents.clear();
for (Account account : accounts)
{
//Change below
HierarchyNode parentNode = idToNode.get(account.ParentId);
AccountHierarchyNode node = new AccountHierarchyNode(false, account);
idToNode.put(account.Id, node);
currentParents.add( account.id );
if (parentNode != null)
node.setParent(parentNode);
else
topNode = node;
}
maxLevel.add( level );
level++;
}
}
return topNode;
}
/**
* Find the tom most element in Heirarchy
* @return objId
*/
public String GetTopElement( String objId ){
Boolean top = false;
while ( !top ) {
//Change below
Account a = [ Select a.Id, a.ParentId From Account a where a.Id =: objId limit 1 ];
if ( a.ParentID != null ) {
objId = a.ParentID;
}
else {
top = true;
}
}
return objId ;
}
public with sharing class AccountHierarchyNode extends HierarchyNode
{
public Account account;
public Account getAccount() { return account; }
public void setAccount( Account a ) { this.account = a; }
public AccountHierarchyNode(Boolean currentNode, Account account)
{
super(currentNode);
this.account = account;
}
}
}
public with sharing class AccountStructure extends Hierarchy
{
/**
* Return ObjectStructureMap to page
* @return asm
*/
public List<AccountHierarchyNode> getObjectStructure()
{
if ( currentId == null ) {
currentId = System.currentPageReference().getParameters().get( 'id' );
}
System.assertNotEquals( currentId, null, 'sObject ID must be provided' );
return formatObjectStructure( CurrentId );
}
/**
* Query Account from top down to build the AccountHierarchyNode
* @param currentId
* @return asm
*/
public List<AccountHierarchyNode> formatObjectStructure( String currentId )
{
List<AccountHierarchyNode> accountNodes = new List<AccountHierarchyNode>();
return (List<AccountHierarchyNode>) generateHierarchy(accountNodes);
}
protected override HierarchyNode getAllNodes()
{
idToNode.clear();
List<Account> accounts = new List<Account>();
List<Id> currentParents = new List<Id>();
HierarchyNode topNode = null;
Integer level = 0;
Boolean noChildren = false;
//Find highest level obejct in the structure
currentParents.add( GetTopElement( currentId ) );
//Loop though all children
while ( !noChildren ){
if( level == 0 ){
//Change below
accounts = [SELECT Type, Site, ParentId, OwnerId, Name, Industry FROM Account WHERE Id IN :currentParents ORDER BY Name];
}
else {
//Change below
accounts = [SELECT Type, Site, ParentId, OwnerId, Name, Industry FROM Account WHERE ParentID IN :currentParents ORDER BY Name];
}
if( accounts.size() == 0 )
{
noChildren = true;
}
else
{
currentParents.clear();
for (Account account : accounts)
{
//Change below
HierarchyNode parentNode = idToNode.get(account.ParentId);
AccountHierarchyNode node = new AccountHierarchyNode(false, account);
idToNode.put(account.Id, node);
currentParents.add( account.id );
if (parentNode != null)
node.setParent(parentNode);
else
topNode = node;
}
maxLevel.add( level );
level++;
}
}
return topNode;
}
/**
* Find the tom most element in Heirarchy
* @return objId
*/
public String GetTopElement( String objId ){
Boolean top = false;
while ( !top ) {
//Change below
Account a = [ Select a.Id, a.ParentId From Account a where a.Id =: objId limit 1 ];
if ( a.ParentID != null ) {
objId = a.ParentID;
}
else {
top = true;
}
}
return objId ;
}
public with sharing class AccountHierarchyNode extends HierarchyNode
{
public Account account;
public Account getAccount() { return account; }
public void setAccount( Account a ) { this.account = a; }
public AccountHierarchyNode(Boolean currentNode, Account account)
{
super(currentNode);
this.account = account;
}
}
}
All Answers