+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12
This is a discussion on relationship queries within the Salesforce Coding Discussions forums, part of the Salesforce category; Hi I haven't done a relationship query before so i have a
  1. #1
    sager@cmc.ca is offline Junior Member
    Join Date
    Jan 2010
    Posts
    11

    Angry relationship queries

    Hi
    I haven't done a relationship query before so i have a dumb question.
    I am querying Contact object and want to get Account Name as well.
    I don't know how...using the code below...to access the Account Name value.
    HELP
    PHP Code:
    <?php
    require_once("c:/php/sforce/soapclient/SforcePartnerClient.php");
    require_once (
    'c:/php/sforce/soapclient/SforceHeaderOptions.php');
    require_once (
    'c:/php/sforce/userAuth.php');
    $conn= new SforcePartnerClient();
    $conn->createconnection("partner.wsdl.xml");
    $mylogin=$conn->login("username","password");

    $query="Select Contact.Id,Contacts.FirstName,Contact.Contacts_ID__c,Contact.org_code__c,Contact.Account.Name from Contact where contacts_id__c='21666'";
    $response=$conn->query($query);
    foreach (
    $response->records as $result) {
      
    $name=$result->fields->FirstName
       $sforce_id
    =$result->Id;
       
        
    $updateFields = array(
         
    'Id'=>$sforce_id,
                      
                 
                 );
       
    $sObjectCustom =  new SObject();
        
    $sObjectCustom->type 'Contact'
       
    $sObjectCustom->fields $updateFields;
      
    $createResponse $conn->update(array($sObjectCustom));  
       
    }
      
    ?>

  2. #2
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    607
    Blog Entries
    16
    what version off php are you using? I understand that you have to be running php 5.3 or greater to get the relationship queries to work. I have been able to get this to work in the past using lowerr versions. Let me dig that code up and see if I can paste it. I know that in the past that if I have queries that I am not sure are going to work, i have used the salesforce query tool to run my queries to make sure they work and then just plug them into the code.

    ~Mike

  3. #3
    sager@cmc.ca is offline Junior Member
    Join Date
    Jan 2010
    Posts
    11

    php version

    hi mike
    yes, i have a version that allows me to do relationship queries
    my question is a simplistic one
    how do i extract the actual account name from the results so i can use that value to update a table in SQL Server?

    eg. my returned object looks like

    SObject Object ( [type] => Contact [fields] => stdClass Object ( [Contacts_ID__c] => someOrgCode [Org_Code__c] => MB [1] => SObject Object ( [type] => Account [fields] => stdClass Object ( [Name] => Some organization ) ) ) [Id] => salesforceId )


    thanks
    tam

  4. #4
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    607
    Blog Entries
    16
    I will do my best to have some code that will allow this some time Monday

    ~Mike

  5. #5
    sager@cmc.ca is offline Junior Member
    Join Date
    Jan 2010
    Posts
    11

    thanks

    hi mike
    thanks
    any help with this is appreciated
    tam

  6. #6
    sager@cmc.ca is offline Junior Member
    Join Date
    Jan 2010
    Posts
    11

    Question any update

    Hi Mike
    Just wondering if you are able to offer any resources on this?
    Thanks
    Tammy

  7. #7
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    607
    Blog Entries
    16
    Sorry Tammy I have been hammered at work. I will do my best to post something here on Monday. Again sorry for the delay

  8. #8
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    607
    Blog Entries
    16
    Okay I was able to get this to work:

    1) I have a login script to salesforce which establishes the connection via the API
    PHP Code:
    <?php


    require_once ('/users/msimonds/public_html/includes/soapclient_new/SforcePartnerClient.php');
    require_once (
    '/users/msimonds/public_html/includes/soapclient_new/SforceHeaderOptions.php');
    // Login to salesforce.com


    $login "you@youremail.com";
    $password "password";
    $wsdl "/users/msimonds/public_html/includes/soapclient_new/partner.wsdl.xml";

    $client = new SforcePartnerClient();
    $client->createConnection($wsdl);

    try
    {
        
    $loginResult $client->login($login$password);
       
        if (
    $loginResult->passwordExpired)
        {
            
    $client null;
        }
    }
    catch (
    exception $e)
    {
        
        
    $client null;
        echo 
    '<pre>'.print_r($e,true).'</pre>';
    }
    ?>
    the I use require_once() in the script where I place the query

    PHP Code:
    <?php
    ini_set
    ("soap.wsdl_cache_enabled""0");

    require_once (
    '/users/msimonds/public_html/includes/simonds.inc');

    $soql "Select a.Id, a.Name, (Select Name From Contacts) from Account a where a.name = 'Pioneer' ";

    $records get_records($client$soql);


    foreach (
    $records as $result)
    {
        echo 
    '<pre>' print_r($resulttrue) . '</pre>'
        exit;

    }


    function 
    get_records(&$connection, &$query)
    {
        
    $queryOptions = new QueryOptions(2000);

        
    $response = new QueryResult($connection->query($query));
        
    // if the size is zero, where done
        
    if ($response->size 0)
        {
            
    $records $response->records;
            
    // Cycles through additional responses if the number of records
            // exceeds the batch size
            
    while (!$response->done)
            {
                
    set_time_limit(100);
                
    $response $connection->queryMore($response->queryLocator);
                
    $records array_merge($products$response->records);
            }
        }
        return 
    $records;
    }
    ?>
    This worked for me:

    Code:
    SObject Object
    (
        [type] => Account
        [fields] => stdClass Object
            (
                [Name] => Pioneer
            )
    
        [Id] => 0015000000OKR1cAAH
        [queryResult] => Array
            (
                [0] => QueryResult Object
                    (
                        [queryLocator] => 
                        [done] => 1
                        [records] => Array
                            (
                                [0] => SObject Object
                                    (
                                        [type] => Contact
                                        [fields] => stdClass Object
                                            (
                                                [Name] => Todd Golay
                                            )
    
                                    )
    
                            )
    
                        [size] => 1
                    )
    
            )
    
    )
    Hope this helps!!

    Make sure you check the paths if you use this script(s)


    ~Mike

  9. #9
    sager@cmc.ca is offline Junior Member
    Join Date
    Jan 2010
    Posts
    11

    thanks for trying

    Hi Mike

    No apology required. I appreciate you taking the time to try to help.

    I actually can already see the information as you display it.
    My dumb question is not how do I write out the sobject but rather, how to i populate a variable with just the name of the person.

    eg.

    $soql = "Select a.Id, a.Name,a.Country (Select Name From Contacts) from Account a where a.Country = 'Canada'";
    $records = get_records($conn, $soql);

    foreach ($records as $result)
    {
    populate a variable with Account Name
    populate a variable with Name of Contact

    exit;
    }

    Hope this makes sense
    Tam

  10. #10
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    607
    Blog Entries
    16
    So you have to modify the original foreach and add some sub-foreach statements:

    Look at the new code:

    PHP Code:
    <?php
    ini_set
    ("soap.wsdl_cache_enabled""0");

    require_once (
    '/users/msimonds/public_html/includes/simonds.inc');

    $soql "Select a.Id, a.Name, (Select Name From Contacts) from Account a where a.name = 'Pioneer' ";

    $records get_records($client$soql);


    foreach (
    $records as $result)
    {
        
        foreach (
    $result->fields as $r)
        {
           
    //echo '<pre>' . print_r($r, true) . '</pre>';
           
    $account_name $r;
           echo 
    $account_name '<br />'
           
        }
        foreach(
    $result->queryResult as $data)
        {
            foreach(
    $data->records as $more_data)
            {
                 
    //echo '<pre>' . print_r($more_data, true) . '</pre>';
                 
    $contact_name $more_data->fields->Name;
                 echo 
    $contact_name;
            }
            
        }
        

    }


    function 
    get_records(&$connection, &$query)
    {
        
    $queryOptions = new QueryOptions(2000);

        
    $response = new QueryResult($connection->query($query));
        
    // if the size is zero, where done
        
    if ($response->size 0)
        {
            
    $records $response->records;
            
    // Cycles through additional responses if the number of records
            // exceeds the batch size
            
    while (!$response->done)
            {
                
    set_time_limit(100);
                
    $response $connection->queryMore($response->queryLocator);
                
    $records array_merge($products$response->records);
            }
        }
        return 
    $records;
    }
    ?>
    let me know if you understand that Tam

    BTW, Are you going to DreamForce this year?

    ~Mike

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Where clause in Child Relationship Select statement?
    By jarrettcoggin in forum Salesforce Coding Discussions
    Replies: 3
    Last Post: 08-04-2009, 08:01 AM
  2. Parsing confusing results from a relationship query
    By bakum in forum Salesforce Coding Discussions
    Replies: 2
    Last Post: 08-04-2008, 07:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO 3.5.2