Avatar

Triggered Lookup function on submit

Zulfahami Aris

Hi guys, I been try to lookup the table data from another application. But seems like the lookup function didnt triggered as the picture below.

And I need to click the lookup one by one. So the question ss there any recommended way that I can triggered all the lookup function when I click the Submit function.

 

This is my Code

 

jQuery.noConflict();
(function($) {
'use strict';
const events2 = [
'app.record.create.change.Number_1',
'app.record.edit.change.Number_1',
"app.record.create.show",
"app.record.edit.show",
"app.record.create.submit"
];

kintone.events.on(events2, function(event) {
kintone.app.record.setFieldShown('Number_1', false);
const targetAppId2 = kintone.app.getLookupTargetAppId('Lookup_0');
const targetRecordId2 = event.record['Number_1'].value;
// Empty the table after clearing the lookup
if (!targetRecordId2) {
event.record['main_ingre'].value = [];
event.record['external_sub_ing'].value = [];
event.record['self_sub_ing'].value = [];
return event;
}

const body2 = {
app: targetAppId2,
id: targetRecordId2,
};

console.log(body2);
kintone.api(kintone.api.url('/k/v1/record', true), 'GET', body2, function(resp) {

event.record['main_ingre'].value = resp.record['main_ingre'].value;
event.record['external_sub_ing'].value = resp.record['external_sub_ing'].value;
event.record['self_sub_ing'].value = resp.record['self_sub_ing'].value;


// When making the subtable uneditable
for(var i = 0; i < event.record['production'].value.length ; i++){
if(event.record['Lookup_0'].value == event.record['production'].value[i].value.Text.value){
event.record['main_ingre'].value.forEach(function(obj) {
Object.keys(obj.value).forEach(function(params) {
obj.value[params].disabled = false;
obj.value['calculated_quantity'].value = obj.value['rm_uprice'].value*event.record['production'].value[i].value.quantity.value;
obj.value['calculated_quantity'].value = obj.value['calculated_quantity'].value.toFixed(2);
});
});
console.log("true");
}else{
console.log("false");
//event.record['production'].value[i].value.quantity.value = resp.record['main_ingre'].value[0].value.quantity2.value;
}

}

//return event;
kintone.app.record.set(event);


}, function(err) {
window.alert('An error occurred in the REST API');
});


});


})(jQuery);

Thanks Again

Follow 2 followers
1

1 comment

Avatar
Junko Werner 1 vote

Hello,

I have looked at your code and noticed three things below if you want to get the lookup field with API.

1. Before the event to save a record(such as "app.record.create.submit") runs,
automatic retrieval of lookup fields does not work well. So if you want to do this, please remove 'app.record.create.submit' and use other events like
'app.record.create.show' and 'app.record.edit.show', which you already included this time.

2. Please set the lookup property of the lookup field to "Update" or "True".
Then, the Lookup field runs when the event that allows the lookup field to be retrieved automatically is returned.

Run Lookup fields
https://developer.kintone.io/hc/en-us/articles/213149017-Record-Edit-Event

3.If the value you are retrieving in the lookup is duplicated,
it is not possible to determine which record value to retrieve,
so you need to set the field you are referring to be "Prohibit duplicate values".

You might want to look into the above three and I hope it solves the issue.

Thanks.

Junko

1
Comment actions Permalink