HOWTO: Lookup email address related to a record via Web Services

bsoremsugar —  November 16, 2010 — 7 Comments

I was going thru the forums today and noticed this post, asking about how to get the email address of a record thru SOAP. Since last week I showed an example of searching by email address, I thought I would add this example of looking up email addresses.

Again, my example will use the PHP 5 SoapClient. I’ll do my example against the v3 web services API. First, we’ll authenicate to the instance and add a contact record.

 'http://instancename/service/v3/soap.php',
        "uri" => 'http://www.sugarcrm.com/sugarcrm',
        )
    );

// Login
$user_name = 'myuser';
$user_password = 'mypass';

try {
    $info = $soapClient->login(
        array(
            'user_name' => $user_name,
            'password'  => md5($user_password),
            )
        );
}
catch (SoapFault $fault) {
    die("Sorry, the service returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring.".");
}

$session = $info->id;
echo "Successful Login! Session ID {$session}n";

// Create Contact
try {
    $info = $soapClient->set_entry(
        $session,
        'Contacts',
        array(
            array('name' => 'first_name', 'value' => 'Foo'),
            array('name' => 'last_name', 'value' => 'Bar'),
            array('name' => 'description', 'value' => 'This is a contact created from a REST web services call'),
            array('name' => 'email1', 'value' => 'foo@bar.com'),
            array('name' => 'email2', 'value' => 'foo@bar.net'),
            )
        );
}
catch (SoapFault $fault) {
    die("Sorry, the service returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring.".");
}

$contactId = $info->id;
echo "Created Contact ID {$contactId}n";

Now with a record in the system, let’s search for it using the get_entry_list() method. The important part of this example is the 7th parameter to it, ‘link_name_to_fields_array’, which indicates that we want to retrieve the given relationship at the same time ( for this example ‘email_addresses’ ), and then which fields in that relationship we would like to retrieved ( ‘id’, ‘email_address’, ‘opt_out’, and ‘primary_address’ for this example ).

// Now let's try to find the contact we just created
try {
    $info = $soapClient->get_entry_list(
        $session,
        'Contacts',
        "contacts.id = '{$contactId}'",
        '',
        0,
        array('first_name','last_name',),
        array(array('name' =>  'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address'))),
        10,
        -1
        );
}
catch (SoapFault $fault) {
    die("Sorry, the service returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring."." . $fault->detail);
}

for ( $i = 0; $i result_count; $i++ ) {
    foreach ( $info->entry_list[$i]->name_value_list as $field ) {
        $name = $field->name;
        $$name = $field->value;
    }
    echo "Contact Name is $first_name $last_namen";

    foreach ( $info->relationship_list[$i][0]->records as $emailRecord ) {
        foreach ( $emailRecord as $field ) {
            $name = $field->name;
            $$name = $field->value;
        }
        echo "Contact has an email address of $email_addressn";
    }
}

Since the data comes back in two parts ( the main record data in the entry_list part, the email address in the relationship_list ), we’ll need to carefully iterate over the data to match the two parts together.

The same pattern can be used not only for email addresses, but any related module. So for example, you could retrieve an account and all the related contacts using the same method and similar code.

7 responses to HOWTO: Lookup email address related to a record via Web Services

  1. 

    Hello,

    Should work with version 2 of the web service? When we try, we do not get the related mails, nor a mistake. It is as if unaware the parameter “link_name_to_fields_array”.

    Thanks.

    • 

      Yes, i’m having trouble returning any relationships at all.
      even when using the example as directly mentioned in the rest documentation for
      This is for v2 and v3

      Returning the email address of a client is very important for a CRM. Why so complicated?

  2. 

    Do you have any examples of how to use the 
    link_name_to_fields_array from Java using JSON?

    I am simply trying to retrieve the account name of a contact.

Trackbacks and Pingbacks:

  1. How to update email field with SOAP ? - SugarCRM Forums - October 23, 2011

    [...] [...]

  2. How to link a record to a relate field - June 14, 2012

    [...] [...]

  3. Retrieve Email Id on user permission - July 24, 2012

    [...] [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s