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

    السؤال

  • 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

    12/ربيع الثاني/1434 02:07 ص

الإجابات

  • 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

    • تم وضع علامة كإجابة بواسطة GaryMk 12/ربيع الثاني/1434 05:08 م
    12/ربيع الثاني/1434 04:59 م
    المشرف

جميع الردود

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

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


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    12/ربيع الثاني/1434 03:23 ص
    المشرف
  • Hi Jason,

    I now receive an "undefined" error.

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

    Thanks,

    Gary

    12/ربيع الثاني/1434 04:51 م
  • 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

    • تم وضع علامة كإجابة بواسطة GaryMk 12/ربيع الثاني/1434 05:08 م
    12/ربيع الثاني/1434 04:59 م
    المشرف
  • Thank you, that worked.

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

    Gary

    12/ربيع الثاني/1434 05:10 م
  • 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

    12/ربيع الثاني/1434 05:15 م
    المشرف
  • Thank you for the help!!!
    12/ربيع الثاني/1434 05:17 م