- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2022 09:57 AM
I am looking for code examples of a server script that checks to see if a record exists in a table based on a specific field, if it does then it updates the record with new information and if it doesn't exist then it creates a new record in the table.
Would appreciate any code snippets or links.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2022 10:59 AM
Ok got it-- yeah so if the record already exists there is no need to initialize a new GlideRecord object, and then there appears to be an issue with your update() line as well. Here is how I would re-write your script to handle creating new or updating existing:
function createVendor(cmpny, cntry) {
var gr = new GlideRecord('u_company_table');
gr.addQuery('u_name', cmpny.name);
gr.query();
if (gr.next()) {
gr.setValue('u_name', cmpny.name);
gr.setValue('u_street', cmpny.street);
gr.setValue('u_city', cmpny.city);
gr.setValue('u_zip', cmpny.zip);
gr.setValue('u_country', cntry);
gr.update(); //removed unnecessary get()
} else {
//instantiate brand new GlideRecord object to insert
var newGr = new GlideRecord('u_company_table');
newGr.initialize();
newGr.setValue('u_name', cmpny.name);
newGr.setValue('u_street', cmpny.street);
newGr.setValue('u_city', cmpny.city);
newGr.setValue('u_zip', cmpny.zip);
newGr.setValue('u_country', cntry);
newGr.insert();
}
}
If this answer is helpful please mark correct and helpful!
Regards,
Christopher Perry
Regards,
Chris Perry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2022 10:00 AM
Could you please provide more specific details about the specific field and what the user is selecting?
Regards,
Chris Perry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2022 10:12 AM
Sorry, I actually meant server script, not client script.
I have a custom widget with an HTML form which contains company name, street, city, state, zip and id.
Users can either look for an existing company in which case these fields are populated from the table and let the user make changes.
Otherwise the user can add a new company.
Currently my server script only creates a new entry:
function createVendor(cmpny, cntry) {
var gr = new GlideRecord('u_company_table');
gr.addQuery('u_name', cmpny.name);
gr.query();
if (gr.next()) {
gr.initialize();
gr.setValue('u_name', cmpny.name);
gr.setValue('u_street', cmpny.street);
gr.setValue('u_city', cmpny.city);
gr.setValue('u_zip', cmpny.zip);
gr.setValue('u_country', cntry);
gr.get(gr.update());
}
}
I am wondering how to modify it so that if the company exists already then these fields simply update otherwise if the company does not exist then a new record should be created. I thought it was simply a case of removing gr.initialize() but that doesn't seem to be the case.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2022 10:59 AM
Ok got it-- yeah so if the record already exists there is no need to initialize a new GlideRecord object, and then there appears to be an issue with your update() line as well. Here is how I would re-write your script to handle creating new or updating existing:
function createVendor(cmpny, cntry) {
var gr = new GlideRecord('u_company_table');
gr.addQuery('u_name', cmpny.name);
gr.query();
if (gr.next()) {
gr.setValue('u_name', cmpny.name);
gr.setValue('u_street', cmpny.street);
gr.setValue('u_city', cmpny.city);
gr.setValue('u_zip', cmpny.zip);
gr.setValue('u_country', cntry);
gr.update(); //removed unnecessary get()
} else {
//instantiate brand new GlideRecord object to insert
var newGr = new GlideRecord('u_company_table');
newGr.initialize();
newGr.setValue('u_name', cmpny.name);
newGr.setValue('u_street', cmpny.street);
newGr.setValue('u_city', cmpny.city);
newGr.setValue('u_zip', cmpny.zip);
newGr.setValue('u_country', cntry);
newGr.insert();
}
}
If this answer is helpful please mark correct and helpful!
Regards,
Christopher Perry
Regards,
Chris Perry