You need to sign in to do that
Don't have an account?
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("&" ,"&", $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.
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("&" ,"&", $introname10);
//echo'<br/>.............parent '.$introname10.' - id is "'.$introid10.'"<br/>';
//$ParentId
return $introid10;
}
}
}
}
$a = new Foo();
$mparent = $a->greatgranddad($ParentId, $mySforceConnection);
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);
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.'';
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
Still not got this working right...
It either comes up with an error or returns itself.