locked
CRM 2011 Function to copy contents of Contact lookup to Name primary field of custom entity RRS feed

  • Question

  • OK I have been struggling with this stupid problem for 3 days and am going to ask for help.  To preface this, I am (obviously) not a Javascript expert...or even amateur.  My knowledge is pretty limited.  However, I don't have a backup and have to figure this out.

    So I am trying to accomplish what i thought would be a very simple and common task.  I have a custom entity called Resumes.  I would like the primary key/name field (new_name is field name) to be populated with lookup data from the Contact entity (new_applicantname is field name).  Because this seems like a common scenario, I was surprised that all the solutions I found so far present something much more complicated than what I think I need - ie: they add time/date to name, they work with arrays and option sets, they slice bread, etc.  I want to simply copy the contents of one field to another.

    I need this to work on Resume creation/onload, and if/when Resume is changed/onsave.

    I found the script below which works onload in IE, but not in Chrome.  It also works onsave in both IE and Chrome.

    The problem is that it concatenates the date/time, which I don't want.  I need the REsume name to match exactly with the Contact lookup name.


          function SetName(){
          var lookupField = Xrm.Page.getAttribute("new_applicantname");

          // Retrieve the Current Date

          var Now = new Date();

          // Convert the Date to a String

          var StringNow = Now.toString();

          // Set the Name         

          Xrm.Page.getAttribute("new_name").setValue(lookupField.getValue()[0].name + "-" + StringNow);
          }    


    I made changes to the script to come up with what i thought would be the logical, and much simplified solution, as follows:

          function SetName()
          {
            var lookupField = Xrm.Page.getAttribute("new_applicantname");
            Xrm.Page.getAttribute("new_name").setValue(lookupField);
          }

    But it doesn't work.  In both IE/Chrome, onload it says "This control only accepts strings or null as an input".  Is there something simple that I'm missing here?  Or should I just do this via a workflow?  Am I missing some kind of error handling, or do I need to pre-populate the field with a null variable before I copy the lookup data to it?

    Thank you in advance for any ideas/input.
    -Daniel

    BONUS POINTS: Also, anyone know why Chrome would run script above onsave but not onload?


    Tuesday, November 17, 2015 7:25 PM

All replies

  • Hello Daniel,

    Variable lookupField will contain lookup attribute itself and not value of a lookup.

    To make your it work you can use following code:

    function SetName()
    {
    	var lookupFieldValue = Xrm.Page.getAttribute("new_applicantname").getValue();
    
    	if (lookupFieldValue != null){
    		lookupFieldValue = lookupFieldValue[0].name;
    	}
    
    	Xrm.Page.getAttribute("new_name").setValue(lookupFieldValue);
    }


    Dynamics CRM MVP
    My blog

    • Proposed as answer by Gopinath Reddy Wednesday, November 18, 2015 2:33 AM
    Tuesday, November 17, 2015 7:59 PM
    Moderator