I want to insert a record if a condition is met

hamidouche
Kilo Expert

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

1 ACCEPTED SOLUTION

Suseela Peddise
Kilo Sage

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.

 

View solution in original post

10 REPLIES 10

Suseela Peddise
Kilo Sage

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.

 

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

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

Justin77
Mega Guru

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