Auto numbering records and if any pre existing record found it must map same number

R Aishwarya
Tera Contributor

I have auto numbered a field called "App ID" in my Business Application table.

So when ever a new Business application is inserted into this table, it will start auto numbering and increments as specified.

But here my requirement is, 

App1 Test (Dev) 
App1 Test (Prod) 

Here it must allocate same App Id number to above 2 applications since Application Name(App1) is same. If the name is different then it must provide different app id.Help me to acheive this.

1 REPLY 1

Ratnakar7
Mega Sage
Mega Sage

Hi @R Aishwarya  ,

 

To achieve this requirement, you can implement a script that generates the "App ID" based on the name of the application. Here's a general approach you can follow:

  1. Query Existing Records: Before inserting a new record, query the Business Application table to check if any existing records have the same application name. If a match is found, retrieve the existing "App ID".

  2. Generate App ID: If no match is found, generate a new "App ID" based on the application name. You can use a numbering scheme such as incrementing numbers appended with a prefix.

  3. Insert New Record: Insert the new record into the Business Application table with the generated or retrieved "App ID".

Here's a sample JavaScript code that illustrates this approach:

// Function to generate a new App ID based on the application name
function generateNewAppId(appName) {
    // Query existing records to find the highest existing App ID for the given application name
    var existingRecords = new GlideRecord('business_application');
    existingRecords.addQuery('name', appName);
    existingRecords.orderByDesc('app_id');
    existingRecords.query();
    
    var newAppId;
    
    if (existingRecords.next()) {
        // If existing records are found, get the highest App ID and increment it
        var highestAppId = existingRecords.app_id;
        var numericPart = parseInt(highestAppId.match(/\d+$/)[0]); // Extract numeric part
        newAppId = highestAppId.replace(/\d+$/, numericPart + 1); // Increment the numeric part
    } else {
        // If no existing records are found, start numbering from 1
        newAppId = appName + '_1';
    }
    
    return newAppId;
}

// Assuming 'current' contains the data of the new record being inserted
var appName = current.name; // Get the name of the new application

// Generate a new App ID based on the application name
var newAppId = generateNewAppId(appName);
current.app_id = newAppId;

// Insert the new record into the Business Application table
current.insert();

 

Thanks,

Ratnakar