none
Error: Object doesn't support property or method setAttribute

    Question

  • HI,

    I'm new to programming in CRM and I'm trying to write a simple javascript that will take the first and last name of a lead and populate the company name field. Currently I only have the the OnChange event triggering on the last name. When I change the last name I receive the above error. Here is my code....

    function updateCompanyName()
    {
        var newCompanyName; 
        var firstName;
        var lastName;   
    
        firstName = Xrm.Page.getAttribute("firstname").getValue()
        lastName = Xrm.Page.getAttribute("lastname").getValue();
        newCompanyName = firstName + " " + lastName;  
      
        Xrm.Page.getAttribute("companyname").setAttribute(newCompanyName);
    
        alert(newCompanyName);
    }

    Thank you for the help!!

    Gary

    Friday, February 22, 2013 2:07 AM

Answers

  • That should work - maybe double check for any other typos - here is the final version - which I just tested on the OnChange event of the Lead form Last Name field.

    function updateCompanyName() {
        var newCompanyName;
        var firstName;
        var lastName;
    
        firstName = Xrm.Page.getAttribute("firstname").getValue()
        lastName = Xrm.Page.getAttribute("lastname").getValue();
        newCompanyName = firstName + " " + lastName;
    
        Xrm.Page.getAttribute("companyname").setValue(newCompanyName);
    
        alert(newCompanyName);
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Marked as answer by GaryMk Friday, February 22, 2013 5:08 PM
    Friday, February 22, 2013 4:59 PM

All replies

  • Instead of setAttribute(value) use setValue(value)

    Xrm.Page.getAttribute("companyname").setValue(newCompanyName);


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, February 22, 2013 3:23 AM
  • Hi Jason,

    I now receive an "undefined" error.

    Xrm.Page.getAttribute("companyname").setValue(newCompanyName);

    Thanks,

    Gary

    Friday, February 22, 2013 4:51 PM
  • That should work - maybe double check for any other typos - here is the final version - which I just tested on the OnChange event of the Lead form Last Name field.

    function updateCompanyName() {
        var newCompanyName;
        var firstName;
        var lastName;
    
        firstName = Xrm.Page.getAttribute("firstname").getValue()
        lastName = Xrm.Page.getAttribute("lastname").getValue();
        newCompanyName = firstName + " " + lastName;
    
        Xrm.Page.getAttribute("companyname").setValue(newCompanyName);
    
        alert(newCompanyName);
    }


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Marked as answer by GaryMk Friday, February 22, 2013 5:08 PM
    Friday, February 22, 2013 4:59 PM
  • Thank you, that worked.

    I updated the web resource and published it. Do I also need to save and publich the form?

    Gary

    Friday, February 22, 2013 5:10 PM
  • No - when you first create a web resource it gets published right away but anytime you make an update after that you will need to publish the resource. You would only need to republish the form if you for example change the method name from "updateCompanyName" to "updateCompanyField". This would require you going into the event on the form and updating which method is called - then publishing to make the changes go into effect.

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Friday, February 22, 2013 5:15 PM
  • Thank you for the help!!!
    Friday, February 22, 2013 5:17 PM