locked
CRM2011 JavaScript error occurring RRS feed

  • Question

  • Took this piece from a tutorial but I get error regarding: 

    There was an error with this field's customised event:

    Field:window

    Event:onload

    Error:Object expected

    Code:

    function ProfilePicture_onFormLoad() {
        // make sure there's a profile picture web resource added in form.
        var profilePictureElement = Xrm.Page.getControl("WebResource_ProfilePicture");
        if (!profilePictureElement) {
            return;
        }
        var facebookAttribute = Xrm.Page.getAttribute("new_facebook");
        if (facebookAttribute) {
            var profileUrl = facebookAttribute.getValue();
            if (profileUrl) {
                var profilePictureUrl = getProfilePictureUrl(profileUrl);
                if (profilePictureUrl) {
                    // set src attribute of default profile picture web resource.
                    profilePictureElement.setSrc(profilePictureUrl);
                    return;
                }
            }
        }
    }

    function getProfilePictureUrl(profileUrl) {
        // trim trailing forward slash in url
        profileUrl = profileUrl.replace(/\/*$/, "");//code appears in green while debuging in IE inspector
        var patterns = [];
        patterns[0] = /^http:\/\/www\.facebook\.com\/([a-zA-Z0-9\.]+?)$/;   
        patterns[1] = /^http:\/\/www\.facebook\.com\/profile\.php\?id=(\d+?)$/; 
        patterns[2] = /^http:\/\/www\.facebook\.com\/groups\/(\d+?)$/;

        for (i in patterns) {
            var matches = patterns[i].exec(profileUrl);
            if (matches) {
                return "http://graph.facebook.com/" + matches[1] + "/picture?type=normal";
            }
        }
        return null;
    }

    I also noticed that while trying to debug the second method where highlighted, it appears like it's been commented out. Any suggestions?

    Friday, April 27, 2012 2:55 PM

Answers

  • Hi,

    make sure you are following all steps defined in that tutorial,

    Error:Object expected it seems you are refering such object which is not available.


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by ginger_1921 Monday, April 30, 2012 1:46 PM
    Friday, April 27, 2012 4:35 PM
    Moderator
  • I've found that the Developer Tools are pretty helpful in debugging an issue like this.

    • Press F12 to open Developer Tools.
    • Switch to the Script tab
    • Change the script file to the one with your code in it.  You probably have to scroll way down in the list.
    • Add a breakpoint at the beginning of the method.
    • Click Start Debugging.
    • Refresh the page.
    • When the debugger catches the breakpoint, step through the code (F10) until the error occurs again.

    • Marked as answer by ginger_1921 Monday, April 30, 2012 1:46 PM
    Friday, April 27, 2012 7:07 PM
  • If any of the above response answered your question, please mark the response as an answer and also vote as helpful.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    • Marked as answer by ginger_1921 Monday, April 30, 2012 1:46 PM
    Friday, April 27, 2012 9:22 PM
  • Are you sure the profileUrl being passed in has a value?  The call to replace looks correct.
    • Marked as answer by ginger_1921 Monday, April 30, 2012 1:46 PM
    Monday, April 30, 2012 12:38 PM

All replies

  • Hi,

    make sure you are following all steps defined in that tutorial,

    Error:Object expected it seems you are refering such object which is not available.


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by ginger_1921 Monday, April 30, 2012 1:46 PM
    Friday, April 27, 2012 4:35 PM
    Moderator
  • I've found that the Developer Tools are pretty helpful in debugging an issue like this.

    • Press F12 to open Developer Tools.
    • Switch to the Script tab
    • Change the script file to the one with your code in it.  You probably have to scroll way down in the list.
    • Add a breakpoint at the beginning of the method.
    • Click Start Debugging.
    • Refresh the page.
    • When the debugger catches the breakpoint, step through the code (F10) until the error occurs again.

    • Marked as answer by ginger_1921 Monday, April 30, 2012 1:46 PM
    Friday, April 27, 2012 7:07 PM
  • In addition to Mahendar Pal
    Your code looks perfect.
    Please check your schema names that your are using in your code.(Does WebResource_ProfilePicture, and new_facebook is on your form
    I think you want to display facebook picture in your contact form.
    Please check the following blog 
    http://blogs.msdn.com/b/crm/archive/2011/09/28/displaying-a-contact-s-facebook-picture-in-microsoft-dynamics-crm-2011.aspx

    This blog will tell you step by step process to complete your task.


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!



    Friday, April 27, 2012 7:10 PM
  • Thanks all for the replies.

    I should have mentioned the steps I have take. 

    I created a field called new_facebook and the format for this field is URL. I also created a web resource called WebResource_ProfilePicture which basically points to a default image which does appear on the contact form. 

    The JS code is running on Form Load. At the point where the form loads is where the error appears. I couldn't step through it using IE inspector, it never hit my break points.

    Friday, April 27, 2012 8:50 PM
  • If any of the above response answered your question, please mark the response as an answer and also vote as helpful.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    • Marked as answer by ginger_1921 Monday, April 30, 2012 1:46 PM
    Friday, April 27, 2012 9:22 PM
  • i was able to add some alerts and found the issue was with the second method. the piece in bold returns null. so i think it's some sort of formatting...anyone suggestion something?

    function getProfilePictureUrl(profileUrl) {
        // trim trailing forward slash in url
    	alert('getting profileUrl');
        profileUrl = profileUrl.replace(/\/*$/, "");
    	alert(profileUrl);
        var patterns = [];
    	alert('patterns');
        patterns[0] = /^http:\/\/www\.facebook\.com\/([a-zA-Z0-9\.]+?)$/;  
    	alert('pattern 1');	
    	alert(patterns[0]);
        patterns[1] = /^http:\/\/www\.facebook\.com\/profile\.php\?id=(\d+?)$/; 
    	alert('pattern 2');	
    	alert(patterns[1]);
        patterns[2] = /^http:\/\/www\.facebook\.com\/groups\/(\d+?)$/;
    	alert('pattern 3');	
    	alert(patterns[2]);
    	
        for (i in patterns) {
            var matches = patterns[i].exec(profileUrl);
            if (matches) {
                return "http://graph.facebook.com/" + matches[1] + "/picture?type=normal";
            }
        }
        return null;

    Monday, April 30, 2012 11:05 AM
  • Are you sure the profileUrl being passed in has a value?  The call to replace looks correct.
    • Marked as answer by ginger_1921 Monday, April 30, 2012 1:46 PM
    Monday, April 30, 2012 12:38 PM
  • I am using the same example in the tutorial...so the value i am entering in the text box is:

    http://www.facebook.com/groups/21809302488


    Monday, April 30, 2012 1:36 PM
  • So before the bolded line, profileUrl = "http://www.facebook.com/groups/21809302488"

    And after the bolded line, profileUrl = null

    Is that right?  Could you double-check with the debugger or an alert?

    Monday, April 30, 2012 1:41 PM
  • ok it's working.

    it was the way the url value i was passing in. 

    thanks all.

    Monday, April 30, 2012 1:46 PM
  • It doesn't like a url like this:

    http://www.facebook.com/profile.php?id=586210302

    But something like this works:

    http://www.facebook.com/586210302

    Monday, April 30, 2012 1:48 PM