Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By NeuroWinter
#794280
Hello all,

I have a rather odd question, I need to connect my ProcessMaker to an external API.

This external API has all the data that I will need in my forms, this includes Customer Information, Product information, etc.

We don't really want to connect PM to the other DB directly and would rather use the REST API. Is there an easy way of doing this?

Or should I create some scripts that import the data from the API to the internal PM SQL DB?

Regards,
Alex
By NeuroWinter
#794313
At the moment I am using a javascript fuction to get the actual data, now I am just struggling with getting that information into variables.

I want the user to be able to choose a customer from a dropdown box. This customer information is from the API.

To be clearer, I am having an issue creating a new set of options.
User avatar
By amosbatto
#794346
There are two ways to populate a dropdown box. One way is to create a case variable in a trigger which holds the list of options for the dropdown box. The "data source" property of the dropdown box needs to be set to "Array Variable" and its "data variable" property needs to be set to the name of the variable. See:
http://wiki.processmaker.com/3.0/DynaFo ... Datasource

The second way is to use the mergeOptions() JavaScript method.
If you are storing your data in an database, then use the executeQuery() and mergeOption() methods together. By the way, you can do the same with dependent fields.
User avatar
By amosbatto
#794349
By the way, if you want to store the results of a REST call in a ProcessMaker case variable, then create a hidden field in your DynaForm associated with a string variable.
Then, add JavaScript code to your Dynaform to do the REST call and save the contents of the REST call as a JSON string in the hidden field.

Using this example as a model, you can do this:
Code: Select all
var host = PMDynaform.getHostName();              // get the hostname
var ws = PMDynaform.getWorkspaceName();           // get the current workspace
var token = PMDynaform.getAccessToken();          // get the access Token

function getRoles() {
    $.ajax({
        url: host+"/api/1.0/"+ws+"/roles",        // endpoint URL
        // GET requests do not send parameters, but POST and PUT can set their data here:
        data: {},                        
        type: "GET",
        // Header with the access token:                            
        beforeSend: function(xhr) {
            xhr.setRequestHeader('Authorization', 'Bearer '+token);
        },      
        success: function(roles) {   
            var sRoles = JSON.stringify(roles);     
            $("#myhidden").setValue(sRoles);
        }
    });
}
$("#getRolesBtn").find('button').click(getRoles); 
Where "myhidden" is the ID of your hidden field and it is associated with the case variable @@myhidden.

Then, you can use this trigger code to access the options:

if (isset(@@myhidden)) {
@=aOptions = json_decode(@@myhidden, true); //true parameter converts object to an associative array
}
Then you can use @=aOptions as the "source variable" in dropdowns in subsequent dynaforms in your process.
By NeuroWinter
#794499
Thank you so much for that I have managed to hack together an odd solution to the problem:
Code: Select all
function getCustomers(argument) {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", "APIURL/api/v1/ENDPOINT/", false);
	xhr.send();
	var jsonResponse = JSON.parse(xhr.responseText);
	console.log(xhr.status);
	console.log(jsonResponse);
	return jsonResponse
}

function testCompanyInVar (companyName, companyVar) {
	for (var i = 0; i < companyVar.length; i++) {
		console.log(companyName == companyVar[i].label	)
		console.log(companyName)
		console.log(companyVar[i].label)
		if (companyName == companyVar[i].label) {
			return true
		};
	};
	return false
}

function attachCurrent (current, newArray) {
	for (var i = 0; i < current.length; i++) {
		newArray.push(current[i]);
	};
}
// get the current list of Customers from PM and DB.
// Check each company from DB add it to PM if its not there
// update PM options
function setupCustomers (argument) {
	var aOpts = getFieldById("drpCustomers").model.attributes.options;
	var aSelected = [];
	attachCurrent(aOpts,aSelected)
	var tempStruct = jQuery.extend(true, {}, aOpts[0])
	var customersjson = getCustomers()
	for (var i=0; i < customersjson.length; i++) {
		var companyName = customersjson[i].name
		if (testCompanyInVar(companyName, aOpts)) {
			console.log(companyName + " in List Already")
		}else{
			var newCustomer = jQuery.extend(true, {}, tempStruct)
			newCustomer.value = (parseInt(newCustomer.value) + 1) 
			newCustomer.label = customersjson[i].name
			console.log(newCustomer)
			aSelected.push(newCustomer)
			tempStruct = jQuery.extend(true, {}, newCustomer)
		};
	};
	getFieldById("drpCustomers").mergeOptions(aSelected);
}
I think that is working how I want it to ? :P

In the rapidly evolving world of online sports be[…]

STEPN integrates social networking and games that […]

Cenforce 150 is a medication used to cope with a c[…]

What's SAP FICO?

Trustworthy and skill-building, each of these actu[…]