This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 4

Salesforce and Machform guide


  1. morsecode
    Member

    Hello everyone,

    First I want to thank the people at Machform for not having monthly or per user pricing! Long live one-time fee option software! I'd rather not pay for expensive monthly salesforce apps so I thought I'd take stab at connecting machform and salesforce myself :)

    On with the show ...

    I wanted to make a post about my journey in connecting machform with salesforce. It's actually quite simple. My goal was to submit the form data to a custom object within salesforce. Lets get started.

    1. Download the Salesforce PHP toolkit from here:
    https://github.com/developerforce/Force.com-Toolkit-for-PHP/archive/master.zip

    2. Unzip. Then upload the "soapclient" folder to your Machform directory. For example, /machform/soapclient/.

    3. Create a file named "sfconfig.php" with the following contents:

    <?php
    /*salesforce connection*/
    require_once (SOAP_CLIENT_BASEDIR.'/SforceEnterpriseClient.php');
    require_once (SOAP_CLIENT_BASEDIR.'/SforcePartnerClient.php');
    require_once (SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');
    $username="xxx";
    $pass="xxx";
    $token="xxx";
    $mySforceConnection = new SforcePartnerClient();
    $mySoapClient 	 	= $mySforceConnection->createConnection(SOAP_CLIENT_BASEDIR.'/partner.wsdl.xml');
    $mylogin 			= $mySforceConnection->login($username,$pass.$token );
    /*salesforce connection*/
    ?>

    Be sure to fill in the $username, $pass and $token variables with your own details.

    4. Upload "sfconfig.php" to the "includes" directory. For example, "machform/includes/sfconfig.php".

    5. Edit includes/post-functions.php. Around line 5277 find:

    $process_result['status'] = true;

    6. Below that paste:

    if ($form_id == XXX) {
            define("SOAP_CLIENT_BASEDIR", "soapclient");
            include("sfconfig.php");
            //Creating the Candidate Object
            $createFields = array(
                'Full_Name__c' => $table_data['element_2'],
                'Email__c' => $table_data['element_3']
            );
            $sObject = new stdclass();
            $sObject->fields = $createFields;
            $sObject->type = 'Candidate__c';
            //Submitting the Candidate to Salesforce
            $upsertResponse = $mySforceConnection->upsert("Email__c", array ($sObject));
    }

    Note: Replace XXX with your form id (you can find your form id in your address bar when viewing your form from the browser: mysite.com/machform/view.php?id=XXX

    Note2: see below if your form is a multipage form.

    7. Let's go over the above code:

    a)

    $createFields = array(
                'Full_Name__c' => $table_data['element_2'],
                'Email__c' => $table_data['element_3']
            );

    i) "Full_Name__c" is the API Name of the Full Name field I created within Salesforce.

    ii) "$table_data['element_2']" - element_2 is the input name machform created when it created the form. For example: <input id="element_2" class="element text medium" type="text" value="" name="element_2"></input>. Right click the page in your browser and "view source" to locate the input name you are looking for.

    b)

    $sObject->type = 'Candidate__c';

    Replace "Candidate__c" with whatever salesforce object you are working with. For example, Lead, Account, Custom__c

    c)

    //Submitting the Candidate to Salesforce
            $upsertResponse = $mySforceConnection->upsert("Email__c", array ($sObject));

    Notice I've used the "upsert" function, which is the same as the "insert" function, except it will replace an existing record if the "Email__c" data is the same.

    That's it!

    BONUS:

    What if I have a multi-page form? This might not be the best way, but this worked for me:

    1. Edit includes/post-functions.php. Around line 5277 find:

    $process_result['status'] = true;

    Insert below:

    if ($input['page_number'] == 1) {
     $_SESSION['is_valid_user'] = true;
     $_SESSION['form_data_page_1'] = $table_data;
    }

    This captures the data from page 1 and puts it into the $_SESSION['form_data_page_1'] array. You can add more lines below this to capture other pages:

    if ($input['page_number'] == 2) {
     $_SESSION['is_valid_user'] = true;
     $_SESSION['form_data_page_2'] = $table_data;
    }

    2. Then instead of pasting the block of code from step 6 here, find:

    }else{ //if this is the last page

    around Line 5629

    and paste below:

    if ($form_id == XXX) {
        define("SOAP_CLIENT_BASEDIR", "soapclient");
        include("sfconfig.php");
        //Creating the Candidate Object
        $createFields = array(
            'Full_Name__c' => $_SESSION['form_data_page_1']['element_1'],
            'Email__c' => $_SESSION['form_data_page_1']['element_2']
        );
        $sObject = new stdclass();
        $sObject->fields = $createFields;
        $sObject->type = 'Candidate__c';
        //Submitting the Candidate to Salesforce
        $upsertResponse = $mySforceConnection->upsert("Email__c", array ($sObject));
    }

    Notice: $_SESSION['form_data_page_1']['element_1']

    Hope this helps!

    Thanks.

    Posted 9 years ago #
  2. yuniar

    Wow! that's very thorough explanation! Thank you so much for sharing this!
    Many people have asked for this and I'll direct them to this post :-)

    Thanks again!


    MachForm Founder

    Posted 9 years ago #
  3. petersj77
    Member

    In Machform 4.2 there are only 4506 lines in "includes/post-functions.php" so can't find line 5277.

    There are 2 occurrences of "$process_result['status'] = true;" in the file

    Please advise as to where I should add the code

    Thanks

    Posted 9 years ago #
  4. morsecode
    Member

    Insert below:

    if(empty($error_elements) && empty($process_result['custom_error'])){		
    
    			$process_result['status'] = true;
    Posted 9 years ago #
  5. offersnet
    Member

    what about posting the form data into other CRMs besides SalesForce? We have a "home grown" Lead management / distribution platform as well as our own ESP front end that i would love to know more about understanding how to post the form data into them. Thanks in advance for any suggestions / help!

    Posted 9 years ago #
  6. yuniar

    offersnet -- you can use the "send form data to another site" option. Here's an example:
    http://www.appnitro.com/blog-mailchimp-integration


    MachForm Founder

    Posted 9 years ago #

RSS feed for this topic

Reply