How To Set AllowFieldTruncationHeader – PHP Toolkit

Recently I had to develop a pretty custom plugin for WordPress that would feed data into Using their PHP Toolkit this was relatively straight forward, although one thing that was not obvious from the start was the AllowFieldTruncationHeader you can pass to, this helped massively in the plugin I built.

This is a quick post just to let you know what it does and how to implement it using the php toolkit.

The AllowFieldTruncationHeader tells to truncate any fields that are too long, the reason this is useful is that generally custom objects inside have been created by a third party and then the application feeding into that has been created by someone else too. When you come to link the two up they rarely work together so truncating fields was the quickest and easiest way to get the data (most of) into

Ok let’s take a look at some code to set the header using the example from the PHP Toolkit. First you require the SforceBaseClient.php or SforceEnterpriseClient.php

$mySforceConnection = new SforceEnterpriseClient();
$mySforceConnection->createConnection($dir . "enterprise.wsdl.xml");
if (isset($_SESSION['enterpriseSessionId'])) {
    $location = $_SESSION['enterpriseLocation'];
    $sessionId = $_SESSION['enterpriseSessionId'];

} else {
    $mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);

    $_SESSION['enterpriseLocation'] = $mySforceConnection->getLocation();
    $_SESSION['enterpriseSessionId'] = $mySforceConnection->getSessionId();
$header = new AllowFieldTruncationHeader(true);

The bit at the start is taken from the Getting Started with Toolkit for PHP. The bit we are interested in is where we set the $header variable. We create a new object and then pass it to the setAllowFieldTruncationHeader method. This allows us to continue with an upsert() or create() without worrying too much about text limits.

If you have the luxury of working with and the application before any data has been created then you can map the fields from your application to to make sure the limits match, which means you won’t come across this problem, if like me you aren’t that lucky then this helps to get you started.

Good luck with site and if you need any help ask below and I’ll try and help as much as I can.