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 4 followers
1

7 comments

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
Avatar
Ben Clarkson 0 votes

I am experiencing a similar issue as Zulfahami, except I created a button to duplicate the last row of a table. I attempted to add the "UPDATE" code suggested as solution #2 above, but it does not appear to work. When I attempt to log the value of the lookup field in the console, I see it is "undefined".

The Field Code of my lookup field is "item_number". Do I need to first access the table before accessing the lookup field since there are multiple lookup fields within this app?

I apologize if the solution is simple. I am still fairly new to JS. Here is my code, in case that helps:

(function() {
'use strict';

let TABLE = 'garments';
let SPACE = 'buttonspace';
let ITEM_NUMBER = 'item_number';
let BRAND = 'brand';
let STYLE = 'style';
let COLOR = 'color';
let QTY = 'qty';
let SIZE = 'size';
let BLANK_PRICE = 'unit_price';
let PRINTED_PRICE = 'printed_price';
let EXTENDED_PRICE = 'extended_price';
let NOTES = 'notes';

let events = ['app.record.create.show', 'app.record.edit.show'];

kintone.events.on(events, function(event) {
let record = event.record;
let elSpace = kintone.app.record.getSpaceElement(SPACE);
let elButton = document.createElement('button');
elButton.textContent = 'Duplicate Last Line Item';
elSpace.appendChild(elButton);

elSpace.addEventListener('click', function() {
let recordData = kintone.app.record.get();
let rec = recordData.record;

let addData = {};
addData.value = {};
addData.value[ITEM_NUMBER] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[ITEM_NUMBER].value
};
addData.value[BRAND] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[BRAND].value
};
addData.value[STYLE] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[STYLE].value
};
addData.value[COLOR] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[COLOR].value
};
addData.value[QTY] = {
"type": "NUMBER",
"value": 0
};
addData.value[SIZE] = {
"type": "DROP_DOWN",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[SIZE].value
};
addData.value[BLANK_PRICE] = {
"type": "NUMBER",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[BLANK_PRICE].value
};
addData.value[PRINTED_PRICE] = {
"type": "CALC",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[PRINTED_PRICE].value
};
addData.value[EXTENDED_PRICE] = {
"type": "CALC",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[EXTENDED_PRICE].value
};
addData.value[NOTES] = {
"type": "MULTI_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[NOTES].value
};
rec[TABLE].value.push(addData);

kintone.app.record.set(recordData);

record['item_number']['lookup'] = 'UPDATE';
console.log(record['item_number']['lookup']);
});


return event;
});
}());
0
Comment actions Permalink
Avatar
Sean Tachibana 1 vote

Hello Ben,

 

To automatically renew the field's value of "lookup" using the kintone.app.record.set() function,

you must execute the kintone.app.record.set() function after you have set "UPDATE" in lookup.

 

I checked your code and found out that it was backward.

 

The reason why the "undefined" is showing up is that the following process is not in the format that references the data within tables.

 

record['item_number']['lookup'] = 'UPDATE';

 

If you wish to do the requested task, after you delete the process below,

 

-----
record['item_number']['lookup'] = 'UPDATE';
console.log(record['item_number']['lookup']);
-----

 

Set the "lookup" to the 「item_number」 field upon creating the table process
and then change the process to execute the kintone.app.record.set() function.
This will hopefully resolve the problem.

 

-----
addData.value[ITEM_NUMBER] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[ITEM_NUMBER].value,
"lookup":'UPDATE'
};
-----

 

*Including the row that has "lookup," I added a comma at the end of the "value" row.
 
Hope this helps.
 
Sean
1
Comment actions Permalink
Avatar
Ben Clarkson 0 votes

Hi Sean,

Thank you so much for your reply. Your suggestion certainly fixed the "undefined" issue, however, I am still experiencing the issue shown in the screenshot. After I adjusted my code to match yours, I still see the "Click 'Lookup' to get the latest data" error when I attempt to save the record. Is there any way to fix this? Here is my updated code:

(function() {
'use strict';

let TABLE = 'garments';
let SPACE = 'buttonspace';
let ITEM_NUMBER = 'item_number';
let BRAND = 'brand';
let STYLE = 'style';
let COLOR = 'color';
let QTY = 'qty';
let SIZE = 'size';
let BLANK_PRICE = 'unit_price';
let PRINTED_PRICE = 'printed_price';
let EXTENDED_PRICE = 'extended_price';
let NOTES = 'notes';

let events = ['app.record.create.show', 'app.record.edit.show'];

kintone.events.on(events, function(event) {
let record = event.record;
let elSpace = kintone.app.record.getSpaceElement(SPACE);
let elButton = document.createElement('button');
elButton.textContent = 'Duplicate Last Line Item';
elSpace.appendChild(elButton);

elSpace.addEventListener('click', function() {
let recordData = kintone.app.record.get();
let rec = recordData.record;
let addData = {};

addData.value = {};
addData.value[ITEM_NUMBER] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[ITEM_NUMBER].value,
"lookup": 'UPDATE' // I also tried setting this value to true and still received the same error
};
addData.value[BRAND] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[BRAND].value
};
addData.value[STYLE] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[STYLE].value
};
addData.value[COLOR] = {
"type": "SINGLE_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[COLOR].value
};
addData.value[QTY] = {
"type": "NUMBER",
"value": 0
};
addData.value[SIZE] = {
"type": "DROP_DOWN",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[SIZE].value
};
addData.value[BLANK_PRICE] = {
"type": "NUMBER",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[BLANK_PRICE].value
};
addData.value[PRINTED_PRICE] = {
"type": "CALC",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[PRINTED_PRICE].value
};
addData.value[EXTENDED_PRICE] = {
"type": "CALC",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[EXTENDED_PRICE].value
};
addData.value[NOTES] = {
"type": "MULTI_LINE_TEXT",
"value": rec[TABLE].value[rec[TABLE].value.length-1].value[NOTES].value
};
rec[TABLE].value.push(addData);

kintone.app.record.set(recordData);
});

return event;
});
}());
0
Comment actions Permalink
Avatar
Sean Tachibana 0 votes

Hello Ben, 

I checked the code and didn't seem to have any problems.
Also, I have used the code and check to see if it replicates, but, it doesn't seem to do so.
Could you provide me with these following information?
・When you open the record details screen, and, start the developer tool by F12, the source code of the tab "Source"'s contents are the correct ones?
・When you click on the button you have created and on the field of "lookup" on the added row, do you see the message in green saying the (Date lookup ~) you have obtained?
・A detailed procedure until the problem occurs.
・Is the domain ".cybozu.com or ".kintone.com" ?

Thanks,

Sean

 

0
Comment actions Permalink
Avatar
Ben Clarkson 1 vote

Hello Sean,

  • Good question. I just checked and the appropriate code is showing in the Sources tab of the Chrome Developer Tools:

  • When I complete the first lookup action, I see the green message. However, what I am looking to do is to automate that task when clicking the "Duplicate Last Line Item" button so that I don't have to individually select the lookup criteria for each row, as this could become quite tedious if adding 20+ rows to that table. I would like to be able to achieve that green message for each new row when the "Duplicate Last Line Item" button is clicked:

  • I complete the following steps prior to seeing the error:
  1. Click "+" to create a new record
  2. Fill out the required fields
  3. Click "Lookup" on the first, empty row of the Garment Information table
  4. Adjust the Quantity and Size fields
  5. Click the "Duplicate Last Line Item" button to duplicate the row
  6. Adjust the Quantity and Size fields of this new row
  7. Click save (this is when the error occurs)

    Here is an animated *.gif of my screen as I follow the above steps:

  • The domain is ".kintone.com"

Thank you again for all your help,

Ben

1
Comment actions Permalink
Avatar
Sean Tachibana 1 vote
I checked the「03203.gif」
When you are trying to acquire “item,” it looks like the data of the original lookup reference duplicated.
When you are trying to acquire the value on the lookup field, and you are operating it from the screen, it could duplicate since the user could assign the record.
But in the case of API, you cannot tell which records value you should acquire, that might be why you are not able to acquire it.
 
Ex:
item | brand |
--------+--------+
5000 | A |
--------+--------+
5000 | B |
 
→ When operated from the screen, you could choose either A or B, but, in the case of API, there is only a piece of information about 5000, so you would not know which record is the acquisition object.
 
Also, on kintone, you could set it to “prohibit the duplication of the value,” so the duplicated value would not set on the field.
 
Could you try and apply the “prohibit the duplication of the value” setting on the reference field of the lookup field and see if that solves the issue?
 
Hopefully, this helps.
 
Sean
1
Comment actions Permalink