locked
Form scripting... RRS feed

  • Question

  • I am looking to do my first bit of form scripting, I need to update the name field on the project relationship form to be the Opportunity + " - " + Account

    Now I have gone into form properties and tried to modify an example helloWorld function - 

    Function helloWorld()
    {
        var varMyAccountValue = Xrm.Page.getAttribute(“Account”).getValue() ;
        var varMyOpportunityValue = Xrm.Page.getAttribute(“Opportunity”).getValue() ;
        var varMyOpportunityAccountValue = varMyOpportunityValue + " - " + varMyAccountValue
    
       alert("Details " + varMyOpportunityAccountValue );
    }

    Then in the Field Properties for Account I have added the helloWorld.

    Now if I try to preview it doesn't appear to have worked as nothing happens but when I close the form, I receive an error message:

    CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>The value of the property 'helloWorld' is null or undefined, not a Function object</Message>
       <Line>298</Line>

    Can anyone point me in the direction of where I am going wrong?

    Matt

    Wednesday, August 21, 2013 11:51 AM

Answers

  • Fixed it, well sort of.

    If I publish it to our test system it works fine, however, if I preview it on the form it errors?!?

    Thanks for everybodys help.

    Matt

    • Marked as answer by Matt_Hirst_UK Wednesday, August 21, 2013 3:26 PM
    Wednesday, August 21, 2013 3:25 PM

All replies

  • I think account and opportunity both are lookups therefore use their schema names like:-

    Function helloWorld()
    {
       
    var varMyAccountValue = Xrm.Page.getAttribute("new_accountid").getValue()[0].name ;
       
    var varMyOpportunityValue = Xrm.Page.getAttribute("new_opportunityid").getValue()()[0].name ;
       
    var varMyOpportunityAccountValue = varMyOpportunityValue + " - " + varMyAccountValue

       alert
    ("Details " + varMyOpportunityAccountValue );
    }


    Regards Faisal

    Wednesday, August 21, 2013 12:02 PM
  • Hi,
    you have several issues.
    1) is "function" not "Function"
    2) the name of the fields (Account and Opportunity) are probably wrong, if for example are lookups inside a custom entity they will looks like as "new_accountid" or "new_opportunityid", so you need to find the exact names
    3) if the fields are lookup you need to get the name of the lookup, assuming the fields are called new_accountid and new_opportunityid

    something like:

    function helloWorld()
    {
        var varMyAccountValue = Xrm.Page.getAttribute(“new_accountid”).getValue()[0].name;
        var varMyOpportunityValue = Xrm.Page.getAttribute(“new_opportunityid”).getValue()[0].name;
        var varMyOpportunityAccountValue = varMyOpportunityValue + " - " + varMyAccountValue
        alert("Details " + varMyOpportunityAccountValue );
    }


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Wednesday, August 21, 2013 12:03 PM
    • Unproposed as answer by Matt_Hirst_UK Wednesday, August 21, 2013 12:28 PM
    Wednesday, August 21, 2013 12:03 PM
  • I have updated the script, but still receive the following error when I test the form:

    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>The value of the property 'helloWorld' is null or undefined, not a Function object</Message>
       <Line>298</Line>

    Any more ideas?

    Matt

    Wednesday, August 21, 2013 12:09 PM
  • Have commented out all the lines in the code and receive the same error message so I assume it is something to do with the event firing but not able to find the function???

    Matt

    Wednesday, August 21, 2013 12:48 PM
  • Just realised if I preview the form and just close it I receive the error 

    Microsoft Dynamics CRM Error Report Contents
    
    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>Invalid character</Message>
       <Line>3</Line>
       <URL>/%7B635126873610000000%7D/WebResources/ancon_1</URL>
       <PageURL>/Tools/FormEditor/preview.aspx?appSolutionId=%7bB6B22919-4314-E211-A4D0-02BF0A86F1E1%7d</PageURL>
       <Function></Function>
       <CallStack>
       </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
       <BrowserUserAgent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)</BrowserUserAgent>
       <BrowserLanguage>en-us</BrowserLanguage>
       <SystemLanguage>en-us</SystemLanguage>
       <UserLanguage>en-gb</UserLanguage>
       <ScreenResolution>1229x983</ScreenResolution>
       <ClientName>Web</ClientName>
       <ClientTime>2013-08-21T14:30:34</ClientTime>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>2057</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>2057</UserCulture>
        <OrgID>{243A0FF8-F595-E211-96EA-02BF0A86F1E1}</OrgID>
        <UserID>{82BC61EF-740C-E211-A4D0-02BF0A86F1E1}</UserID>
        <CRMVersion>5.0.9690.2865</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>

    It means nothing to me, can anybody help?

    Matt

    Wednesday, August 21, 2013 1:33 PM
  • Hi Matt,

    You've got the Word style quotation marks in your code (“typographers quotation marks. notice how they slant and one faces upwards and the other downwards” ). Replace them with the standard quotation marks ("this is what you want").


    Michael Palmer
    xRMPalmer     @MJFPalmer     Rockstar365

    Wednesday, August 21, 2013 2:29 PM
  • Tried that but still not liking it.

    Matt

    Wednesday, August 21, 2013 2:37 PM
  • Hi Matt,

    Do you still get the Invalid Character error? Please post the full JS web resource.

    Are you calling the hellowWorld function on load of a new form?

    Xrm.Page.getAttribute(“new_accountid”).getValue()

    will return null if called on load of a new entity.


    Michael Palmer
    xRMPalmer     @MJFPalmer     Rockstar365

    Wednesday, August 21, 2013 2:45 PM
  • Hi,

    function updateName()
    {
        var varMyAccountValue = Xrm.Page.getAttribute("orb_accountid").getValue()[0].name;
        var varMyOpportunityValue = Xrm.Page.getAttribute("orb_opportunityid").getValue()[0].name;
        var varMyOpportunityAccountValue = varMyOpportunityValue + " - " + varMyAccountValue;
    
        Xrm.Page.getAttribute("orb_name").setValue(varMyOpportunityAccountValue);
    
    //  alert("Details " + varMyOpportunityAccountValue );
    }
    

    The error, when I close the form down after preview,  I am getting is:

    Microsoft Dynamics CRM Error Report Contents
    
    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>Invalid character</Message>
       <Line>3</Line>
       <URL>/%7B635126934420000000%7D/WebResources/ancon_1</URL>
       <PageURL>/Tools/FormEditor/preview.aspx?appSolutionId=%7bB6B22919-4314-E211-A4D0-02BF0A86F1E1%7d</PageURL>
       <Function></Function>
       <CallStack>
       </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
       <BrowserUserAgent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)</BrowserUserAgent>
       <BrowserLanguage>en-us</BrowserLanguage>
       <SystemLanguage>en-us</SystemLanguage>
       <UserLanguage>en-gb</UserLanguage>
       <ScreenResolution>1229x983</ScreenResolution>
       <ClientName>Web</ClientName>
       <ClientTime>2013-08-21T15:50:51</ClientTime>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>2057</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>2057</UserCulture>
        <OrgID>{243A0FF8-F595-E211-96EA-02BF0A86F1E1}</OrgID>
        <UserID>{82BC61EF-740C-E211-A4D0-02BF0A86F1E1}</UserID>
        <CRMVersion>5.0.9690.2865</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>
    
    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>The value of the property 'helloWorld' is null or undefined, not a Function object</Message>
       <Line>298</Line>
       <URL>/Tools/FormEditor/preview.aspx?appSolutionId=%7bB6B22919-4314-E211-A4D0-02BF0A86F1E1%7d</URL>
       <PageURL>/Tools/FormEditor/preview.aspx?appSolutionId=%7bB6B22919-4314-E211-A4D0-02BF0A86F1E1%7d</PageURL>
       <Function>RaiseEvent(oEvent,customHandler,eventId)</Function>
       <CallStack>
        <Function>RaiseEvent(oEvent,customHandler,eventId)</Function>
        <Function>RaiseOnChangeEvent(oEvent)</Function>
        <Function>HandleAfterLookup(lookupItems)</Function>
        <Function>Lookup(bIgnoreOnClick,bNoDialog,sSearchString,bAsynchOperation)</Function>
        <Function>anonymousanonymous(){Lookup()}</Function>
        <Function>orb_accountid_onchange_handler(eventObj,eventArgs)</Function>
       </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
       <BrowserUserAgent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)</BrowserUserAgent>
       <BrowserLanguage>en-us</BrowserLanguage>
       <SystemLanguage>en-us</SystemLanguage>
       <UserLanguage>en-gb</UserLanguage>
       <ScreenResolution>1229x983</ScreenResolution>
       <ClientName>Web</ClientName>
       <ClientTime>2013-08-21T15:51:28</ClientTime>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>2057</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>2057</UserCulture>
        <OrgID>{243A0FF8-F595-E211-96EA-02BF0A86F1E1}</OrgID>
        <UserID>{82BC61EF-740C-E211-A4D0-02BF0A86F1E1}</UserID>
        <CRMVersion>5.0.9690.2865</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>

    Matt

    Wednesday, August 21, 2013 2:53 PM
  • Sorry just updated and the error shows the updateName that I have changed the function to.

    Matt

    • Marked as answer by Matt_Hirst_UK Wednesday, August 21, 2013 3:26 PM
    • Unmarked as answer by Matt_Hirst_UK Wednesday, August 21, 2013 3:26 PM
    Wednesday, August 21, 2013 2:57 PM
  • Fixed it, well sort of.

    If I publish it to our test system it works fine, however, if I preview it on the form it errors?!?

    Thanks for everybodys help.

    Matt

    • Marked as answer by Matt_Hirst_UK Wednesday, August 21, 2013 3:26 PM
    Wednesday, August 21, 2013 3:25 PM
  • Right the next question that builds on from this is - if I get this to update a field how do I get it to save the data, without the user physically selecting save?

    Idea is when the form loads if the field needs updating, it gets updated and then autosaves.  If no change required it does nothing.

    Matt

    Friday, September 6, 2013 9:33 AM
  • Turns out it was simple really

    Xrm.Page.data.entity.save();

    Matt

    Friday, September 6, 2013 2:31 PM