Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a module record from a workflow via webservice
#1
Okay, I took a look at the following files:

https://github.com/tsolucio/corebos/blob/master/modules/com_vtiger_workflow/tasks/VTCreateEventTask.inc

and

https://github.com/tsolucio/corebos/blob/master/build/HelperScripts/importcsv.php

My goal is to make my module (that basically relies on the use of workflows) create a record for each workflow execution to show the result. The result will be a server answer, since it is connecting to a foreign application. Since I know very little about the vTiger/coreBOS webservice, I thought I'd start a topic here that will maybe help others as well.

My starting point is a line in the file in the second link:

PHP Code:
$row vtws_create('Accounts'$row$current_user); 

This is from this line. I presume this is a function from 'include/Webservices/Create.php' that handles the actual creation of a record. I will need to take a look at that function to find out more.

I also studied the class definition in the include file that creates an event, I know the 'doTask' method is the one that is actually executed when the workflow condition is met. I now have very simple custom functions in a single file that handle the API calls to the foreign server, and I'd basically like to expand them with a method from an existing class (the 'vtws_create' command?) that creates a record with the return from the foreign server. I can only get the answer from the server from the same function as I use to send data, so whatever I use, it can't be a separate workflow.

The above may be a little confusing, since I studied a lot of files but don't really get the context yet. In the end, I just want to create a new row in the main table of my module, but do it in a system-accepted way, so all other related actions are also taken (like updating the crmentity table). So can I use the 'vtws_create' function? Will it take care of all other system-updates for me? Does it expect any special parameters? Do I need to include special files (other that the 'include/Webservices/Create.php')?
Reply
#2
Webservice is a layer of calls that permit you to interact with the application from outside. For example, you can access information from a spreadsheet or create a record from a webform.
It makes it really easy to manipulate the system without getting into the internals while creating a nice high level abstraction layer.

You can read about this in our book on the subject and I would recommend you download and use the coreBOS Webservice Developer tool to get a feel of everything you can do.

https://github.com/tsolucio/coreBOSwsDevelopment

The webservice libraries make it even easier to use: https://github.com/tsolucio/coreBOSwsLibrary

Since the webservice API is at a more abstract level than the internals, it is easier to use, even from within the application, so if you want to create a record using the webservice API you would call the CREATE method:

https://github.com/tsolucio/coreBOSwsDevelopment/blob/master/testcode/020lib_createContact.php

this ends up calling the vtws_create function in the application, so, if you are programming inside the application you can directly call the vtws_create function to achieve the same goal which is a little easier than setting up and calling the save() method.

It may also be a little faster.
Joe
TSolucio
Reply
#3
(12-15-2015, 03:25 PM)joebordes Wrote: Since the webservice API is at a more abstract level than the internals, it is easier to use, even from within the application, so if you want to create a record using the webservice API you would call the CREATE method:

Do you mean this method?:

PHP Code:
$cbconn->doCreate 
And if so, which class does it belong to? In the example file there is no class instantiation or file inclusion so I can't tell. Apart from that, it seems quite straightforward: first parameter is the module name, second is an array where 'key' is the column name of the main table of the module and 'value' is the value you want to insert.
Reply
#4
coreBOS Webservice Developer tool does a lot of abstraction so that you can concentrate on each method. If you load the library for the language you are working with, it will give you a pretty high level of abstraction.

The idea is exactly as you say: module and array of field values
Joe
TSolucio
Reply
#5
I'm sorry but I'm just not experienced with the system enough to grasp all these abstractions. If I understand correctly there are three different ways to create a record internally:

  1. Via the entity 'save' method. I just found out about this. It seems first I need to create a handler, or instatiation for this. An instantiation of the CRMEntity class? Or an instantiation of the module's class, since it extends the CRMEntity class by default? And is the 'entity' variable (or object) within a workflow automatically an instantiation of the moduleclass the workflow is designed for?
  2. Via the webservice Which would allow me to use vtws_create and just create a row with the column names as keys and the values as array values. The current user variable (or object?) is available by default within the workflow since I'm operating in the system I presume.
  3. Via the webservice part 2 Where I could use the 'doCreate' method, but I don't understand of which class this method is.

I will surely try the development tool since I want to learn more about this, but for now I just want to have a firm understanding of this basic functionality before I proceed with more things I don't understand yet.
Reply
#6
Let me try to clarify a little.

You actually have two ways of saving information in the application:

1.- from within the application: you are executing your code inside the same server and the same directory where coreBOS is installed and the whole application infrastructure is loaded. This is the case of an event handler, a workflow, a custom module or a direct vtlib script that has included vtlib/Module.php

2.- from outside the application using webservice. this can be executed on any server anywhere (even the same server coreBOS install is on) and all it needs from the coreBOS install is it's URL and a valid user to connect.

In the first case you can use the system's save() process which is explained here:

http://corebos.org/documentation/doku.php?id=en:devel:saverecordfromwithin

In the second case you can either call the webservice directly

https://github.com/tsolucio/coreBOSwsDevelopment/blob/master/testcode/020_createContact.php

or use one of the many libraries that are available that will make it a bit easier:

https://github.com/tsolucio/coreBOSwsDevelopment/blob/master/testcode/020lib_createContact.php

---------------------

Ok, so now we have only two ways of creating: inside save() and outside webservice

As you can see the internal save() process suppose a lot of things, the information is coming from the browser in a specific format, a user is connected, certain $_REQUEST variables must exist.... which make it a bit complicated. So, as an alternative we can benefit that the webservice interface takes care of most of these details and call the webservice functions directly.

The idea is that you are inside the application, so you have access to EVERYTHING that is there, that includes all the webservice code. You can call the webservice functions just as you would call any other function in the system. So when calling doCreate() from outside the application, that ends up (after validating the user and doing a bunch of other things) calling the function vtws_create(), since we are already inside the application environment (in your case) then you can call this function directly with no overhead of validating and with no complicated formatting issues because vtws_create() will take care of that. It is just an easier way of manipulating the information, in the end ALL will call the save() method.

keep asking :-)
Joe
TSolucio
Reply
#7
Thanks, it's good to know that all will eventually call the internal save() method. If I understand correctly what you're proposing is the use of a layer that is meant for communication from outside, but use it from the inside, because the layer itself simplifies things and automates certain tasks.

I found the attached document. On page 6 I see I should include the file vtwsclib/Vtiger/WSClient.php and instantiate the class Vtiger_WSClient. Then I can call the 'doCreate' method and save a record from workflow I think?


Attached Files
.pdf   vtwsclib-1.1.pdf (Size: 414.11 KB / Downloads: 1)
Reply
#8
You will need the WSClient library and to call the doCreate ONLY if you are working totally outside the application, in your case you should be able to directly call the vtws_create() function.
If you can't have a look at the workflow cron script, that one includes almost everything:

https://github.com/tsolucio/corebos/blob/master/cron/modules/com_vtiger_workflow/com_vtiger_workflow.service

Now that I look at it, it seems that it doesn't load the create script, but you can add that if you need it.
Joe
TSolucio
Reply
#9
Ah Okay. I'll go and create the workflow save function from the vtws_create function. I still need to find out why the module won't save but that is a different thread.

I'll post here any issues I run into, if any.
Reply
#10
Okay, now that I've got my module to save records from within the application, I know I should be able to create records from a workflow. So, here's what I think I should do:

Within my workflow functions, I include the file 'include/Webservices/Create.php'. Then I execute the following:

PHP Code:
vtws_create('ExactOnline'$array_of_data_to_save$current_user); 

Now for some questions. For the module name, I assume I should use the name from the 'vtiger_tabs' table. The array should be 'columnname in exactonline table' => 'value to save', excluding the record no, since that is a ui type 4 and should auto gen. the current user, will that be available, or should I first bring it in from the global scope? Or even do it like this:

PHP Code:
$current_user Users::getActiveAdminUser(); 

Like in this example?
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)