locked
how to get the lookup field value back RRS feed

  • Question

  • Hi,

    I am working on displaying a prompt message when a lookup field value is selected as '0'.

    After the user responds to the prompt box, the lookup field has to display its old value instead of '0'.

    How can I do this?

    function abc()

    {

    var count=Xrm.Page.data.entity.attributes.get("new_count").getValue();

    var countname=count[0].name;

    if (countname==null){

    return;

    }

    if(countname!=null && countname=="0")

    {

    alert("Please close the activity before making count as 0");

    Xrm.Page.getAttribute("new_count").setValue(countname); 

    }

    }

    Thanks

    Thursday, September 25, 2014 7:11 PM

Answers

  • You may have to set the value of initialcount in the form's onload event.  I am not sure the field value will have been populated at the time your WebResource is loaded.

    var initialcount;
    
    function onFormLoad() {
        initialcount = Xrm.Page.getAttribute("new_count").getValue();
    }

    • Marked as answer by ReignFan Monday, September 29, 2014 1:19 PM
    Thursday, September 25, 2014 9:04 PM
  • try:-

    //Code onLoad of form
    //In CRM if you declare a variable without var it becomes a global variable
    if (Xrm.Page.data.entity.attributes.get("new_count").getValue() != null) {// Ideally it should be countid if it is a lookup
        previousCountName = Xrm.Page.data.entity.attributes.get("new_count").getValue()[0].name;
        previousCountId = Xrm.Page.data.entity.attributes.get("new_count").getValue()[0].id;
    }
    
    //Code onchange of field
    if (Xrm.Page.data.entity.attributes.get("new_count").getValue() != null) {
        if (Xrm.Page.data.entity.attributes.get("new_count").getValue() == 0) {
            alert("Please close the activity before making count as 0");
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = previousCountId;
            lookupValue[0].name = previousCountId;
            lookupValue[0].entityType = LookupEntityTypeName;
    
            Xrm.Page.getAttribute('new_count').setValue(lookupValue);
        }
    }
    


    Regards Faisal

    • Marked as answer by ReignFan Monday, September 29, 2014 1:19 PM
    Friday, September 26, 2014 3:00 PM

All replies

  • Make 'initialcountname' a global variable and set its value when the form loads.  Then reset to that value instead of countname.

    Thursday, September 25, 2014 8:12 PM
  • tried this.. but no change!!

    var initialcountname=Xrm.Page.data.entity.attributes.get("new_count").getValue();
    function abc()

    {

    var count=Xrm.Page.data.entity.attributes.get("new_count").getValue();

    var countname=count[0].name;

    if (countname==null){

    return;

    }

    if(countname!=null && countname=="0")

    {

    alert("Please close the activity before making count as 0");
    if(initialcountname!=null)
    {Xrm.Page.getAttribute("new_count").setValue(initialcountname);}
    }

    }

    Thanks

    Thursday, September 25, 2014 8:38 PM
  • You may have to set the value of initialcount in the form's onload event.  I am not sure the field value will have been populated at the time your WebResource is loaded.

    var initialcount;
    
    function onFormLoad() {
        initialcount = Xrm.Page.getAttribute("new_count").getValue();
    }

    • Marked as answer by ReignFan Monday, September 29, 2014 1:19 PM
    Thursday, September 25, 2014 9:04 PM
  • You can also write a webservice call and get last value from DB. Check this

    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, September 26, 2014 2:44 AM
    Moderator
  • yes, before the webResource is triggered there will be a value for the count. It is an OnChange event of that field.

    Thanks

    Friday, September 26, 2014 11:52 AM
  • I believe this is more or less close to what you need.

    I’ve added a lookup helper to make the actual implementation short and to avoid declaration of private fields in global scope ,which is a bad practice!

    Note: this is just pseudo/wrote this directly into the thread:

    var countLui;
    //form load
    function OnCrmLoad() {
        countLui = new MyLookup("new_count");
        countLui.AddOnChange(OnCountChange)
    }
    //count change
    function OnCountChange() {
        if (countLui.HasValue()) {
        
            if (countLui.GetName() == "0") {
                alert("Please close the activity before making count as 0");
                countLui.ToLastValue();
                return;
            }
    
            countLui.SetLastValue();
        }
    }
    //helper
    function MyLookup(sId){
        var ml = this;
        ml.Attribute = Xrm.Page.getAttribute(sId);
        ml.LastValue = ml.Attribute.getValue();
        
        ml.SetLastValue = function() {
            ml.LastValue = ml.Attribute.getValue();
        }
        
        ml.AddOnChange = function(func) {
            ml.Attribute.addOnChange(func);
        }
        
        ml.Get = function() {
            return ml.Attribute.getValue();
        }
        
        ml.GetName = function(index) {
            return ml.Get()[index || 0].name;
        }
        
        ml.HasValue = function() {
            return ml.Get() != null;
        }
        
        ml.ToLastValue = function(){
            ml.Attribute.setValue(ml.LastValue);
        }
    }
    


    Dev Blog: Dynamics CRM - Thinking outside the Box

    Friday, September 26, 2014 12:03 PM
  • What is the type of count field. Is it lookup or picklist?

    Regards Faisal

    Friday, September 26, 2014 2:11 PM
  • try:-

    //Code onLoad of form
    //In CRM if you declare a variable without var it becomes a global variable
    if (Xrm.Page.data.entity.attributes.get("new_count").getValue() != null) {// Ideally it should be countid if it is a lookup
        previousCountName = Xrm.Page.data.entity.attributes.get("new_count").getValue()[0].name;
        previousCountId = Xrm.Page.data.entity.attributes.get("new_count").getValue()[0].id;
    }
    
    //Code onchange of field
    if (Xrm.Page.data.entity.attributes.get("new_count").getValue() != null) {
        if (Xrm.Page.data.entity.attributes.get("new_count").getValue() == 0) {
            alert("Please close the activity before making count as 0");
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = previousCountId;
            lookupValue[0].name = previousCountId;
            lookupValue[0].entityType = LookupEntityTypeName;
    
            Xrm.Page.getAttribute('new_count').setValue(lookupValue);
        }
    }
    


    Regards Faisal

    • Marked as answer by ReignFan Monday, September 29, 2014 1:19 PM
    Friday, September 26, 2014 3:00 PM
  • //Code for onLoad event function of the form
    //In CRM if you declare a variable without var it becomes a global variable
    if (Xrm.Page.getAttribute("new_count").getValue() != null) {// Ideally it should be countid if it is a lookup
        previousCountName =Xrm.Page.getAttribute("new_count").getValue()[0].name;
        previousCountId = Xrm.Page.getAttribute("new_count").getValue()[0].id;
    }
    
    //Code for onchange event function of lookup field
    if (Xrm.Page.getAttribute("new_count").getValue() != null) {
        var count = Xrm.Page.getAttribute("new_count").getValue();
    	
        if (count.getValue()[0].name === "0") {
            alert("Please close the activity before making count as 0");
    		
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = previousCountId;
            lookupValue[0].name = previousCountId;
            lookupValue[0].entityType = LookupEntityTypeName; // eg: "contact"
    
            Xrm.Page.getAttribute('new_count').setValue(lookupValue);
        }
    }


    Hope this helps. Amar

    CRM Forum Guidance on how to Help Us Help You

    Friday, September 26, 2014 3:55 PM
  • It is a lookup field..
    Monday, September 29, 2014 1:19 PM