Data Service

The Data Service allows you to Add, Update, Query and Load Database records.

In the SDK you get access to the data service functions by including the file “DataService.php” in your code. Below is a list of the functions, their return values and the arguments they accept.

Function Description Return Value Arguments
dsAdd Adds a record to Infusionsofts database. Integer 2
dsDelete Deletes a record from Infusionsofts database. Boolean 2
dsUpdate Updates a record in Infusionsofts database. Integer 3
dsLoad Loads a record from Infusionsofts database. Array 3
dsFind Searches a table records field for specific data. Array 6
dsQuery Queries a table for specified field criteria. Array 5
authenticateUser Validates user login credentials. Integer 2
addCustomField Allows for the creation of new custom fields. Integer 4
updateCustomField Updates a custom field. Boolean 1
dsGetSetting Returns Application settings. String 2

dsAdd( [string] Table Name, [array] Field Data );

Top Menu

With dsAdd you can add a new record to a table in the Infusionsoft database. This function takes 2 parameters, the table name(you can find the names in api_field_access.xml) and the associative array with the field names and data that you wish to add to them. Like addCon dsAdd will return the ID of the record it creates. Below we have an example of adding a contact record with this function.

$conDat = array('FirstName' => 'John',
                'LastName'  => 'Doe',
                'Email'     => 'JDoe@email.com');

$conID = $myApp->dsAdd("Contact", $conDat);

dsDelete( [string] Table Name, [int] Record ID );

Top Menu

With dsDelete you can delete records from the Infusionsoft database if their access rights permit delete actions.

$status = $myApp->dsDelete("Contact", 123);

dsUpdate( [string] Table Name, [int] Record ID, [array] Field Data );

Top Menu

The dsUpdate function is used to update specified records in Infusionsoft. It accespts 3 arguments, Table Name, ID, and Field Data. If the update was successful it will return the ID of the updated record. Below we have an example of updating a group name.

$grp = array('GroupName'  => 'Test Group',
             'GroupDescription'  => 'This Group was Created with code');
$grpID = 97;

$grpID = $myApp->dsUpdate("ContactGroup", $grpID, $grp);

dsLoad( [string] Table Name, [int] Record ID, [array] Return Fields );

Top Menu

The dsLoad function is used to return the values of fields in a specific database record. To use this function you just pass a the table name, the records ID and an array of the field names you wish to retrieve. Below is an example of loading data from a contact.

$returnFields = array('Email', 'FirstName', 'LastName');
$conDat = $myApp->dsLoad("Contact", 123, $returnFields);

If the function runs without any errors the returned data will look like:

Array
(
    [Email] => JDoe@email.com
    [FirstName] => John
    [LastName] => Doe
)

dsFind( [string] Table Name, [int] Limit, [int] Page, [String] Search Field, [string] Search Data, [array] Return Fields );

Top Menu

The dsFind function is used to find records that have a specific value in a specific field. It will return results to you back in a nested array. Note that the maximum results returned back (limit) is 1000 and that paging is 0 based, meaning page 0 is the first page of [limit] results. Below we have an example of using dsFind to locate all contact records in a specific contact group.

$returnFields = array('ContactId','ContactGroup');
$contacts = $myApp->dsFind('ContactGroupAssign',5,0,'GroupId',97,$returnFields);
echo "<pre>";
print_r($contacts);
echo "</pre>";

The output of the above code with show the breakdown of the nested array returned by dsFind, the output looks like:

Array
(
    [0] => Array
        (
            [ContactGroup] => Test Leads
            [ContactId] => 1258
        )

    [1] => Array
        (
            [ContactGroup] => Test Leads
            [ContactId] => 1260
        )

    [2] => Array
        (
            [ContactGroup] => Test Leads
            [ContactId] => 1262
        )

    [3] => Array
        (
            [ContactGroup] => Test Leads
            [ContactId] => 1270
        )

)

dsQuery( [string] Table Name, [int] Limit, [int] Page, [array] Query Data, [array] Return Fields );

Top Menu

The dsQuery method allows you to query multiple fields in a table for matching criteria. This method returns results back in a nested array. Note that the maximum amount of results able to be returned by a single query is 1000, you specify this with the limit argument. Also paging is 0 based meaning that page 0 is actually the first page of results. When using dsQuery a % in your data array is treated as a wild card. Below is a sample of a query that pulls up all contacts who’se first name contains “est”.

$returnFields = array('Id','FirstName');
$query = array('FirstName' => '%est%');
$contacts = $myApp->dsQuery("Contact",10,0,$query,$returnFields);
echo "<pre>";
print_r($contacts);
echo "$lt;/pre>";

If the function runs without any errors the returned data will look like:

Array
(
    [0] => Array
        (
            [FirstName] => LSTest2
            [Id] => 244
        )

    [1] => Array
        (
            [FirstName] => LSTest3
            [Id] => 245
        )

    [2] => Array
        (
            [FirstName] => Tester
            [Id] => 1469
        )

    [3] => Array
        (
            [FirstName] => APITEST
            [Id] => 1473
        )

)

authenticateUser( [String] username, [String] password );

Top Menu

With the authenticateUser function you can pass a username and password into Infusionsoft and if it matches the credentials of a user it will return the users ID.

$uid = $app->authenticateUser("JDoe","Test123");

addCustomField( [String] context, [String] displayName, [String] dataType, [int] groupId );

Top Menu

With the addCustomField function you are able to create custom fields via the API.

Parameters:
context – (String) which context to use the custom field in (ie Contact, Opportunity)
displayName – (String) name that the custom field will display as
dataType – (String) type of data that will be stored in this custom field
groupId – which header that this custom field will be grouped under

$newField = $app->addCustomField('Contact','Test Field','Text',1);

updateCustomField( [Int] customFieldId, [Array] values );

Top Menu

The updateCustomField function will allow you to change properties of an existing custom field. The values will be an associative array to define fieldname/value. below is a sample of creating a custom text field and then renaming it with the update

$meta = $app->addCustomField('Contact','API TEST','Text',1);
$values = array('Label' => 'API_TEST_UPDATE');
$status = $app->updateCustomField($meta,$values);

The Available fields are as follows:

[Int] GroupId – What Header to place the field in

[String] Label – The fields display name

[String] DefaultValue – What data should start off in the field

[String] Values – Newline seperated string to populate list boxes and drop down lists

[Int] – ListRows – The amount of rows visible in a listbox

[Int] – UserGroup – The user group the field belongs to


dsGetSetting( [String] Module, [String] Setting );

Top Menu

This function allows you to get application settings out of Infusionsoft. They are usually returned to you in a comma delimited string

<br />
For example, we can pull out the available Contact Types and list them off with the following code.

$types = $app->dsGetSetting("Contact","optiontypes");
$types = explode(",",$types);
foreach($types as $type) {
  echo $type . <br />;
}

This concludes the Data Service functions. Happy Coding!

Leave a Reply