+ Start a Discussion
AmuellerAmueller 

Search by email and then retrieve ID

Hello,

 

I am trying to capture an email address with a form and then search for it in saelsforce and if it exists I will convert the lead. I can convert the lead via API but I can't seem to lookup by a form value, like $emailx. All I have to go on is

 

$result = $crmHandle->search('FIND {Joe Moke} IN Name Fields RETURNING Lead(Name, Phone)');



Which doesn't seem to work. There's isn't much PHP salesforce API help that I have found,

 

Any help would be appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Park Walker (TAGL)Park Walker (TAGL)

You cannot reference a variable outside the scope in which it was defined. In this case, your $i. I'd suggest a good introductory PHP book get if you plan on doing much more of this. The investment in reading time will save you a lot of time in the future and possibly reduce the frustration level.

 

$crmHandle = new SforceEnterpriseClient();
$crmHandle->createConnection(SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');
$crmHandle->login($USERNAME, $PASSWORD);
$emailx= $_POST['email'];    
$query = "Select l.Id From Lead l where Email='$emailx'";
$queryResponse = $crmHandle->query($query);

print('<pre>');
print_r($queryResponse);
print('</pre>');

if ($queryResponse->size > 0)
{
   $lead = $queryResponse->records[0];    
   $leadConvert = new stdClass();
   $leadConvert->leadId = $lead->Id;
   $leadConvert->convertedStatus = 'Closed - Converted';
   $leadConvert->doNotCreateOpportunity = false;
   $leadConvert->overwriteLeadSource = false;
   $leadConvert->sendNotificationEmail = true;
   $result = $crmHandle->convertLead($leadConvert);  
}

 

Park

All Answers

Park Walker (TAGL)Park Walker (TAGL)

You're almost there. Using your example, your statement might read as follows:

 

$result = $crmHandle->search("FIND ($emailx) IN EMAIL FIELDS RETURNING Contact(Id), Lead(Id)");

 

A couple of things to note. It is possible for an email address to contain a dash (-) which is a reserved character in the search call and must be escaped by a backslash. Also, I've used double quotes which allows me to place the string to be searched for inside the string. 

 

Take a look at the search function in API documentation for more information and for the format of the results.

 

Park

AmuellerAmueller

Hi Thanks fro your help.

 

Still having problems.

 

Based on my research this should work:

 

    $result = $crmHandle->search("FIND ($emailx) IN EMAIL FIELDS RETURNING Lead(Id)");
   
 $leadConvert = new stdClass();
 $leadConvert->leadId = $result->id;
 $leadConvert->convertedStatus = 'Closed - Converted';
 $leadConvert->doNotCreateOpportunity = false;
 $leadConvert->overwriteLeadSource = false;
 $leadConvert->sendNotificationEmail = true;
 $result = $crmHandle->convertLead($leadConvert);

 

but it's not. Any suggestions? I can't find much in the API documentation for PHP. I only have what is on here. http://phpandsalesforce.com/2009/06/16/welcome-to-php-and-salesforce/

Park Walker (TAGL)Park Walker (TAGL)

Not much to go on there. Have you looked at what the search call is returning? It looks like you are using the Enterprise WSDL. Is that correct? Are you sure you have the correct endpoint? What error are you getting?

 

The documentation you're looking for is here.

 

Park

AmuellerAmueller

Thanks for trying to help.

 

I tried the search using that documantiation but they didn't work either. Plus none of the search examples are pulling from a variable.  I am using Entertprise. I'm not much of a programmer when I try and print the result it doesn't give me anything.

 

What I'm trying to do is if someone fills out a form it takes the email from the form, looks it up in salesforce and pulls out the result, uses the id from the result to plug into the convert formula to convert the lead.

 

$emailx= $_POST['email'];  // form email result of course

 

$result = $crmHandle->search("FIND ($emailx) IN EMAIL FIELDS RETURNING Lead(Id)"); // then the string gets looked up in salesforce

 

this is where i have problems based on the examples I have...

   
 $leadConvert = new stdClass();
 $leadConvert->leadId = $result->id; // it appears that $result->id would be the id from the search above and that will enable me to convert the lead. it seems like it's missing something and obviously something isn't working because it doesn't convert the lead. If I just put the ID I want to convert in that place it will work.

 

 $leadConvert->convertedStatus = 'Closed - Converted';
 $leadConvert->doNotCreateOpportunity = false;
 $leadConvert->overwriteLeadSource = false;
 $leadConvert->sendNotificationEmail = true;
 $result = $crmHandle->convertLead($leadConvert);

 

Park Walker (TAGL)Park Walker (TAGL)

The API documentation is here. If you look up the search call you'll see that it returns an array of sObjects. You'll need to grab one of those, assuming that there are any, and get it's Id. The use of a variable rather than a string makes no difference. As noted in the documentation, single 'words' - such as an email address - do not need to be quoted. As I noted earlier, you will run into problems with some email addresses unless you take care to escape dashes.

 

If you add a print_r($result); after calling search you will see the information that the seach call is returning in a structured format. It can be very helpful in understanding how to access the data you are looking for from the result variable.

 

Other than that, what you have should work.

 

Park

AmuellerAmueller

Okay thanks for trying to help. I've kind of given up with donig it that way. it doesn't return anything for some reason. So I'm doing it another way. Which get's me the ID I need but I can't seem to figure out how to get the query result to the convert lead spot.

 

$crmHandle = new SforceEnterpriseClient();
    $crmHandle->createConnection(SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');
    $crmHandle->login($USERNAME, $PASSWORD);
    $emailx= $_POST['email'];    
      $query = "Select Name, Email From Lead l limit 20";
      $something = "Select l.Id From Lead l where Email='$emailx'";
      $createResponse = $crmHandle->query($something);
      
foreach ($createResponse as $createResult){
//print_r($createResponse);
    //print("<br>");
    for ($i=0, $num_result=count($createResult); $i < $num_result; $i++){
        echo "<tr>";
          echo "<td>";
            print($createResult[$i]-> Id); // this prints the ID I need but I can't get it below. Even if I get rid of the print stuff it won't go below.
        echo "</td>";
        echo "<td>";    
   }
}

 $leadConvert = new stdClass();
 $leadConvert->leadId = $createResult[$i]-> Id; // What do I have to do to get the value here so I can convert it?
 $leadConvert->convertedStatus = 'Closed - Converted';
 $leadConvert->doNotCreateOpportunity = false;
 $leadConvert->overwriteLeadSource = false;
 $leadConvert->sendNotificationEmail = true;
 $result = $crmHandle->convertLead($leadConvert);  

Park Walker (TAGL)Park Walker (TAGL)

You cannot reference a variable outside the scope in which it was defined. In this case, your $i. I'd suggest a good introductory PHP book get if you plan on doing much more of this. The investment in reading time will save you a lot of time in the future and possibly reduce the frustration level.

 

$crmHandle = new SforceEnterpriseClient();
$crmHandle->createConnection(SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');
$crmHandle->login($USERNAME, $PASSWORD);
$emailx= $_POST['email'];    
$query = "Select l.Id From Lead l where Email='$emailx'";
$queryResponse = $crmHandle->query($query);

print('<pre>');
print_r($queryResponse);
print('</pre>');

if ($queryResponse->size > 0)
{
   $lead = $queryResponse->records[0];    
   $leadConvert = new stdClass();
   $leadConvert->leadId = $lead->Id;
   $leadConvert->convertedStatus = 'Closed - Converted';
   $leadConvert->doNotCreateOpportunity = false;
   $leadConvert->overwriteLeadSource = false;
   $leadConvert->sendNotificationEmail = true;
   $result = $crmHandle->convertLead($leadConvert);  
}

 

Park

This was selected as the best answer
AmuellerAmueller

Well while I'm not a programmer I have coded some PHP and ASP. I worked with Infusionsoft's API and that was much simplier than this and I'm basically doing the same things I did with that. Every sample code I have gotten from Salesforce API has not worked and has needed to be modified in order to work.

 

Thank you for the code and your help. While it did work I still can't achieve my end result. I really don't use the print command much but when it prints it kills the rest of the script and it doesn't convert the ID. If I take away the print command the script doesn't complete either. I don't know why it seems to be so complicated to do a simple lookup, take the variable and plug it in somwhere else. I've never had so many problems doing this before.

AmuellerAmueller

Okay it actually did work, I had the wrong converted status which caused the problem. Thanks for all your help.