Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Autopopulate fields based on the selection of the lookup field. Giving error TypeError: Cannot read property 'sr_email' of undefined

Posted on by 340

Here i have two entities Branch and Store. On Branch entity there is lookup field pointing to Store entity. When lookup is loaded automatically two fields email and phone on Branch entity are need to be loaded with Store entity email and phone details. I am getting the error TypeError: Cannot read property 'sr_email' of undefined or TypeError: Cannot read property 'sr_email' of undefined. I could unable to fix this error. Below is the code.

function BranchOnLoad() {
var svc = new Mscrm_OrganizationService(null, null, true);
var _branchLookup = new Array();
_branchLookup = Xrm.Page.getAttribute("br_storeassigned").getValue();

if (_branchLookup != null) {
debugger;
var branchName = _branchLookup[0].attributes.name.value;
var fetch = '<fetch mapping="logical">' +
'<entity name="sr_stores">' +
'<attribute name="sr_storeid"/>'+
'<attribute name="sr_name"/>'+
'<attribute name="sr_email"/>' +
'<attribute name="sr_phone1"/>' +
'<order attribute="sr_name" descending="false" />' +
'<filter type="and">' +
'<condition attribute="sr_name" operator="eq" value="' + branchName + '" /> ' +
'</filter>' +
'</entity>' +
'</fetch>';

var res = svc.Fetch(fetch);
if (res[0].attributes.sr_email) {
Xrm.Page.getAttribute("br_branchemail").setValue(res[0].attributes.sr_email.value);
}
else {
return;
}
if (res[0].attributes.sr_phone1) {
Xrm.Page.getAttribute("br_branchphone").setValue(res[0].attributes.sr_phone1.value);
}
else {
return;
}
}
if (_branchLookup == null) {
Xrm.Page.getAttribute("br_branchemail").setValue(null);
Xrm.Page.getAttribute("br_branchphone").setValue(null);

}
Xrm.Page.getAttribute("br_branchemail").setSubmitMode("always");
Xrm.Page.getAttribute("br_branchphone").setSubmitMode("always");
}

*This post is locked for comments

  • Sandy Hello Profile Picture
    Sandy Hello 340 on at
    RE: Autopopulate fields based on the selection of the lookup field. Giving error TypeError: Cannot read property 'sr_email' of undefined

    var xml_special_to_escaped_one_map = {

       '&': '&',

       '"': '"',

       '<': '<',

       '>': '>'

    };

    var escaped_one_to_xml_special_map = {

       '&': '&',

       '"': '"',

       '<': '<',

       '>': '>'

    };

    function encodeXml(string) {

       return string.replace(/([\&"<>])/g, function(str, item) {

           return xml_special_to_escaped_one_map[item];

       });

    };

    function decodeXml(string) {

       return string.replace(/("|<|>|&)/g,

           function(str, item) {

               return escaped_one_to_xml_special_map[item];

       });

    }

    Adding this to my code is working but still giving error when the lookup is loaded from lookup window it is working as expected when selecting the lookup value from list when we click on lookup icon

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Autopopulate fields based on the selection of the lookup field. Giving error TypeError: Cannot read property 'sr_email' of undefined

    It seems that there is no value in "Sr_email" field. Use below code:

    if(typeof(res[0].attributes.sr_email) !== "undefined")

    {

       Xrm.Page.getAttribute("br_branchemail").setValue(res[0].attributes.sr_email.value);

    }else

    {

       return;

    }

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Autopopulate fields based on the selection of the lookup field. Giving error TypeError: Cannot read property 'sr_email' of undefined

    Hi

    Please try the the following;

    Add a console.log("Records returened" + res.length) / alert("Records returened" + res.length) just after var res = svc.Fetch(fetch);

    Could you please try the following?

    function BranchOnLoad()
    {
    var svc = new Mscrm_OrganizationService(null, null, true);
    var _branchLookup = new Array();
    _branchLookup = Xrm.Page.getAttribute("br_storeassigned").getValue();

    if (_branchLookup != null)
    {
    debugger;
    var branchName = _branchLookup[0].attributes.name.value;
    alert("Branch Name : " + branchName);
    var fetch = '<fetch mapping="logical">' +
    '<entity name="sr_stores">' +
    '<attribute name="sr_storeid"/>' +
    '<attribute name="sr_name"/>' +
    '<attribute name="sr_email"/>' +
    '<attribute name="sr_phone1"/>' +
    '<order attribute="sr_name" descending="false" />' +
    '<filter type="and">' +
    '<condition attribute="sr_name" operator="eq" value="' + branchName + '" /> ' +
    '</filter>' +
    '</entity>' +
    '</fetch>';

    var res = null;

    alert("About to fetch records");
    try
    {
    res = svc.Fetch(fetch);
    alert("Records returened" + res.length);

    } catch (err)
    {
    alert("Failed to fetch records : " + err.message);

    }


    if (res[0].attributes.sr_email)
    {
    Xrm.Page.getAttribute("br_branchemail").setValue(res[0].attributes.sr_email.value);
    }
    else {
    return;
    }
    if (res[0].attributes.sr_phone1) {
    Xrm.Page.getAttribute("br_branchphone").setValue(res[0].attributes.sr_phone1.value);
    }
    else {
    return;
    }
    }
    if (_branchLookup == null) {
    Xrm.Page.getAttribute("br_branchemail").setValue(null);
    Xrm.Page.getAttribute("br_branchphone").setValue(null);

    }
    Xrm.Page.getAttribute("br_branchemail").setSubmitMode("always");
    Xrm.Page.getAttribute("br_branchphone").setSubmitMode("always");
    }

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Autopopulate fields based on the selection of the lookup field. Giving error TypeError: Cannot read property 'sr_email' of undefined

    In addition I would suggest to use  web API to generate your code using CRMRESTBuilder .

    Take a look below video, am sure you will love it and it's easy to build your code with that you can test also.

    m.youtube.com/watch

  • David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: Autopopulate fields based on the selection of the lookup field. Giving error TypeError: Cannot read property 'sr_email' of undefined

    The error indicates that your query has not returned a record - you should check the length of the res variable to check if it has returned a record. Are you sure the query is correct ?

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans