3

I want the signup form data to go to Salesforce leads table.

I have a WordPress website where i have made a signup form using php code, and whenever an user signs up, the form data goes to the Backendless users table, for this i have used the REST API and cURL. So now i want that same data to go to the Salesforce Lead table as well.. I haven't found any REST API solution to this problem and i need a code for the REST API or cURL or a plugin which could do my work and push the users data into Salesforce.
I could attach a backendless with WordPress code for reference

<?php

if(isset($_POST['Submit'])) 
    {

    //extract data from the post
    //set POST variabless
    $fname = $_POST['fname'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    //retrieve the data from the backendless table and check if the email is present in it.
    $service_url1 = 'https://api.backendless.com/{APP-KEY}/{REST-API}/data/Users';
    $curl1 = curl_init($service_url1);
    curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
    $curl_response1 = curl_exec($curl1);

    if ($curl_response1 === false) {
        $info1 = curl_getinfo($curl1);
        curl_close($curl1);
        die('error occured during curl exec. Additioanl info: ' . var_export($info1));
    }
    curl_close($curl1);
    //getting the array which is stored in $curl_response1, putting it in decoded and puiling out only the email field and arranging it properly.
    $decoded = json_decode($curl_response1);
    $res1 = array_column($decoded, 'email');
    $res2 = implode("', '", $res1);
    //checking if the new user email is present in the array or not.
    if (in_array($email, $res1)) 
        {
            echo"<div style='text-align:center'>You have already registered before.</div>"; 

        }       

    else
        {
            //since its not present in the backendless table we will now add it to the backendless table by POST.
            $ch = curl_init('https://api.backendless.com/{APP-KEY}/{REST-API}/data/Users');
            $data = array("fname" => $fname, "email" => $email, "password" => $password);

            $data_string = json_encode($data);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
            );
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            //execute post
            $result = curl_exec($ch);
            //close connection
            curl_close($ch);

            echo"<div style='text-align:center'>Thank you for signing up with us ".$fname.".<br> </div>";
            echo"<div style='text-align:center'>We will be sending you an email shortly.</div>";

        }

}

else
{

?>
<html>
<head>
<style> 
input[type='text'], input[type='email'], input[type='password'] { border : 0.5px solid gray; margin-bottom: 5px; }
.signup-submit {margin-top:20px;}
input[type='password'] { 
    padding-top: 8px;
    padding-bottom: 8px;
}
</style>
</head>
<body style="text-align:left;">

<div>

    <p align="center"> Please complete the following form to register as a user </p>

    <form method="post" id="signup">

        <table>
            <tr>
                <div class="col-sm-12 col-xs-12">
                    <span class="wpcf7-form-control-wrap fname">
                        <td>
                            <label>Enter Name * : </label>
                        </td>                   
                        <td>
                            <input type="text" name="fname" value="" size="15" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required wpcf7-validates-as-name required " aria-required="true" aria-invalid="false" required />
                        </td>
                    </span>
                </div>  
            </tr>


            <tr>
                <div class="col-sm-12 col-xs-12">
                    <span class="wpcf7-form-control-wrap email">
                        <td>
                            <label>Enter Email * : </label>
                        </td>                   
                        <td>
                            <input type="email" name="email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required wpcf7-validates-as-email required email" aria-required="true" aria-invalid="false" required />
                        </td>
                    </span>
                </div>  
            </tr>

                <tr>
                <div class="col-sm-12 col-xs-12">
                    <span class="wpcf7-form-control-wrap password">
                        <td>
                            <label>Enter Password * : </label>
                        </td>                   
                        <td>
                            <input type="password" name="password"  value="" id="pwinput"  minlength="8" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required" aria-required="true" aria-invalid="false" autocomplete="new-password" required />

                            <input type="checkbox" onclick="myFunction()"><span class="checkboxtext">Show Password</span>
                        </td>
                        <script>
                            function myFunction() 
                            {
                                var x = document.getElementById("pwinput");
                                if (x.type === "password") 
                                    {
                                        x.type = "text";
                                    } 
                                else 
                                    {
                                        x.type = "password";
                                    }
                            }
                        </script>
                    </span>
                </div>  
            </tr>


            <tr>
                <div class="col-sm-12 col-xs-12">

                        <td>
                            <input type="submit" value="Submit" name="Submit" class="wpcf7-form-control wpcf7-submit signup-submit center Submit">
                        </td>
                </div>  
            </tr>
        </table>  
    </form>
</div>
</body>
</html>

<?php
 }
?>
Shaurya
  • 41
  • 4

2 Answers2

0

Basic

Salesforce offers Web-to-Lead and Web-to-Case solutions. They're pretty easy to use (you don't need to login to SF first), you click through SF to generate a HTTP POST form. And then looking at it you can easily figure out how to make a POST request.

They're also pretty primitive, they work by passing your unique SF org id to a generic url that will dispatch the request - so it's something you definitely want to hide server-side, maybe protect with CAPTCHA... The moment somebody learns your SF org id they can eaisly spam you with thousands of fake Leads. Read the help associated to Web-to-Lead, limitations and decide if it sounds right.

enter image description here

SF will generate a HTML form out of it

<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">

<input type=hidden name="oid" value="00D7000000{hidden}">
<input type=hidden name="retURL" value="https://stackoverflow.com">

<label for="first_name">First Name</label><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>

<label for="last_name">Last Name</label><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>
...

So now you have URL and parameters for your POST. Slap Content-Type:application/x-www-form-urlencoded header on it for good measure and you're good to go. Maybe keep the org ID in some configuration so you can easily change between test and prod SF instances.

Advanced

If you need something fancier (load not only Lead but also other tables, maybe attachments, maybe check if same email exists and should be updated first...) - you'd need proper API access, SOAP or REST. That'd mean two POST calls, one to obtain session ID, one for actual save. You can hand-craft it all or use one of SF PHP libraries like https://developer.salesforce.com/index.php?title=Getting_Started_with_the_Force.com_Toolkit_for_PHP&oldid=51397. If you hand-craft - sample login message is here https://stackoverflow.com/a/56034159/313628 or go to REST API documentation.

And once you're in (you can also use https://workbench.developerforce.com/login.php -> REST explorer or https://developer.salesforce.com/docs/api-explorer/sobject/Lead)

enter image description here

eyescream
  • 18,088
  • 2
  • 34
  • 46
0

Hi im posting the solution which worked for me, in order to help someone in the future!

<?php

if(isset($_POST['Submit'])) 
    {

    //extract data from the post
    //set POST variabless
    $fname = $_POST['fname'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    //retrieve the data from the backendless table and check if the email is present in it.
    $service_url1 = 'https://api.backendless.com/<App-id>/<Rest-api-key>/data/Users';
    $curl1 = curl_init($service_url1);
    curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
    $curl_response1 = curl_exec($curl1);

    if ($curl_response1 === false) {
        $info1 = curl_getinfo($curl1);
        curl_close($curl1);
        die('error occured during curl exec. Additioanl info: ' . var_export($info1));
    }
    curl_close($curl1);
    //getting the array which is stored in $curl_response1, putting it in decoded and pulling out only the email field and arranging it properly.
    $decoded = json_decode($curl_response1);
    $res1 = array_column($decoded, 'email');
    $res2 = implode("', '", $res1);

    //checking if the new user email is present in the array or not.
    if (in_array($email, $res1)) 
        {
            echo"<div style='text-align:center'>You have already registered before.</div>"; 
            echo '<th><strong><u><center><a target="_blank" href="javascript:window.close();">Click here to return.</a></center></u></strong></th>';        
        }       

    else
        {
            //since its not present in the backendless table we will now add it to the backendless table by POST.
            $ch = curl_init('https://api.backendless.com/<App-id>/<Rest-api-key>/data/Users');
            $data = array("fname" => $fname, "email" => $email, "password" => $password);

            $data_string = json_encode($data);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
            );
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            //execute post
            $result = curl_exec($ch);
            //close connection
            curl_close($ch);

            echo"<div style='text-align:center'>Thank you for signing up with us ".$fname.".<br> </div>";
            echo"<div style='text-align:center'>We will be sending you an email shortly.</div>";
            echo '<th><strong><u><center><a style="color:#2c666d" target="_blank" href="javascript:window.close();">Click here to return.</a></center></u></th>';
        }

}

else
{

?>
<html>
<head>
<style> 
input[type='text'], input[type='email'], input[type='password'] { border : 0.5px solid gray; margin-bottom: 5px; }
.signup-submit {margin-top:20px;}
input[type='password'] { 
    padding-top: 8px;
    padding-bottom: 8px;
}
</style>
</head>
<body style="text-align:left;">

<div>

    <p align="center"> Please complete the following form to register as a user </p>

    <form method="post" id="signup">

        <table>
            <tr>
                <div>
                    <span class="fname">
                        <td>
                            <label>Enter Name * : </label>
                        </td>                   
                        <td>
                            <input type="text" name="fname" value="" size="15" required />
                        </td>
                    </span>
                </div>  
            </tr>


            <tr>
                <div>
                    <span class="email">
                        <td>
                            <label>Enter Email * : </label>
                        </td>                   
                        <td>
                            <input type="email" name="email" value="" size="40" required />
                        </td>
                    </span>
                </div>  
            </tr>

                <tr>
                <div>
                    <span class="password">
                        <td>
                            <label>Enter Password * : </label>
                        </td>                   
                        <td>
                            <input type="password" name="password"  value="" minlength="8" required />

                            <input type="checkbox" onclick="myFunction()"><span class="checkboxtext">Show Password</span>
                        </td>
                    </span>
                </div>  
            </tr>

            <tr>
                <div>

                        <td>
                            <input type="submit" value="Submit" name="Submit" class="Submit">
                        </td>
                </div>  
            </tr>
        </table>  
    </form>
</div>
</body>
</html>

<?php
 }
?>
Shaurya
  • 41
  • 4