Error message for duplicate records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-13-2024 09:41 AM
Hi all, I use UI action button 'Approve' to copy list of records from one table A to another Table B. No the challenge is I have a field PO number field in Table A . When I First time select the records may be 10 records with same PO number from table A and click Approve it should push to Table B.. But next time if the records with same PO number is created and click Approve it should not create record in Table B , instead it should throw error. How to achieve this..
Now if I click button only one record is inserting instead of 10 ( my requirement is first time those 10 records should insert , next time if same 10 records are used it should validate).
below is my ui action script:
if (current.u_status == 'approved') {
gs.addInfoMessage('Record is already approved.');
action.setRedirectURL();
} else {
var missingFields = [];
if (!current.u_product) {
missingFields.push('Product');
}
if (!current.u_publisher) {
missingFields.push('Publisher');
}
if (missingFields.length > 0) {
gs.addErrorMessage('The following mandatory fields are missing: ' + missingFields.join(', '));
} else {
var existingRecord = new GlideRecord('u_approved_entitlements');
existingRecord.addQuery('u_ponumber', current.u_ponumber);
existingRecord.query();
if (existingRecord.next()) {
gs.addErrorMessage('A record with the same ponumber already exists in the approved entitlements table.');
} else {
/
var gr = new GlideRecord('u_approved_entitlements');
gr.initialize();
gr.u_display_name = current.u_display_name;
gr.u_product = current.u_product;
gr.u_publisher = current.u_publisher;
gr.u_ponumber = current.u_ponumber;
gr.insert();
current.u_status = 'approved';
current.update();
gs.addInfoMessage('Record is approved and moved to the approved entitlements table.');
action.setRedirectURL();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-13-2024 02:48 PM
Hi @Sharath807 ,
Your script only inserts one record as the script is not proper also while inserting you are only validating the current record rather than all the records.
Please find the updated script-
if (current.u_status == 'approved') {
gs.addInfoMessage('Record is already approved.');
action.setRedirectURL(current);
} else {
var missingFields = [];
if (!current.u_product) {
missingFields.push('Product');
}
if (!current.u_publisher) {
missingFields.push('Publisher');
}
if (missingFields.length > 0) {
gs.addErrorMessage('The following mandatory fields are missing: ' + missingFields.join(', '));
} else {
var existingRecord = new GlideRecord('u_approved_entitlements');
existingRecord.addQuery('u_ponumber', current.u_ponumber);
existingRecord.query();
if (existingRecord.hasNext()) {
gs.addErrorMessage('A record with the same PO number already exists in the approved entitlements table.');
} else {
var selectedRecords = getSelectedRecordsFromTableA(); // Fetch selected records
for (var i = 0; i < selectedRecords.length; i++) {
var gr = new GlideRecord('u_approved_entitlements');
gr.initialize();
gr.u_display_name = selectedRecords[i].u_display_name;
gr.u_product = selectedRecords[i].u_product;
gr.u_publisher = selectedRecords[i].u_publisher;
gr.u_ponumber = selectedRecords[i].u_ponumber;
gr.insert();
}
current.u_status = 'approved';
current.update();
gs.addInfoMessage('Records are approved and moved to the approved entitlements table.');
action.setRedirectURL(current);
}
}
}
function getSelectedRecordsFromTableA() {
var selectedRecords = [];
return selectedRecords;
}
If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2024 08:50 AM
Hi @Community Alums I updated script as below , but when i select multiple new record and click ui button, Both info messages are displaying (Records are approved and moved to the approved entitlements table.,
Record is already approved.)
This is my updated script :