Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.

Moderator: amosbatto

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 AbbesMA
#794971
Hi

I try to call a trigger from Javascript, I have this script in Javascript Dynaform :
Code: Select all
$.ajax({
        url: host+"/api/1.0/"+ws+"/cases/"+app_uid+"/execute-trigger/"+trig_uid+"",        // endpoint URL
        data: {},                         // a GET request does not send parameters. But in case of other type of requests, data could be sent here
        type: "PUT",
        beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer '+token);},       // Header with the access token
        
     	success: function(xhr, status, error){
        	alert("SUCCESS!!"+xhr.responseText);
        },
       	error: function(xhr, status, error){
        	alert("ERROR!!"+error);
      	}
    });
and in trigger i have this script :
Code: Select all
@@textPanel="Coooooooooooooooool";
But i have this message ==> SyntaxError: Unexpected end of JSON input
#794975
Hi,

You can use a better way for call ajax requests.
I do this for my project

1. javascript code in my form is like this :
Code: Select all
function do_test(){
	var codeTest = $('#text_codeTest').getValue();
	
	$.ajax({
      type : 'POST',
      url : window.location + '&AJAX=1&REQ_TYPE=do_test',
      data : {
		codeTest : codeTest
      }
    }).done(function (msg) {
      if (msg.error) {
        alert(msg.error);
      } else {
		alert(msg.data);
      }
    });
}
2. I create a trigger for example with "00 - ajax" name and set it to before my form.
My php code is like this:
Code: Select all
global $db, $output;
$output = array();

function do_test(){
	global $output;
	
	$output['data'] = "Coooooooooooooooool";
}

// AJAX Data Loading
if($_GET['AJAX'] == 1){
	ob_end_clean();
	
	switch ($_GET['REQ_TYPE']) {
		case "do_test":
			do_test();
			break;
		default:
			$output['error'] = 'Your request is invalid';
	}
	
	header('Content-type: application/json');
	echo json_encode($output);
	die();
}
With this way I insert my php ajax codes in triggers in my process and call them without webservice call.

Thanks
#795003
AbbesMA wrote:Hi

I try to call a trigger from Javascript, I have this script in Javascript Dynaform :
Code: Select all
$.ajax({
        url: host+"/api/1.0/"+ws+"/cases/"+app_uid+"/execute-trigger/"+trig_uid+"",        // endpoint URL
        data: {},                         // a GET request does not send parameters. But in case of other type of requests, data could be sent here
        type: "PUT",
        beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer '+token);},       // Header with the access token
        
     	success: function(xhr, status, error){
        	alert("SUCCESS!!"+xhr.responseText);
        },
       	error: function(xhr, status, error){
        	alert("ERROR!!"+error);
      	}
    });
and in trigger i have this script :
Code: Select all
@@textPanel="Coooooooooooooooool";
But i have this message ==> SyntaxError: Unexpected end of JSON input
I don't see anything wrong with the JS code that you provided, but you might not be setting the variables correctly. Can you post your complete JS code?
#795009
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
var app_uid = $("#APP_UID_FORM").getValue();
var trig_uid = "16574096659a30e6b02f585012430028";



// hide liste fichier vide
$("#gridVar001").hide();


// Verifier le N°  de dossier
$("#button0000000002").find("button").click(function(){
  
});

//$('a.media').media({width:400, height:400});

$('a.media').media({width:500, height:500});


// Charger les fichiers
$("#button0000000001").find("button").click(function(){
  var nbreFiles, i;
  nbreFiles = $("#multipleFileVar001").getInfo().fileCollection.length;
 
  
  $("#multipleFileVar001").find("div.pmdynaform-multiplefile-box").hide();
  $("#gridVar001").show();
  
  
  $('a.media').attr('href','http://127.0.0.1/pm_A016.tmp_tmp/3757_ADD_PERE.pdf');
  $('a.media').media({width:500, height:500});
  
  
  /*
	if (fileUrl != '') {
  		$("#pdfObject").prop('data', fileUrl);
  		$("#pdfFrame").prop('src', fileUrl);
  		//$("#pdfFallback").attr('href', fileUrl);
	}
  */
  
  

  
   $.ajax({
        url: host+"/api/1.0/"+ws+"/cases/"+app_uid+"/execute-trigger/"+trig_uid+"",        // endpoint URL
        data: {},                         // a GET request does not send parameters. But in case of other type of requests, data could be sent here
        type: "PUT",
        beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer '+token);},       // Header with the access token
        
     	success: function(xhr, status, error){
        	alert("SUCCESS!!"+xhr.responseText);
        },
       	error: function(xhr, status, error){
        	alert("ERROR!!"+error);
      	}
    });     
  
 //  PUT /cases/{app_uid}/execute-trigger/{tri_uid}
  
  	for (i = 0; i < nbreFiles; i++) {
    	$("#gridVar001").setValue($("#multipleFileVar001").getInfo().fileCollection.models[i].attributes.file.name, i+1, '1');
    	$("#gridVar001").setValue(identifierTypedocument($("#multipleFileVar001").getInfo().fileCollection.models[i].attributes.file.name), i+1, '2');
    	$("#gridVar001").addRow();
	}
  
 jQuery("#gridVar001").deleteRow(nbreFiles+1);
 $("#gridVar001").find("button.pmdynaform-grid-newitem").hide();
 $("#gridVar001").find("div.pmdynaform-grid-removerow-responsive.remove-row").hide();

  
});


// Function to get type document from the name of document
function identifierTypedocument(p1) {
  if(p1.match(/^.*DSH_PERE.*/)){
  	return 1;
  }else if(p1.match(/^.*DSH_MERE.*/)){
  	return 2;
  }else if(p1.match(/^.*DSH_TUTEUR.*/)){
  	return 3;
  }else if(p1.match(/^.*CNI_PERE.*/)){
  	return 4;
  }else if(p1.match(/^.*CNI_MERE.*/)){
  	return 5;
  }else if(p1.match(/^.*CNI_TUTEUR.*/)){
  	return 6;
  }else if(p1.match(/^.*ADD_PERE.*/)){
  	return 7;
  }else if(p1.match(/^.*ADD_MERE.*/)){
  	return 8;
  }else if(p1.match(/^.*LDF.*/)){
  	return 9;
  }else if(p1.match(/^.*ADS_PERE.*/)){
  	return 10;
  }else if(p1.match(/^.*ADS_MERE.*/)){
  	return 11;
  }else if(p1.match(/^.*ADS_TUTEUR.*/)){
  	return 12;
  }else if(p1.match(/^.*RGA_PERE.*/)){
  	return 13;
  }else if(p1.match(/^.*RGA_MERE.*/)){
  	return 14;
  }else if(p1.match(/^.*RGA_TUTEUR.*/)){
  	return 15;
  }else if(p1.match(/^.*ANA_PERE.*/)){
  	return 16;
  }else if(p1.match(/^.*ANA_MERE.*/)){
  	return 17;
  }else if(p1.match(/^.*ANA_TUTEUR.*/)){
  	return 18;
  }else if(p1.match(/^.*ADT.*/)){
  	return 19;
  }else{
  	return 20;
  }
}




#795012
Hi,

I create a javascript like your code in my form like 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
var trig_uid = "37726039459a5376e1ae830028862240";

$.ajax({
	url: host+"/api/1.0/"+ws+"/cases/"+app_uid+"/execute-trigger/"+trig_uid+"",        // endpoint URL
	data: {},                         // a GET request does not send parameters. But in case of other type of requests, data could be sent here
	type: "PUT",
	beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer '+token);},       // Header with the access token
   
	success: function(xhr, status, error){
	   alert("SUCCESS!!"+xhr.responseText);
	},
	  error: function(xhr, status, error){
	   alert("ERROR!!"+error);
	 }
}); 
1. You don't need define an input in your form for app_uid in this line : " var app_uid = $("#APP_UID_FORM").getValue(); ", because app_uid variable is defined when a dynaform run.

2. your php code in trigger must be like this, because webserice return json object :
Code: Select all
@@textPanel="Coooooooooooooooool";

$output = array(
	'responseText' => 1
);
header('Content-type: application/json');
echo json_encode($output);
die();
Thanks
#795059
I figured out the problem. This REST endpoint doesn't send back any response when it is successfully executed and jQuery's .ajax() function takes that empty response and tries to call JSON.parse() on an empty string, which causes an error message like:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

What you need to do is set the dataType: "text", which is tells jQuery.ajax() that the endpoint will return text, so it shouldn't call JSON.parse(). If the endpoint is successful, then do nothing. If it is unsuccessful and it has returned a responseText, then call JSON.parse() to get the error object. Here is the code that you should use:
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
var app_uid = frames.app_uid ? frames.app_uid : ''; // get the case ID
var trig_uid = "61048893559a5d178ac62a10846078482"; // set to the ID of the trigger

$("#callRestButton").find("button").click(function(){ 
  if (app_uid) { 
    $.ajax({
        url: host+"/api/1.0/"+ws+"/cases/"+app_uid+"/execute-trigger/"+trig_uid, // REST endpoint
        //uncomment if needing to send POST parameters:
        //data: JSON.stringify( {param1: "value1", param2: "value2"} ),                         
        type: "PUT",
        contentType: "application/json",
        dataType: 'text',
        beforeSend: function(xhr) {
          xhr.setRequestHeader('Authorization', 'Bearer '+token); // Header with access token
        }, 
        success: function(xhr, status, error) {
          alert("Success" + (xhr.responseText ? ": "+xhr.responseText: ''));
        },
        error: function(xhr, status, error) {
          if (xhr.responseText) {
            oResponse = JSON.parse(xhr.responseText);
            alert(oResponse.error.message)
          } else {
            alert("Error: " + error);
          }
        }
    });     
  } 
});
#795260
Hi,

I used this method :
Code: Select all
var host = PMDynaform.getHostName();             
var ws = PMDynaform.getWorkspaceName();         
var token = PMDynaform.getAccessToken();        
var filesFolder = $("#folder").getValue();
var trig_uid = "48669433559a7d5d446d787071140233";


$("#selectFile").setOnchange(function(){
  
  alert("id selected "+$("#selectFile").getValue());
  
   	$.ajax({
   		url: host+"/api/1.0/"+ws+"/cases/"+app_uid+"/execute-trigger/"+trig_uid+"",       
   		data: {},                         
   		type: "PUT",
   		beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer '+token);},       
   
   		success: function(xhr, status, error){
     		alert("resulat !!!!!"+xhr.responseText);

     		$("#pdfObject").prop('data', host+"/"+filesFolder+"/"+xhr.responseText);
	 		$("#pdfFrame").prop('src', host+"/"+filesFolder+"/"+xhr.responseText);
   		},
     	error: function(xhr, status, error){
      		alert("ERROR!!"+error);
    	}
	}); 
  
});
Code: Select all
$N_Dossier = @@NDOSSIER;
$idSelectFile = @@selectFile;
$nameFile;


$query = "select ID_DOSSIER, N_DOSSIER, NOMDUFICHIER, TYPEFICHIER, LIBELLETYPEFICHIER, REPERTOIRE
from bitnami_pm.pmt_focp_dossier_initial
WHERE N_DOSSIER='$N_Dossier'";

$result = executeQuery($query);

//check if document found
if (is_array($result) and count($result) > 0) {
	
	for($i = 1; $i <= count($result); $i++){
		
		if((strcmp($result[$i]['TYPEFICHIER'], $idSelectFile) > 0)){
			$nameFile=$result[$i]['NOMDUFICHIER'];
		}
	}	
}


$output = array(
   'responseText' => $nameFile
);
header('Content-type: application/json');
echo json_encode($output);
die();
i would like to send à variable to this trigger and to get it in code trigger.

Thanks.
#795277
AbbesMA wrote:i would like to send à variable to this trigger and to get it in code trigger.
You can use the PUT cases/{app_uid}/variable endpoint like 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
var app_uid = frames.app_uid ? frames.app_uid : ''; // get the case ID
var trig_uid = "61048893559a5d178ac62a10846078482"; // set to the ID of the trigger
//variables to send to the trigger:
var oVars = {
  'NDOSSIER' : $("#NDOSSIER").getValue(),
  'selectFile' : $("#selectFile").getValue()
}

var oExecTrigger = {
        url: host+"/api/1.0/"+ws+"/cases/"+app_uid+"/execute-trigger/"+trig_uid,    // REST endpoint
        //data: JSON.stringify( {} ),    //uncomment if needing to send POST parameters                     
        type: "PUT",
        contentType: "application/json",
        dataType: 'text',
        beforeSend: function(xhr) {
          // Header with the access token
          xhr.setRequestHeader('Authorization', 'Bearer '+token);
        }, 
        success: function(xhr, status, error){
          alert("Success" + (xhr.responseText ? ": "+xhr.responseText: ''));
        },
        error: function(xhr, status, error){
          if (xhr.responseText) {
            oResponse = JSON.parse(xhr.responseText);
            alert(oResponse.error.message)
          } else {
            alert("Error: " + error);
          }
        }
};

var oSendVars = {
        url: host+"/api/1.0/"+ws+"/cases/"+app_uid+"/variable",        // endpoint URL
        data: JSON.stringify( oVars ),                      
        type: "PUT",
        contentType: "application/json",
        dataType: 'text',
        beforeSend: function(xhr) {
          xhr.setRequestHeader('Authorization', 'Bearer '+token); //set access token
        },      
        success: function() { 
          $.ajax(oExecTrigger); 
        },
        error: function(xhr, status, error) {
          if (xhr.responseText) {
            oResponse = JSON.parse(xhr.responseText);
            alert(oResponse.error.message)
          } else {
            alert("Error: " + error);
          }
        }
};    

$("#callRestButton").find("button").click(function() {
  if (app_uid) {
    $.ajax(oSendVars);     
  }
}); 
#823653
Hi,
I use this code for call ajax in my dynaform.
It's true and return success with web edition but in mobile app it return error without any message.
Code: Select all
$.ajax({
	url: host+"/api/1.0/"+ws+"/roles",
	data: {},
	type: "GET",
	beforeSend: function(xhr) {
	  xhr.setRequestHeader('Authorization', 'Bearer '+token);
	},
	success: function(msg) {
	  alert("Success" + msg.length);
	},
	error: function(xhr, status, error) {
	  if (xhr.responseText) {
		oResponse = JSON.parse(xhr.responseText);
		alert(oResponse.error.message)
	  } else {
		alert("Error: " + error);
	  }
	}
}); 
What is error in this code for mobile?! (it's true in web and my variables host and ws are true)
Thanks
#823695
Try this code:
Code: Select all
	error: function(xhr, status, error) {
	  if (xhr.responseText) {
		oResponse = JSON.parse(xhr.responseText);
		alert(oResponse.error.message)
	  } else {
		alert("Status: "+status+"\nXhr: "+JSON.stringify(xhr)+"\n\nError: " + JSON.stringify(error));
	  }
	}
#823803
Hi,
This is my code:
Code: Select all
var host = PMDynaform.getHostName();
var ws = PMDynaform.getWorkspaceName();
var token = PMDynaform.getAccessToken();

$.ajax({
	url: host+"/api/1.0/"+ws+"/roles",
	data: {},
	type: "GET",
	beforeSend: function(xhr) {
	  xhr.setRequestHeader('Authorization', 'Bearer '+token);
	},
	success: function(msg) {
	  alert("Success" + msg.length);
	},
	error: function(xhr, status, error) {
	  if (xhr.responseText) {
		oResponse = JSON.parse(xhr.responseText);
		alert(oResponse.error.message)
	  } else {
		alert("Status: "+status+"\nXhr: "+JSON.stringify(xhr)+"\n\nError: " + JSON.stringify(error));
	  }
	}
});
This code is true in web edition but I get that error in mobile app.
I just use directadmin for my centos server.
Thanks

Hi, You get this error: Native Error: Table 'wf_w[…]

Hi, You can set panel content with php for example[…]

Hi, You must set max_input_vars config to 8000 h[…]

end process using trigger

Hi! In your trigger you should use a condition wi[…]