How to solve undifined or null reference in javascript CRM

1:55 AM

How to solve undifined or null reference in javascript CRM
Unable to get property 'value' of undifined or null reference
when you customize dynamic CRM using client side or javascript code some times we get an error coused the variable we had define get null value or undefined .
these are a simple step to solve it:

Step 1
this is my full function
function autofill()
{
 var formtype =Xrm.Page.ui.getFormType();
 
 if (formtype==1){
 
  var objlookup = Xrm.Page.getAttribute("new_contact").getValue();
  if (objlookup!=null)
  {   
   var fetchXml= "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
        "<entity name='contact'>"+
       "<attribute name='fullname' />"+
       "<attribute name='telephone1' />"+
       "<attribute name='contactid' />"+
       "<attribute name='emailaddress1' />"+
       "<attribute name='mobilephone' />"+
       "<order attribute='fullname' descending='false' />"+
       "<filter type='and'>"+
         "<condition attribute='statecode' operator='eq' value='0' />"+
       "</filter>"+
        "</entity>"+
      "</fetch>";
   var _contact = XrmServiceToolkit.Soap.Fetch(fetchXml);
   if (_contact.length > 0){
    var fullname="";
    var mobilephone="";
    var email="";
    if (_contact[0].attributes.fullname != undefined)
     fullname = _contact[0].attributes.fullname.value;
    if (_contact[0].attributes.mobilephone != undefined) 
     mobilephone = _contact[0].attributes.mobilephone.value;        
   }    
  }
 }
}


to solve the problem just add a little bit code below

if (_contact[0].attributes.fullname != undefined)
{
 //you code here
}


Thanks for visiting and happy coding :)

You Might Also Like

0 comments