New doc or set value in Frappe Call

I am trying to push a value from doctype A to B.
My use case is:

  1. when there is no entry in entry, create one and push value in field 1
  2. if there is any entry found, just set value to field 2 of that doc.

here’s my code

for(var i=0;i<id_data2.length;i++)
{

//on other doctype

var zz;
frappe.call({
    method:"frappe.client.get_list",
    args: {
        doctype:"fup",
	fields: ['*'],
	filters: {"name":id_data2[i]},
	
    },
    callback: function(r) {
zz=r.message[0]
    	
    }
})
if(zz)
{
	frappe.call({
					"method": "frappe.client.set_value",
					"args": {
						"doctype": "fup",
						"name": id_data2[i],
						"fieldname": {
							 "followup_2": message_s
									},
							}
				});
}

else if(!zz)
{
	frappe.call({
"method": "frappe.client.insert",
"args": {
"doc": {"doctype":"fup","name":id_data2[i],"followup_1":message_s,"recid":id_data2[i]}
}

});

}
}

fup is doctype B.

I am getting all the list existing in doctype B and there is a for loop at above which is pushing id_ist one by one.
If in frappe get list of doctype B(fup), if there exist that entry, use frappe set value method and if not then frappe.client.insert method.

Any help what I am doing wrong as for existing segement, set value isn’t working but i am getting error of duplicacy

Any help ?