Create new customDataEntry using JXA

I’m trying to reliably create/update certain custom data on different tasks, but haven’t be able to work out how to create a new custom data entry using JXA.

I’ve created a helper function which accepts a task (OmniPlan is in the global scope) and then tries to set the key/value.

function setData(context, k, v) {
	if (context.customDataEntries.byName(k).exists()) {
		context.customDataEntries.byName(k).value = v;
	} else {
		var d = OmniPlan.CustomDataEntry({ name: k, value: v.toString() });
		context.customDataEntries.push(d);
	}
}

It fails on the else condition where is tries to push the new custom data entry onto the task.

Thanks so much for your help!