opendns / autotask-php

A PHP SOAP wrapper for the Autotask Web Service API
BSD 3-Clause "New" or "Revised" License
61 stars 33 forks source link

Is there any documentation about create and update requests #111

Closed mcl12345 closed 6 years ago

mcl12345 commented 6 years ago

Is there any documentation about create or update requests ?

I want to create and update the database through the API

Thanks in advance

mcl12345 commented 6 years ago

I tried this but it doesn't seem to work to create a contact

<?php

require_once("autotask-php/src/autoload.php");
$username = "******************";
$password = "******************";

$authWsdl = 'https://webservices.autotask.net/atservices/1.5/atws.wsdl';
$opts = array('trace' => 1);                                     
$client = new ATWS\Client($authWsdl, $opts);               
$zoneInfo = $client->getZoneInfo($username);                     

$authOpts = array(
    'login' => $username,
    'password' => $password,
    'trace' => 1,   // Allows us to debug by getting the XML requests sent
);
$wsdl = str_replace('.asmx', '.wsdl', $zoneInfo->getZoneInfoResult->URL);
$client = new ATWS\Client($wsdl, $authOpts);

$contact = new ATWS\AutotaskObjects\Contact();
$contact->id        = 0;
$contact->Active    = 1;
$contact->FirstName = 'Test';
$contact->LastName  = 'Contact';
$contact->Phone     = '123-123-1234';
$contact->AccountID = 29683560;

$createdContactResp = $client->create($contact);

$newContact = $createdContactResp->createResult->EntityResults->Entity;

foreach($newContact as $key => $value) {
    if ($value === '' || $value === null) {
        unset($newContact->$key);
    }
}

print($client->__getLastRequest());
?>

I have the print : 01Test29683560Contact123-123-1234

devinhyden commented 6 years ago

@mcl12345 overall your code looks good. Sounds like you are getting into Autotask's API. If you every have a problem creating / updating, my first look is to see if there are any NULL or empty values. Also, the UserDefinedFields can cause a real headache.

I was able to create a contact with your code working by unsetting the UserDefinedFields.

Below is the method I use for creating any new entity within Autotask. I also have a similar one for update.

public function create($entity)
    {
        foreach($entity as $key => $value)
        {
            if ($value === '' || $value === ' ' || $value === null) {
                unset($entity->$key);
            }
        }
        if (isset($entity->UserDefinedFields))
        {
            if (is_null($entity->UserDefinedFields->UserDefinedField))
            {
                 unset($entity->UserDefinedFields);
            }
        }
        return $this->client->create($entity);
    }

Another tip - $client->__getLastResponse() is a lifesaver. Autotask is really good about telling you why your request did not work.

reynolek commented 6 years ago

Yeah UDFs are horrible to work with, and often the cause of any headaches involving updates and creates.

mcl12345 commented 6 years ago

it works !

<?php
// This is an example script for creating a ticket in Autotask
// via the autotask-php client by OpenDNS

require_once __DIR__ . '/src/autoload.php';

// Edit these variables to get this example to work for you
$username = "***************";
$password = "**********";

$contact = new ATWS\AutotaskObjects\Contact();
// Set required fields
$contact->id        = 0;
$contact->Active    = 1;
$contact->FirstName = 'Test';
$contact->LastName  = 'Contact';
$contact->Phone     = '0185762128';
$contact->AccountID = 174;

$authWsdl = 'https://webservices.autotask.net/atservices/1.5/atws.wsdl';
$opts = array('trace' => 1);
$client = new ATWS\Client($authWsdl, $opts);
$zoneInfo = $client->getZoneInfo($username);

$authOpts = array(
    'login' => $username,
    'password' => $password,
    'trace' => 1,   // Allows us to debug by getting the XML requests sent
);
$wsdl = str_replace('.asmx', '.wsdl', $zoneInfo->getZoneInfoResult->URL);
$client = new ATWS\Client($wsdl, $authOpts);

print_r($client->create($contact));
?>