Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

JS reference error

Posted on by

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

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: JS reference error

    Change function name from "toOnSave" to "OnSave"

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JS reference error

    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.

  • RE: JS reference error

    I had already added the createdby field. That doesn't seem to be the problem

  • RE: JS reference error

    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.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JS reference error

    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";

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JS reference error

    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.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JS reference error

    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.

  • RE: JS reference error

    Hi Goutam,

    I used your solution, but am getting a different error now.

    7510.script-error.PNG

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: JS reference error

    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 .

  • Piyush Paliwal Profile Picture
    Piyush Paliwal 817 on at
    RE: JS reference error

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans