Author: pbrocks

  • How To Implement a Counter and Increment When An Action Occurs

    1. ) Open up your text editing program
    2. ) Then copy and paste the following script into the editor
    NOTE: Lines beginning with “//” denote comments explaining what the subsequent chunk of code does. They also sometimes point out things you can change or customize in the code. Reading through them will help you get a judo grip on the code and its nuances and let you cater the code to your needs.

    [php]

    <!–?php require("isdk.php"); $myApp = new iSDK; $myApp—>cfgCon("connectionName");

    // Get Contact ID from POST variable

    $conId = $_POST[‘Id’];
    // Set Video Last View Date to Today (_LastViewDate )

    $infusionDate = date("YmdTH:i:s");
    $data = array(‘_LastViewDate’ => $infusionDate);
    $update = $myApp->dsUpdate("Contact", $conId, $data);

    // Increment Video Watched Counter By One (_VideoWatchedCounter )
    // Load current value then add new value

    $returnFields = array(‘_VideoWatchedCounter’);
    $conDat = $myApp->dsLoad("Contact", $conId, $returnFields);

    $counter = $conDat[‘_VideoWatchedCounter’];
    //echo "Counter is ".$counter."
    ";

    $counter++;
    //echo "Now counter is ".$counter."
    ";

    $data = array(‘_VideoWatchedCounter’ => $counter);
    $update = $myApp->dsUpdate("Contact", $conId, $data);
    //echo $update;
    ?>

    [/php]

    Okay so lets break it down now. You know how to connect to Infusionsoft, so lets find out what we have to do to understand and get this script running. 

    [php]$conId = $_POST[‘Id’];
    [/php]

    Firstly we want to figure out what’s happening here to start with. When I read line 9, I’m seeing a variable (a container to store data) $conId, which has been created to hold the customer ID [‘Id’] number. The customer ID number is sent to the server by the $_POST variable. Now the $_POST variable is a global variable which has been pre defined by some of the geeky guys who originally wrote php. It. Always contains variables from a previous page , normally a form or something that contains user input and a button has been clicked.

    So now that we have the data past through and then wrapped up in the container $conId we need to do something useful with it.

    And that useful something will be to create a date of when the specific user watched a video … and then we’ll put that date into a custom field you’ve created in Infusionsoft.

    Lets do this now.

    [php]$infusionDate = date("YmdTH:i:s");
    $data = array(‘_LastViewDate’ => $infusionDate);
    $update = $myApp->dsUpdate("Contact", $conId, $data);[/php]

    We first have to take care of the date side of things, so we go ahead and make up a variable called $infusionDate and inside that variable we want to say “ Everytime you come across $infusionDate, we’d like it to create today’s date in this specific format. (Year, month, day)

    Now the next thing we have to do is tell it which field we want to insert the date in. In this case we have created a custom field called LastViewDate. Now remember when you’re inserting data or specifying a custom field in a script, you have to precede it with an under score, like so: _LastViewDate.

    So we create a variable called $data and assign it or in other words store the data inside of the two variables $infusionDate and LastViewDate to the container $data.

    Did you get me on that one?

    I’ll say it in other words if you didn’t. The $data variable will hold the information stored inside of $infusionDate and LastViewDate.

    We now have to execute the three lots of data we’ve stored in our containers and put them in their rightful places.

    [php]$update = $myApp->dsUpdate("Contact", $conId, $data);[/php]

    So looking at line 14, this is where it all happens. We want to put all of the data we have collected and created so far (The customer ID, the date and the custom field we want to put the date in.) and then house them inside of a variable called $update and when the parser reads this, it will execute the function like so:

    1. Open the connection

    2. Use the correct infusionsoft method – dsUpdate

    3. Find the Contact table

    4. Find this specific contact

    5. Find the field inside of the variable $data (LastViewDate) and then find today’s date ($infusionDate ) and insert it in this format (y,m,d)

    Also, while you’re in there, can you execute the following …

    Firstly, find out how many times this video has been watched by this contact and secondly increment it by one, because it looks like they’ve just watched it again.

    [php]$returnFields = array(‘_VideoWatchedCounter’);
    $conDat = $myApp->dsLoad("Contact", $conId, $returnFields);

    $counter = $conDat[‘_VideoWatchedCounter’];
    //echo "Counter is ".$counter."
    ";

    $counter++;
    //echo "Now counter is ".$counter."
    ";

    $data = array(‘_VideoWatchedCounter’ => $counter);
    $update = $myApp->dsUpdate("Contact", $conId, $data);
    //echo $update;
    ?>[/php]

    So what we have to do here is a couple of things.

    [php]$returnFields = array(‘_VideoWatchedCounter’);
    $conDat = $myApp->dsLoad("Contact", $conId, $returnFields);[/php]

    Firstly name a variable $returnFields and put the information from the custom field VideoWatchedCounter in it.

    Next, we need to assign another variable $conDat to go to the Table named Contact and fined the customer fields who watched the video and bring back the data in the custom field VideoWatchedCounter.

    [php]$counter = $conDat[‘_VideoWatchedCounter’];
    //echo "Counter is ".$counter."
    ";

    $counter++;
    //echo "Now counter is ".$counter."
    ";[/php]

    So now that we’ve returned the data in the custom field VideoWatchedCounter inside of the $conDat variable, line 22 says we now need to assign it to a new variable called $counter and then increment it by one, which is the ++ signs after the variable on line 25.

    You’ll notice there is an echo sign behind the forward slashes, this is because we always use this to test the script and have it read back to us.

    [php]$data = array(‘_VideoWatchedCounter’ => $counter);
    $update = $myApp->dsUpdate("Contact", $conId, $data);
    //echo $update;
    ?>[/php]

    So now that we’ve the updated the count number for videos … and we did this by returning the existing number in the custom field (VideoWatchedCounter) and incremented it by one. We have to now put it back, so we assign this new data to the already created variable called $data. Note: Variables can be re-assigned within the same script. Line 28 says get the new number which has now been incremented by one and assign it to the custom field VideoWatchedCounter.

    Now it hasn’t been executed yet and this is the last step in the script. Line 29 says makeup a variable and call it $update use the method dsUpdate, then look for the table Contact and find this specific contact, which is stored inside $conId, once you’ve done that find the custom field VideoWatchedCounter and put the data that is stored inside of the container $counter in there.

    Again you’ll see the echo statement in green. You should always be testing. Just cancel out the two forward slashes, upload your script to the server and then type in the url to test if the script works. You should see the new figure telling you how many times the video has been watched.

  • How To Send An Email Using An Infusionsoft Template

    [php]

    // Add connection info here
    <pre>$sendTo = array(12,16,22);
    $templateID = 3380
    $app->sendTemplate($sendTo,$templateID);
    [/php]

  • How To Change the Opportunity Stage

    [php]</p>
    <p>&lt;?php </p>
    <p>echo &quot;Hello World! &lt;br/&gt;&lt;br/&gt;&quot;; </p>
    <p>// Connect to Infusionsoft</p>
    <p>require_once(&quot;isdk.php&quot;);<br />
    $app = new iSDK;<br />
    if ($app-&gt;cfgCon(&quot;sandbox&quot;)) { // Make sure this matches your connection name in conn.cfg</p>
    <p>// Recipe starts here</p>
    <p> // Current date in Infusionsoft-friendly format</p>
    <p> $currentDate = date_format(date_create(), ‘YmdTH:i:s’);<br />
    echo &quot;You connected at $currentDate &lt;br/&gt;&lt;br/&gt;&quot;;</p>
    <p> $contactId = 129; // Dritte Dawg<br />
    $newStageId = 24; // Change this StageID to whatever you want it to change it to</p>
    <p> // API Call -&gt; Query the lead table to find the opportunities for this contact</p>
    <p> $qryFields = array(‘Id’, ‘ContactID’, ‘StageID’);<br />
    $query = array(‘ContactID’ =&gt; $contactId);<br />
    $myOpp = $app-&gt;dsQuery(&quot;Lead&quot;,5,0,$query,$qryFields);</p>
    <p> //Remove these three lines when done testing<br />
    echo &quot;&lt;pre&gt;&quot;;<br />
    print_r($myOpp);<br />
    echo &quot;&lt;/pre&gt;&quot;;</p>
    <p>// Update the opportunity with the new stage</p>
    <p> $update= array(‘StageID’ =&gt; $newStageId);<br />
    $oppID = $myOpp[0][‘Id’]; // This line assumes there is only one opp fpr this contact<br />
    $opp = $app-&gt;dsUpdate(&quot;Lead&quot;, $oppID, $update);</p>
    <p> // Remove this line when done testing<br />
    echo &quot;&lt;br/&gt;Updated Opp ID# &quot;.$opp.&quot; &lt;br/&gt;&quot;;</p>
    <p>} else {<br />
    echo &quot;Not Connected…&quot;;<br />
    } </p>
    <p> [/php]

  • How To Change the Owner ID

    [php]

    <?php

    // Connect to the Infusionsoft API
    require("isdk.php");
    $app = new iSDK;
    $app->cfgCon("sandbox");

    // Notice the names in red match up to the names sent in the http post snippet

    $contactId = $_POST[‘contactId’];
    $repId = $_POST[‘repId’];

    // API call -> update contact record for $ContactId with the information in $rep

    $rep = array(‘OwnerID’ => $repId);
    $owner = $app->dsUpdate("Contact", $contactId, $rep);

    &nbsp;

    [/php]

  • How To Set A Custom Field To A Date

    1) Open your favorite text editing program

    2)  Copy and paste the following PHP code into the blank text editor file:

    NOTE: Lines beginning with “//” denote comments explaining what the subsequent chunk of code does. They also sometimes point out things you can change or customize in the code. Reading through them will help you get a judo grip on the code and its nuances and let you cater the code to your needs.

    [php]
    <?php
    //Connects to the Infusionsoft API. This is a standard way to start any script that uses the Infusionsoft API. No tweaks necessary here!
    require("isdk.php");
    $app = new iSDK;
    $app->cfgCon("connectionName");

    //The variable (dateFieldName) stores the name of the custom Infusionsoft field where the date will be posted (NameOfYourDateField in this case).
    // Custom fields in Infusionsoft always have an underscore before their name when being referenced by a script.
    $dateFieldName = ‘_NameOfYourDateField’;

    //Get post data for Contact ID.
    $contactId = $_POST[‘Id’];

    //Stores today’s date in YearMonthDate format.
    $currentDate = date("YmdTH:i:s");

    //Grabs the data from Infusionsoft for your custom field.
    $returnFields = array($dateFieldName);

    //dsLoad is an Infusionsoft API method that grabs data from the specified field in Infusionsoft.
    $contacts = $app->dsLoad("Contact", $contactId, $returnFields);

    //If NameOfYourDateField is currently empty/unset, then set it to today’s date. If it’s already set, add one year to the date
    if (isset ($contacts[‘_NameOfYourDateField’]) == FALSE) {
    $conDat = array($dateFieldName => $currentDate);
    } else {
    $classOf = strtotime($contacts[‘_NameOfYourDateField’]);
    $yrPlusOne = date("YmdTH:i:s", strtotime(‘+1 year’, $classOf));
    $conDat = array($dateFieldName => $yrPlusOne);
    }

    //Another API method that updates a given contact with the data in the second variable passed.
    $conID = $app->updateCon($contactId, $conDat);
    ?>

    [/php]

    3) Now that you’ve got the code in your text editor, it’s time to edit it to suit your needs/wants/whimsies. Most of the script should work just fine as-is, but it does need a few customizations here and there. First, let’s look at this line:

    [php] $dateFieldName = ‘_NameOfYourDateField’; [/php]

    As the comment says, dateFieldName is a variable that will represent the custom field where you want to put the date in Infusionsoft. You need to change ‘_NameOfYourDateField’ to ‘_WhateverYourCustomFieldIsActuallyCalled’. For instance, if you wanted to put the date in a custom Infusionsoft field called MyCustomDate, you would put ‘_MyCustomDate’. You also need to change this anywhere else the code references ‘_NameOfYourDateField’, so in your text editor use the Find-and-Replace function to change all instances of ‘_NameOfYourDateField’ to ‘_MyCustomDate’ or whatever you named your field.

    4) Jump down to the line

    [php]$currentDate = date("YmdTH:i:s");[/php]

    Right now, this bit of PHP will store the current date in a variable aptly named currentDate in the format YYYYMMDD followed by the time-zone abbreviation followed by the time in 24-hour/military time format. So if it was 3:19 PM on September 20th, 2013 in Eastern Standard Time, the script would format the date as 20130920EST15:19:00. This is handled in the code by the part in quotes that looks like a cat ran across the keyboard (“YmdTH:i:s”) and you can change it around however you want. This is how it presently breaks down:

    Y – The 4-digit year

    m – The 2-digit month with leading zeroes (

    d – The 2-digit day with leading zeroes.

    T – Time-zone abbreviation

    H – The hour using a 24-hour clock

    i – Minutes

    s – seconds

    You could re-arrange it to (“m d Y H:i:s T”) and the script would then format the date as 09 20 2013 15:19:00 EST. You can also change how the date appears by switching the letters you use.  For instance, changing the ‘m’ to ‘F’ would make the month in the date appear as “September” instead of “09.” For a full list of all possible formatting options, refer to the PHP manual here : http://php.net/manual/en/function.date.php.

    5) The last line you might want to tweak is:

    [php]  $yrPlusOne = date("YmdTH:i:s", strtotime(‘+1 year’, $classOf)); [/php]

    Currently, this script will add one year to the contact’s custom date field every time you run this script. So if you initially set the contact’s date to 09/20/2013 (formatted however you chose, obviously), the next time you run the script that date will be updated to 09/20/2014. You can change ‘+1 year’ to ‘+1 month’ or ‘+1 day’ depending on what increment you want. Go ahead, try it! It’s like magic. Internet magic.

    And that’s it! Congratulations, you have a custom date in a custom field that goes up by a custom increment every time you run this script. It’s so custom that “custom” probably doesn’t sound like a real word to you anymore!

  • How To Capitalize First Name

    1) Open your favorite text editing program

    2)  Copy and paste the following PHP code into the blank text editor file:

    NOTE: Lines beginning with “//” denote comments explaining what the subsequent chunk of code does. They also sometimes point out things you can change or customize in the code. Reading through them will help you get a judo grip on the code and its nuances and let you cater the code to your needs.

    [php]<?php

    echo "Hello World! <br/><br/>";

    // Connect to Infusionsoft

    require_once("isdk.php");
    $app = new iSDK;
    if ($app->cfgCon("sandbox")) {

    // Current date in Infusionsoft-friendly format

    $currentDate = date_format(date_create(), ‘YmdTH:i:s’);
    echo "You connected at $currentDate <br/><br/>";

    // Recipe starts here

    // Assign POST variables using ternary operator instead of IF statement
    // If your POST is being sent from somewhere other than Infusionsoft,
    // you may need to modify the name in $_POST[] to match form field name or key

    if ($_POST[‘Id’]) {
    $conID = $_POST[‘Id’];
    } else {
    $conID = ($_POST[‘contactId’]) ? $_POST[‘contactId’] : ”;
    }

    $fname = ($_POST[‘FirstName’]) ? $_POST[‘FirstName’] : ”;

    // Make the value all lower case and then capitalize first letter
    $capname = ucwords(strtolower($fname));

    // Write the result back to First Name field in Infusionsoft
    $conDat = array(‘FirstName’ => $capname);

    $conID = $app->updateCon($conID, $conDat);

    } else {
    echo "Not Connected…";
    }

    [/php]

    3) Now that you’ve got the code in your text editor, it’s time to edit it to suit your needs/wants/whimsies. Most of the script should work just fine as-is, but it does need a few customizations here and there. First, let’s look at these lines:
    [php firstline=”24″]if ($_POST[‘Id’]) {
    $conID = $_POST[‘Id’];
    } else {
    $conID = ($_POST[‘contactId’]) ? $_POST[‘contactId’] : ”;
    }[/php]
    These lines are using the Ternary Operator as a shorthand for a traditional nested If/Else Statement to get the Contact Id, if it exists, from the $_POST variable. Because we aren’t sure where the $_POST variable is coming from, we need to allow for all cases. If it’s coming from an Infusionsoft Action Set, “Id” is going to get assigned to $conID. If it’s coming from the Campaign Builder, “contactId” is going to get assigned to $conID. And if it’s coming from an external source like a Gravity Form on your website, there may be no Contact Id at all, in which case we need to tell $conID this field should be empty.

    The only reason you may need to change this code is if you decide to change the default (“contactId”) in Campaign Builder to something else. In that case, you’ll need to replace “contactId” with your new variable name. Otherwise, you’re good to go.

    4) You probably will need to change line 30 if you’re using an external form or if you change the Infusionsoft default (“FirstName”) in Campaign Builder. Remember, variables are case sensitive, so “firstname” is NOT the same as “FirstName”. Yours may be “First”, “Name” or any variation. Make sure your variable name matches here.

    [php firstline=”30″]$fname = ($_POST[‘FirstName’]) ? $_POST[‘FirstName’] : ”;[/php]

    In line 38 there’s another instance of the $_POST variable that you may need to change.

    [php firstline=”38″]$conDat = array(‘FirstName’ => $capname);[/php]

    Other than these changes, the rest of the code should work right out of the box. Remember, your clients are probably just as sloppy getting their last names in their contact records too, so have fun getting those looking good with a little alteration to this code!

  • How To Apply/Remove a Tag

    1) Open your favorite text editing program

    2)  Copy and paste the following PHP code into the blank text editor file:

    NOTE: Lines beginning with “//” denote comments explaining what the subsequent chunk of code does. They also sometimes point out things you can change or customize in the code. Reading through them will help you conquer the code and its nuances and let you modify the code to suit your needs.

    [php]

    <?php

    // Connect to Infusionsoft

    require_once("isdk.php");
    $app = new iSDK;

    if ($app->cfgCon("sandbox")) { // change this to match your connection name in conn.cfg

    // Current date in Infusionsoft-friendly format

    $currentDate = date_format(date_create(), ‘YmdTH:i:s’);
    echo "You connected at $currentDate <br/><br/>";

    // Recipe begins here

    $contactId = 104; // This is the contact you want to add the tag to – modify as needed
    $srvcGood = 106; // This is the tag ID, which you find in Infusionsoft CR< Settings -> Tags
    $srvcBad = 108;

    // Remove a tag from Contact using grpRemove
    // NOTE: You can only add or remove one tag at a time

    $result1 = $app->grpRemove($contactId, $srvcGood);
    $result2 = $app->grpRemove($contactId, $srvcBad);

    echo "<pre>";
    var_dump($result1);
    echo "</pre>";

    // Add a tag to Contact using grpAssign

    $result = $app->grpAssign($contactId, $srvcGood); // This is the API call that adds the tag

    echo "<pre>";
    var_dump($result);
    echo "</pre>";

    // Example use case – you will be applying a tag at the end of a script to kick off
    // a campaign in campaign builder. Because that tag may already exist on that contact ID,
    // it is a good idea to remove it at the beginning of your script and then reapply.

    } else {
    echo "Not Connected…";
    }

    [/php]

    3) Now that you’ve got the code in your text editor, it’s time to season to taste. Most of the script should work just fine as-is, but it does need a few customizations here and there. First, let’s look at this line:

    [php firstline=”17″] $contactId = 104; [/php]

    This line of code is what we call “hard coded” with the Contact Id.  In other words, the Contact Id of 104 is specifically programmed into the program.  Please understand that Contact Id 104 belongs to a contact in MY Infusionsoft application.  You will need to find a specific contact in YOUR application, make note of that Id, and replace my 104 with your particular contact Id number.  The Contact Id number can be found by going to Infusionsoft/CRM/Contacts.  You’ll see the Id right under the contact’s name.

    The same “hard coded” concept holds true for the next 2 lines.  You will need to find two tags in YOUR application to replace my 106 and 108.  You can find your Tag Id’s by going to Infusionsoft/CRM/Settings/Tags.

    [php firstline=”18″] $srvcGood = 106; [/php]

    [php firstline=”19″] $srvcBad = 108; [/php]

    You may also want to change the variables “$srvcGood” and “$srvcBad” to something that makes more sense to you for your chosen tags. Just be sure to keep the $ before each, and to make your changes both when we first declare the variables here:

    [php firstline=”17″] $srvcGood = 106; [/php]
    [php firstline=”18″] $srvcBad = 108; [/php]

    as well as when we call our grpRemove and grpAssign methods on these lines:

    [php firstline=”24″] $result1 = $app->grpRemove($contactId, $srvcGood); [/php]
    [php firstline=”25″] $result2 = $app->grpRemove($contactId, $srvcBad); [/php]
    [php firstline=”33″] $result = $app->grpAssign($contactId, $srvcGood);[/php]

    That’s about it. You can now confidently apply and remove tags from your contacts right from the API.  

  • How To Add A New Contact

    This is the HTML for a sample form you can use for testing.

    [html]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>Web Form Test</title>
    </head>
    <body>

    <!– this tells the browser where and how to send the data from the form –>
    <form action=’cb_add_contact.php’ method=’post’ enctype="multipart/form-data">

    <!– the script puts this data into Infusionsoft –>
    First Name : <input type=’text’ name=’FirstName’><br />
    Last Name : <input type=’text’ name=’LastName’><br />
    Email : <input type=’text’ name=’Email’ ><br />

    <input type=’submit’ value=’Test’>
    </form>

    </body>
    </html>

    [/html]

    This is the PHP for the script being called by the test form.

    [php]
    <?php

    echo "Hello World! <br/>";

    // Connect to Infusionsoft

    require_once("isdk.php");
    $app = new iSDK;

    if ($app->cfgCon("connectionName")) {

    // Current date in Infusionsoft-friendly format

    $currentDate = date_format(date_create(), ‘YmdTH:i:s’);
    echo "You connected at $currentDate <br/>";

    // Recipe begins here

    /*
    These are test values – make sure they are
    deleted or commented out when
    testing is complete
    */

    //$fname = ‘John’;
    //$lname = ‘Kennedy’;
    //$email = ‘jfk@whitehouse.gov’;

    // Check to see if the request method is POST or GET

    if ($_SERVER[REQUEST_METHOD] == ‘POST’) {

    if ($_POST[‘FirstName’]) {
    $fname = $_POST[‘FirstName’]; // These must match the name of the field in your form
    } else {
    $fname = ”;
    }

    if ($_POST[‘LastName’]) {
    $lname = $_POST[‘LastName’]; // including proper capitalization
    } else {
    $lname = ”;
    }

    if ($_POST[‘Email’]) {
    $email = $_POST[‘Email’];
    } else {
    $email = ”;
    }

    } else { // We are going to assume it is a GET coming from an Action

    // This is a short hand for IF ELSE called a ternary operator

    $_GET[‘FirstName’] ? $fname = $_GET[‘FirstName’] : $fname = ”;
    $_GET[‘LastName’] ? $lname = $_GET[‘LastName’] : $lname = ”;
    $_GET[‘Email’] ? $email = $_GET[‘Email’] : $email = ”;

    // This does the same thing as the code in the IF
    // but notice how much cleaner and easier to read it is

    }

    // Update contact record using AddWithDupCheck method
    $data = array(‘FirstName’ => $fname, // The key on the left must exact match table field names
    ‘LastName’ => $lname, // The value on the right can be anything as long as type matches field
    ‘Email’ => $email); // e.g you cannot put a string in date field
    $update = $app->addWithDupCheck($data, ‘Email’); // This is the API call which checks for duplicates using Email and Name

    echo $update; // If successful, this will return the contactId of the contact added or updated

    } else {
    echo "Not Connected…";
    }

    ?>

    [/php]