- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2020 07:51 AM
Hi,
I am importing some data and inserting them as rows in a table. I first query the table to see if a record already exists. If it does, then I will only update one of its fields, called 'statut', otherwise I will insert all new records (4 fields)
The problem is that I do update the existing record, but it inserts it also as a new record, so there's a duplicate record. As you can see, I also do a GlideRecord twice on the same table, which I feel a bit redundant and maybe even inefficient. Here is my code:
var recordDemande = new GlideRecord('table_name');
recordDemande.initialize();
//check if the record exists: do I need to GlideRecord the table again?
var checkRecord = new GlideRecord('same_table as above');
checkRecord .addEncodedQuery('u_codeLIKEA10012' + code);
checkRecord .query();
if(checkRecord .next()){
checkRecord .setValue('u_statut', statut);
}else{
recordDemande.setValue("u_nom", nom);
recordDemande.setValue("u_prenom", prenom);
recordDemande.setValue("u_code", code);
recordDemande.setValue("u_active", statut);
}
recordDemande.update();
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2020 08:08 AM
Hi,
Try the below code:
var checkRecord = new GlideRecord('<<table>>');
checkRecord .addEncodedQuery('u_codeLIKEA10012' + code);
checkRecord .query();
if(checkRecord .next()){ //if it is exists update the value
checkRecord .setValue('u_statut', statut);
checkRecord.update();
}
else{ //if not create a new Record
checkRecord.initialize();
checkRecord.setValue("u_nom", nom);
checkRecord .setValue("u_prenom", prenom);
....
checkRecord.insert();
}
If I have answered your question, please mark my response as correct and/or helpful.
Thanks,
Suseela P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2020 08:08 AM
Hi,
Try the below code:
var checkRecord = new GlideRecord('<<table>>');
checkRecord .addEncodedQuery('u_codeLIKEA10012' + code);
checkRecord .query();
if(checkRecord .next()){ //if it is exists update the value
checkRecord .setValue('u_statut', statut);
checkRecord.update();
}
else{ //if not create a new Record
checkRecord.initialize();
checkRecord.setValue("u_nom", nom);
checkRecord .setValue("u_prenom", prenom);
....
checkRecord.insert();
}
If I have answered your question, please mark my response as correct and/or helpful.
Thanks,
Suseela P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2020 09:46 AM
hi Suseela,
Unfortunately, it's not working. Specifically, it's not updating the existing record or if there it's duplicating the same record again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2020 10:48 AM
Hi,
My bad I put the wrong field name here: checkRecord .setValue('u_statut', statut);
it should be: checkRecord .setValue('u_active', statut);
so yes this solution works.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2020 08:12 AM
Try moving your "initialize" call into your else conditional. That is what is creating new records, so you should only call "initialize" when you want that new record created. And if initializing, you should do an insert call instead of an update. So your final code might look something like:
//check if the record exists: do I need to GlideRecord the table again?
var checkRecord = new GlideRecord('same_table as above');
checkRecord.addEncodedQuery('u_codeLIKEA10012' + code);
checkRecord.query();
if(checkRecord.next()){
checkRecord.setValue('u_statut', statut);
checkRecord.update();
}else{
checkRecord.initialize();
checkRecord.setValue("u_nom", nom);
checkRecord.setValue("u_prenom", prenom);
checkRecord.setValue("u_code", code);
checkRecord.setValue("u_active", statut);
checkRecord.insert();
}