Auto-Fill with Custom API

Hi Guys, I would like your help. In my project I’m using a custom API:/Server Events → Global Code/
$API_ACTIONS[“BuscaCEP”] = function(Request $request, Response &$response) {
$numcep = Param(“NumCEP”, Route(1)); // Get the input value from $GET or $POST
if ($numcep !== NULL)
WriteJson(ExecuteRow(“SELECT CODPAI, CODEST, CODCID, CODBAI, DESLOG, TIPLOG FROM CADLOG WHERE NUMCEP = '” . AdjustSql($numcep) . “'”));
};/Clients Scripts → Startup Script/
$(“#x_NUMCEP”).change(function() {
var url = ew.API_URL, action = “BuscaCEP”, cep = encodeURIComponent($(this).val());
$.get(url + “?action=” + action + “&NumCEP=” + cep, function(res) { // Get response from custom API action
if (res)

	$("#x_CODPAI").val(res['CODPAI']);
	$("#x_CODEST").val(res['CODEST']);
	$("#x_CODCID").val(res['CODCID']);
	$("#x_CODBAI").val(res['CODBAI']);
	$("#x_DESLOG").val(res['DESLOG']);
});

});Api returns the data perfectly, however as in my formary the fields to be filled with the API return are Lookup fields with parent field, the lists are not generated from all fields.So the value is not shown, how could it solve this problem?Thank you for any help!

It depends on the Edit Tag settings of the target fields, you may post them for discussion.

My field it’s of lookup type, with parent field dependency
https://www.hkvstore.com/phpmaker/doc/dynamicselect.htm/Clients Scripts → Startup Script/
$(“#x_NUMCEP”).change(function() {
var url = ew.API_URL, action = “BuscaCEP”, cep = encodeURIComponent($(this).val());
$.get(url + “?action=” + action + “&NumCEP=” + cep, function(res) { // Get response from custom API action
if (res)$(“#x_CODPAI”).val(res[‘CODPAI’]);
$(“#x_CODPAI”).trigger(“onChange”);$(“#x_CODEST”).val(res[‘CODEST’]);
$(“#x_CODEST”).trigger(“onChange”);$(“#x_CODCID”).val(res[‘CODCID’]);
$(“#x_CODCID”).trigger(“onChange”);$(“#x_CODBAI”).val(res[‘CODBAI’]);
$(“#x_CODBAI”).trigger(“onChange”);$(“#x_DESLOG”).val(res[‘DESLOG’]);
});
});Using JavaScript trigger works?

If it is just a simple SELECT Edit Tag (e.g. not AutoSuggest, Modal Lookup, radio/checkbox, etc.), your code:$(“#x_CODPAI”).val(res[‘CODPAI’]);should work. Otherwise, you may try to use the PHPMaker functions, e.g.ew.selectOption($(“#x_CODPAI”)[0], [res[‘CODPAI’]]); // Assume SELECT Edit Tag and v2019

Didn’t work :-(I tried it this way but it also didn’t work.

		var selpai = $("#x_CODPAI");
		ew.updateOptions.call(selpai);
		selpai.val('1');
		selpai.change();
		
		var selest = $("#x_CODEST");
		ew.updateOptions.call(selest);
		selest.val('PA');
		selest.change();
		
		var selcid = $("#x_CODCID");
		ew.updateOptions.call(selcid);
		selcid.val('1');
		selcid.change();
		
		var selbai = $("#x_CODBAI");
		ew.updateOptions.call(selbai);
		selbai.val('1');
		selbai.change();

Change this:
selpai.val(‘1’);
selpai.change();to:
selpai.val(‘1’).change();and similar to the others, too.

don’t working! in field lookup, but in field text working perfectly! :frowning:

I am trying to do the same - fill detail from a reference table depending on the value on the master
to load data into detail I use in master client startup:$(“#x_IDno”).change(function() {
var url = ew.API_URL, action = “getAccounts”, id = encodeURIComponent($(this).val());
$.get(url + “?action=” + action + “&IDno=” + id, function(res) { // Get response from custom API action
if (res){
$(“#x_Amount”).val(res[“CurrentAmt”]).toNumber();
$(“#x_ClientEmail”).val(res[“ClientEmail”]);
}
});
});What is the correct way load into detail page and to convert a field into numeric?

smuyambo wrote:
$(“#x_Amount”).val(res[“CurrentAmt”]).toNumber();.toNumber() is a method of the .fields() plugin, not a jQuery method. jQuery’s .val() returns a String/Number/Array, not jQuery object, see: http://api.jquery.com/val/, so you cannot chain it. To convert to an integer, you simply use native JavaScript, e.g.var value = parseInt(res[“CurrentAmt”]); // or use parseFloat() if you want a float valueYou may also refer to the following topics in the help file for details:

  • Field Setup > Client side events (for Add/Copy/Edit/Search)
  • Server Events and Client Scripts > Table-Specific → Add/Copy page > Startup Script for details of the plugin

I have gone through the documentation but still not clear how to insert the data into the detail table. Do I use the client server events on the master or detail?The code i tried

$row = $(this).fields();
if (res){
$(this).fields(“PaymentRef”).value(val(res[“PropertyNo”])); //insert data into detail
}

Did you manage to get this working, if so how?
thanks