locked
JScript: Isses handling empty values in a lookup field RRS feed

  • Question

  • Hi there

    I am having some issues with a JScript which is supposed to hide or show certain fields on a form depending on the value of a lookup. The script is bound to the onLoad event of the form and the onChange event of the lookup.

    However if I delete the value of the lookup (select the lookup field and press DEL, then the JScript returns an error:

    Field:new_address1_countryid

    Event:onchange

    Error:Unable to get value of the property '0': Object is null or undefined.

    Below is my code. I tried replacing if (countryLookup != null) with if (countryLookup != null && countryLookup.lenght > 0) but then the script simply wouldn't execute when I change the values of the lookup field (no error is returned though).

    function displayAddressFields(countryField, roiCountyField, stateProvField, cityField) {
    	
    	var countryLookup = Xrm.Page.getAttribute(countryField);
    		if (countryLookup != null) {
    			var lookupID = countryLookup.getValue()[0].id;
    			if (lookupID == "{B0C6F05C-146A-E211-B4C7-3C4A92DBC8C7}") {
    				Xrm.Page.ui.controls.get(roiCountyField).setVisible(true);
    				Xrm.Page.ui.controls.get(stateProvField).setVisible(false);
    				Xrm.Page.ui.controls.get(cityField).setVisible(false);
    			}
    		else {
    				Xrm.Page.ui.controls.get(roiCountyField).setVisible(false);
    				Xrm.Page.ui.controls.get(stateProvField).setVisible(true);
    				Xrm.Page.ui.controls.get(cityField).setVisible(true);
    		}
    	}
    }
    
    function clearAddressFields(countryField, roiCountyField, stateProvField, cityField) {
    	var countryLookup = Xrm.Page.getAttribute(countryField);
    		if (countryLookup != null) {
    			var lookupID = countryLookup.getValue()[0].id;
    			if (lookupID == "{B0C6F05C-146A-E211-B4C7-3C4A92DBC8C7}") {
    				Xrm.Page.data.entity.attributes.get(stateProvField).setValue('');
    				Xrm.Page.data.entity.attributes.get(cityField).setValue('');
    			}
    		else {
    				Xrm.Page.data.entity.attributes.get(roiCountyField).setValue();
    		}
    	}
    }


    Any ideas?

    Cheers,
    P.

    Thursday, January 31, 2013 3:05 PM

Answers

  • Hello,

    you can just add one condition like below in your code

    var countryLookup = Xrm.Page.getAttribute(countryField);

    if (countryLookup != null) {
    if(countryLookup.getValue()!=null)
    {

    var lookupID = countryLookup.getValue()[0].id;

    //rest of the code
    }

    }


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    • Edited by HIMBAPModerator Thursday, January 31, 2013 3:21 PM
    • Proposed as answer by HIMBAPModerator Thursday, January 31, 2013 3:21 PM
    • Marked as answer by pmdci Thursday, January 31, 2013 4:34 PM
    Thursday, January 31, 2013 3:20 PM
    Moderator

All replies

  • Hello there,

    I need to rite a JScript to check the value of a lookup field. So if the lookup field value is X, then I will do a couple of things (hide and show some fields, which I know how to do).

    What would be the easiest way to check for the value of a lookup?

    Cheers,

    P.

    Thursday, January 31, 2013 11:00 AM
  • Hi,

    Try Below JS.

     var testLookup = Xrm.Page.getAttribute("new_accountid");
     if (testLookup != null) {
         var lookupName = testLookup.getValue()[0].name;
         if (lookupName == "X") {
             // Businesslogic
         }
         else {
         // Business logic
           
         }
     }



    Chandan - http://mscrm-chandan.blogspot.in/ I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful !!!

    Thursday, January 31, 2013 11:26 AM
  • Hi,

    Try this

    function displayAddressFields(countryField, roiCountyField, stateProvField, cityField) { var countryLookup = Xrm.Page.getAttribute(countryField).getValue(); if (countryLookup != null) { var lookupID = countryLookup.getValue()[0].id; if (lookupID == "{B0C6F05C-146A-E211-B4C7-3C4A92DBC8C7}") { Xrm.Page.ui.controls.get(roiCountyField).setVisible(true); Xrm.Page.ui.controls.get(stateProvField).setVisible(false); Xrm.Page.ui.controls.get(cityField).setVisible(false); } else { Xrm.Page.ui.controls.get(roiCountyField).setVisible(false); Xrm.Page.ui.controls.get(stateProvField).setVisible(true); Xrm.Page.ui.controls.get(cityField).setVisible(true); } } }

    function clearAddressFields(countryField, roiCountyField, stateProvField, cityField) {
    	var countryLookup = Xrm.Page.getAttribute(countryField).getValue();
    		if (countryLookup != null) {
    			var lookupID = countryLookup.getValue()[0].id;
    			if (lookupID == "{B0C6F05C-146A-E211-B4C7-3C4A92DBC8C7}") {
    				Xrm.Page.data.entity.attributes.get(stateProvField).setValue('');
    				Xrm.Page.data.entity.attributes.get(cityField).setValue('');
    			}
    		else {
    				Xrm.Page.data.entity.attributes.get(roiCountyField).setValue();
    		}
    	}
    }


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !



    • Edited by _Vikram Thursday, January 31, 2013 3:11 PM
    • Proposed as answer by HIMBAPModerator Thursday, January 31, 2013 3:20 PM
    • Unproposed as answer by pmdci Thursday, January 31, 2013 4:35 PM
    Thursday, January 31, 2013 3:10 PM
  • Hello,

    you can just add one condition like below in your code

    var countryLookup = Xrm.Page.getAttribute(countryField);

    if (countryLookup != null) {
    if(countryLookup.getValue()!=null)
    {

    var lookupID = countryLookup.getValue()[0].id;

    //rest of the code
    }

    }


    Contact Me
    Follow me on Twitter
    My Facebook Page
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    • Edited by HIMBAPModerator Thursday, January 31, 2013 3:21 PM
    • Proposed as answer by HIMBAPModerator Thursday, January 31, 2013 3:21 PM
    • Marked as answer by pmdci Thursday, January 31, 2013 4:34 PM
    Thursday, January 31, 2013 3:20 PM
    Moderator