function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
zeezackzeezack 

A kind of do while loop?

I have in the database a grand parent, parent, child association

 

BERRY GROUP

                      > BERRY PLC

                                        > BERRY LTD

 

I want to be able to find the parent value of the grand dady of the entire family all the time...

 

Say I look at BERRY LTD, it contains the parent id to BERRY PLC.... BERRY PLC contains the parent id to BERRY GROUP. BERY GROUP has no parent id.

 

Now

 

 this is my code so far for pulling out the value of a parent id.

 

 

 

    echo'- parent is '.$ParentId.'<br/>';
    $query10 = "SELECT Id, Name, ParentId from Account WHERE Id ='$ParentId'";
    $queryResult10 = $mySforceConnection->query($query10);
    $records10 = $queryResult10->records;
   
    if(!empty($records10))
    {
        foreach ($records10 as $record1) {           
            $sObject = new SObject($record1);
            $introid10 = $sObject->Id;
            $introname10 =$sObject->fields->Name;
            $introname10 = eregi_replace("&" ,"&amp;", $introname10);
            echo'<br/>............. grandparent '.$introname10.' - id is "'.$introid10.'"<br/>';
            //$ParentId
             return $this->$introname10;
        }
    }

 

 

if a parent id exists in this search... I want to push it back into the code to try and find the great great grand dady of any family

 

I am trying to sort it out in a function...but I am getting undefined function errors.

 

 

zeezackzeezack

I've got a function now... which will search an id and return a parentid if its present...

 

I now need to cycle this script so it will keep searching the id's returned until there is no parentid to return... thus then we will have the great great great grand daddy.

 

How do I go about doing this?

 

 

 

 

class Foo
{
    function greatgranddad($ParentId, $mySforceConnection)
    {
        $query10 = "SELECT Id, Name, ParentId from Account WHERE Id ='$ParentId'";
        $queryResult10 = $mySforceConnection->query($query10);
        $records10 = $queryResult10->records;
       
        if(!empty($records10))
        {
            foreach ($records10 as $record1) {           
                $sObject = new SObject($record1);
                $introid10 = $sObject->Id;
                $introname10 =$sObject->fields->Name;
                $introname10 = eregi_replace("&" ,"&amp;", $introname10);
                //echo'<br/>.............parent '.$introname10.' - id is "'.$introid10.'"<br/>';
                //$ParentId
                return $introid10;
            }
        }
    }
}

 

 

$a = new Foo();    

$mparent = $a->greatgranddad($ParentId, $mySforceConnection);

 

 

 

 

zeezackzeezack
This is an urgent request people, love some ideas soon.
zeezackzeezack
What is the best way of finding the highest branch of the tree
Park Walker (TAGL)Park Walker (TAGL)

zeezack wrote:
What is the best way of finding the highest branch of the tree

It's a basic programming concept know as recursion. Most introductory programming texts should have a discussion of it.

 

It looks something like this:

 

function GetParent(person)

{

if (null == person.parent) return person;

else return GetParent(person.parent);

}

 

ultimateAncestor = GetParent(someone);

Message Edited by Redsummit on 03-25-2009 09:03 AM
zeezackzeezack

mm. I've tried setting it up.. but I am getting a timeout.

 

 

                                   

function GetParent($ParentId, $mySforceConnection) { $query10 = "SELECT Id, Name, ParentId from Account WHERE Id ='$currentId'"; $queryResult10 = $mySforceConnection->query($query10); $records10 = $queryResult10->records; if(!empty($records10)) { foreach ($records10 as $record1) { $sObject = new SObject($record1); $ParentId = $sObject->fields->ParentId; } } if($ParentId.parent =="") { return $ParentId; echo'i am in if parent null<br/>'; } else { return $this->GetParent($ParentId.parent, $mySforceConnection); echo'i am a else - found grandparent<br/>'; } } $a = new Foo(); $ultimateAncestor = $a->GetParent($introid, $mySforceConnection); echo'NEW ULTIMATE ancestor '.$ultimateAncestor.'';

 


 

Message Edited by zeezack on 03-26-2009 03:34 AM
Park Walker (TAGL)Park Walker (TAGL)

Try this:

 

 

function GetParent($TargetId, $mySforceConnection){

$query = "SELECT Id, Name, ParentId from Account WHERE Id ='$TargetId'";

$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;

 

if(empty($records))

{

return $TargetId;

}

$ParentId = $record[0]->fields->ParentId;

 

if($ParentId == "")

{

echo 'i am in if parent null<br/>';

return $TargetId;

}

else

{

echo 'i am in else - found grandparent<br/>';

return $this->GetParent($ParentId, $mySforceConnection);

}

}

 

$ultimateAncestor = GetParent($introid, $mySforceConnection);

echo'NEW ULTIMATE ancestor ' . $ultimateAncestor . '';

 

 

I don't have a hierarchy to test this against, but it should show you where the problems were with the original. I do not know if $ParentId will be an empty string if there isn't one. You may need to play with that a bit.

 

Park 

 

Message Edited by Redsummit on 03-26-2009 09:40 AM
zeezackzeezack
Thanks for that man. Its close but its returning its own id?
zeezackzeezack

Still not got this working right...

 

It either comes up with an error or returns itself.