How to attach documents to contacts via API in VtigerCRM

How to attach documents to contacts via API in VtigerCRM

The VtigerCRM system provides an excellent tool that allows you to perform any operations with modules - create, edit, view, delete. Imagine a situation - you have a web service where users attach scans of documents. And you want to make sure that these documents are sent via API to VtigerCRM and attached, for example, to a contact card.

The VtigerCRM system provides an excellent tool that allows you to perform any operations with modules - create, edit, view, delete.

Imagine a situation - you have a web service where users attach scans of documents. And you want to make sure that these documents are sent via API to VtigerCRM and attached, for example, to a contact card.

To do this manually, you need to go to the contact card, click on the documents tab and add a new document in it. You can see how it looks in the system in the screenshot above. But if you have your own service, where users have already uploaded documents, then you do not need to additionally transfer the entire file. It is enough to send a link to it on an external service. Therefore, it is usually more convenient to add a link to the file. Fortunately, Vtiger has similar functionality.

So, we know how to add a link to a file through the system interface. Now let's try to do it from the API.

To do this, you need to send a POST request to Vtiger /webserbive.php as a form with the following fields:

  • operation: create
  • sessionName: session key for auth
  • elementType: Documents
  • element: text in json format, for example

{

  "notes_title": "External Five PDF",

  "filename": "https:\/\/itvolga.com\/storage\/settings\/October2018\/ITy6ZA0VlyRWZsYOGNnZ.png",

  "assigned_user_id": "19x1",

  "notecontent": "",

  "filetype": "",

  "filesize": "0",

  "filelocationtype": "E",

  "fileversion": "",

  "filestatus": "1",

  "filedownloadcount": "",

  "folderid": "22x1",

  "modifiedby": "19x1",

  "created_user_id": "19x1",

  "starred": "0",

  "tags": "",

  "relations": "12x100"

}

 

In the example above, you see the relations field, in which we tell the system that the document should be bound to a contact with id 100.

If you send a similar request to Vtiger, then you will have successfully created a document with a link to the file. But if you go to the contact card, then the required document will not be there. Here we see a problem that when creating a document, the creation of a link does not work. Let's try to solve this problem.

To do this, firstly, create a new utility function in the include/Webservices/Utils.php file

 

function vtws_getEntityId($entityName) {

    global $adb;

    $wsrs=$adb->pquery('select id from vtiger_ws_entity where name=?', array($entityName));

    if ($wsrs && $adb->num_rows($wsrs)==1) {

        $wsid = $adb->query_result($wsrs, 0, 0);

    } else {

        $wsid = 0;

    }

    return $wsid;

}

 

Then go to include/Webservices/SetRelation.php, find the vtws_internal_setrelation function in it (line 57) and at the end of this function, before the line

 

relateEntities($focus, $moduleName, $elementId, $withModuleName, $withElementId);

 

Add following block of code:

 

if ($moduleName === 'Documents' && $withModuleName === 'Contacts') {

    $focus = CRMEntity::getInstance($withModuleName);

    $moduleName = $withModuleName;

    $tmpElementId = $elementId;

    $elementId = $withElementId;

    $withElementId = $tmpElementId;

    $withModuleName = 'Documents';

        }

 

After these operations, the next time you send a request to create an external document, it will not only be created in the "Documents" module, but it will also be successfully linked to the contact.

 

If you still have questions on how to improve the system, please contact me, I will be happy to help you.