Error message for duplicate records

Sharath807
Tera Contributor

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();
}
}
}

2 REPLIES 2

Community Alums
Not applicable

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

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 :

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 selectedRecords = getSelectedRecordsFromTableA(); // Fetch selected records

        if (selectedRecords.length > 0) {
            var poNumberExists = false;
            var recordsInserted = false; // Flag to track if any records were inserted

            // Check if any record with the same asset tag exists in approved entitlements
            for (var i = 0; i < selectedRecords.length; i++) {
                var existingRecord = new GlideRecord('u_approved_entitlements');
                existingRecord.addQuery('u_asset_tag', selectedRecords[i].u_asset_tag);
                existingRecord.query();

                if (existingRecord.hasNext()) {
                    poNumberExists = true;
                    break;
                }
            }

            if (poNumberExists) {
                gs.addErrorMessage('A record with the same asset tag already exists in the approved entitlements table.');
            } else {
                // Update the status of the current record to 'approved'
                current.u_status = 'approved';
                current.update();

                // Insert records into approved entitlements table
                for (var y = 0; y < selectedRecords.length; y++) {
                    var gr = new GlideRecord('u_approved_entitlements');
                    gr.initialize();
                    gr.u_display_name = selectedRecords[y].u_display_name;
                    gr.u_product = selectedRecords[y].u_product;
                    gr.u_publisher = selectedRecords[y].u_publisher;
                    gr.u_asset_tag = selectedRecords[y].u_asset_tag;
                    gr.insert();

                    recordsInserted = true;

                    // Update the status of each selected record to 'approved'
                    var recordToUpdate = new GlideRecord('u_software_entitlement');
                    if (recordToUpdate.get(selectedRecords[y].sys_id)) {
                        recordToUpdate.u_status = 'approved';
                        recordToUpdate.update();
                    }
                }

                // Display success message if records were inserted
                if (recordsInserted) {
                    gs.addInfoMessage('Records are approved and moved to the approved entitlements table.');
                }

                // Set redirect URL after processing
                action.setRedirectURL(current);
            }
        } else {
            gs.addErrorMessage('No records were selected to be approved.');
        }
    }
}

// Function to fetch selected records from table A
function getSelectedRecordsFromTableA() {
    var selectedRecords = [];
    var gr = new GlideRecord('u_software_entitlement'); // Replace with your actual table name
    gr.addQuery('u_asset_tag', current.u_asset_tag); // Match asset tag
    // Add any other relevant conditions here
    gr.query();

    while (gr.next()) {
        selectedRecords.push({
            sys_id: gr.sys_id.toString(),
            u_display_name: gr.u_display_name.toString(),
            u_product: gr.u_product.toString(),
            u_publisher: gr.u_publisher.toString(),
            u_asset_tag: gr.u_asset_tag.toString()
        });
    }
    return selectedRecords;
}