Hello,
I have the following code to set a lookup field in my custom entity :
var id = Xrm.Page.data.entity.attributes.get("createdby").getValue()[0].id;
var name = Xrm.Page.data.entity.attributes.get("createdby").getValue()[0].name;
var entityType = "new_accident";
if (id != null) {
var lookupVal = new Array();
lookupVal[0] = new Object();
lookupVal[0].id = guid;
lookupVal[0].name = name;
lookupVal[0].entityType = entityType;
Xrm.Page.getAttribute("new_contactid").setValue(lookupVal);
}
I have set it to OnSave, but I get the following error :
ReferenceError: toOnSave is not defined
at eval (eval at RunHandlerInternal (smartconstat.crm4.dynamics.com/.../ClientApiWrapper.aspx), <anonymous>:1:1)
at RunHandlerInternal (smartconstat.crm4.dynamics.com/.../ClientApiWrapper.aspx)
at RunHandlers (smartconstat.crm4.dynamics.com/.../ClientApiWrapper.aspx)
at ExecuteHandler (smartconstat.crm4.dynamics.com/.../ClientApiWrapper.aspx)
at Mscrm.TurboForm.Control.CustomScriptsManager.$Dk_1 (crmcdn.azureedge.net/.../formcontrols.js)
at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandler (crmcdn.azureedge.net/.../formcontrols.js)
at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandlerByDescriptor (crmcdn.azureedge.net/.../formcontrols.js)
at crmcdn.azureedge.net/.../formcontrols.js
at smartconstat.crm4.dynamics.com/.../global.ashx
at Mscrm.TurboForm.Control.Data.DataEntity.$Ew_1 (crmcdn.azureedge.net/.../formcontrols.js)
What could be the reason for this?
Thanks in advance
*This post is locked for comments
Change function name from "toOnSave" to "OnSave"
Hi,
Records are created by CRM users and not by Contact. So created by will return you the user value not contact value. There is something wrong. Either you need to select a different lookup field (i.e. not createdby or set a different lookup field (i.e. not contact)
Hope this helps.
I had already added the createdby field. That doesn't seem to be the problem
Hi Ravi,
I'm trying to set the "Contact lookup field" of my custom entity "Accident" to the person who created the record, and hence "created by". So if Mr. X creates an accident record, a new contact would be created for him.
Ravi's concerns is correct which I missed out , Which entity you are referencing the lookup "new_contactid"? Make sure it's should be user lookup and belongs to User entity as you are setting created by value which belongs to user entity.
In that case Entity type should not be "new_accident" and it should be "systemuser".
lookupVal[0].entityType = "systemuser";
Hi,
Could you please share what exactly you are trying to achieve with this script? Even after fixing the error, this script will not work because CreatedBy is a lookup to User entity whereas you are setting that value to some custom entity lookup "new_accident"
Ensure that you are retrieving the correct field and setting the correct lookup type.
Hope this helps.
This is because created by field is not added in the form , as result control is getting null. Go to form editor and add createdby field in the forms and check again.
Hi Goutam,
I used your solution, but am getting a different error now.
Hi
Try with this -you mentioned "lookupVal[0].id = guid;" it should be id variable.
function OnSaveForm()
{
var id = Xrm.Page.data.entity.attributes.get("createdby").getValue()[0].id;
var name = Xrm.Page.data.entity.attributes.get("createdby").getValue()[0].name;
var entityType = "new_accident";
if (id != null) {
var lookupVal = new Array();
lookupVal[0] = new Object();
lookupVal[0].id = id;
lookupVal[0].name = name;
lookupVal[0].entityType = entityType;
Xrm.Page.getAttribute("new_contactid").setValue(lookupVal);
}
}
also make sure you provided correct function name .
Hey, are there any other JS running on the form? Also, did you try to see if you are able to debug this script in the browser? If yes, was there any error thrown while execution of any of the above mentioned code.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156