Hi Mike,
I this is related to JavaScript Objects creation, in this case you are trying to set the JavaScript Property name dynamically which I believe is possible too.
Try changing below line from
entity.new_field = paramOneValue;
TO
entity[paramOne] = paramOneValue;
OR
eval("entity." + paramOne + " = '" + paramOneValue + "'");
And still if problem persists which I think should not, then use the below change to your code which is if... else blocks
function testFunction (executionContext,paramOne){ // paramOne will contain the field logical name (new_field)
var formContext = executionContext.getFormContext();
var lead = formContext.getAttribute("new_lead").getValue(); // Get the value of the lead lookup field
if (lead !== null) { // Check if the lead lookup field contains data
var leadId = lead[0].id.slice(1, -1); //Slice the lead ID into a usable format
var paramOneValue = formContext.getAttribute(paramOne).getValue(); // Get the field value of the passed field name
var entity = {};
if (paramOne ==="new_field"){
entity.new_field = paramOneValue; // This seems to work if I replace paramOne with the actual lead field logical name
}
else if (paramOne ==="new_fieldTwo"){
entity.new_fieldTwo = paramOneValue; // And go on for different possible fields and places you are triggering this function from....
}
var req = new XMLHttpRequest();
req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/leads("+leadId+")", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
//Success
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
}
}
Hope this will solve your query. Kindly mark this as verified answer if it does.
As always feel free to reach out to me if more assistance is needed.
Have a great day ahead.....
Best Regards
Abhishek Dhoriya
Consultant @ Microsoft India