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

I am just eyeballing here, are you sure the value you are setting it to is correct? if it is an import, where are you picking the defined variable from? like 'code', 'prenom' and these fields?

 

I am doing the exact same thing in one of my script and it seems to be working fine, js here is a JSON object:

	for(i=0;i<js.data.length;i++)
		{
		var gr=new GlideRecord(importTable);
		gr.addQuery('u_odin_id',js.data[i]["Application.id"]);
		gr.query();
		if(gr.next())
			{
			gr.name=js.data[i]["Application.name"];
			gr.u_technical_service_director=js.data[i]["User.email"];
			gr.u_under_change_freeze=js.data[i]["Application.under_change_freeze"];
			gr.short_description = js.data[i]["Application.description"];
			gr.u_lifecycle_state=js.data[i]["Application.lifecycle_state"];
		    gr.u_support_model=js.data[i]["Application.support_model"];
			gr.u_organization=js.data[i]["ApplicationOrgNode.org_tree_string"];
			gr.update();
		}
		else
			{
			gr.initialize();
			gr.u_odin_id=js.data[i]["Application.id"];
			gr.name=js.data[i]["Application.name"];
			gr.u_technical_service_director=js.data[i]["User.email"];
			gr.u_under_change_freeze=js.data[i]["Application.under_change_freeze"];
			gr.u_lifecycle_state=js.data[i]["Application.lifecycle_state"];
		    gr.u_support_model=js.data[i]["Application.support_model"];
			gr.u_organization=js.data[i]["ApplicationOrgNode.org_tree_string"];
			gr.insert();
			
		}
	}