locked
Quick javascrip question RRS feed

  • Question

  • I want to set the initial value for an attribute to be a certain integer but just when a new object is created.  What code do I need to indicate just on creation?
    Wednesday, July 15, 2009 1:19 PM

Answers

  • Drop this code to the OnLoad event of your form. I've cleaned up the old code.

    var CRM_FORM_TYPE_CREATE = 1;
    var today = new Date();
    var month = today.getMonth() + 2 ;
    var dayNum = 0;

    switch (crmForm.FormType)
    {
      case CRM_FORM_TYPE_CREATE:
          crmForm.all.closeprobability.DataValue = 25;
          crmForm.all.CFCEst_Service_Revenue.DataValue=0;
          crmForm.all.CFCEstimated_Product_Rev.DataValue=0;
          //This should return the 30th of Next month. 
          var dayNum = daysInMonth(month, today.getFullYear()); 
          oDate = new Date( today.getFullYear() +'/'+  month + '/' + dayNum  ); 
          crmForm.all.estimatedclosedate.DataValue = oDate;
          break;
    }

    function daysInMonth(iMonth, iYear)
     {
         return 32 - new Date(iYear, iMonth, 32).getDate();
     }


    Hassan Hussain | http://hassanhussain.wordpress.com/
    • Proposed as answer by Hassan Hussain Wednesday, July 15, 2009 5:24 PM
    • Marked as answer by Phil_Banta Wednesday, July 15, 2009 5:46 PM
    • Edited by Hassan Hussain Thursday, July 16, 2009 12:25 PM Replaced 30 with dayNum
    Wednesday, July 15, 2009 4:09 PM

All replies

  • You need to place the code on the OnLoad event of the form. This code will only set the default value for the attribute if the form is of type "Create".

    var CRM_FORM_TYPE_CREATE = 1;

    switch (crmForm.FormType)
    {
      case CRM_FORM_TYPE_CREATE:
          crmForm.all.<AttributeName>.DataValue = <Default number>;
          break;
    }

    Hope this helps.

    Hassan.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    • Proposed as answer by Hassan Hussain Wednesday, July 15, 2009 1:32 PM
    Wednesday, July 15, 2009 1:32 PM
  • thanks Hassan.  You seem to be very knowledgeable on the script level so let me ask you another question.

    We have a close date value for leads/oppurtunities.  We would like to set the default value to be 30 days from creation, is there a way to do this through the onLoad module or must it be done through workflow?

    Thanks,
    Phil
    Wednesday, July 15, 2009 1:44 PM
  • Your welcome.

    That should be fairly easy to do in JavaScript. Just add 30 days to your current date and set it to the close date field.

    Do you need the JavaScript for that?

    H.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, July 15, 2009 1:51 PM
  • Just how to get the current date?  I can add 30 days to that because it is just an int value is it not?

    What confuses me is if i can use getTime() getMonth() methods in CRM javascript?  It's more of the syntax of how each command is read in CRM that seems alien and different to me.
    Wednesday, July 15, 2009 1:54 PM
  • Great. Use the standard JavaScript function to get current date.

    var today = new Date();
    alert(today.getDate());


    H.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, July 15, 2009 1:58 PM
  • Just spoke with my boss.  He wants it a little more defined now.  Say I open an oppurtunity today, the close date must be the 30th of next month.  Looking at the javascript methods there is a getmonth and getdate.

    The getmonth will return 1-11 which I can add 1 to in order to indicate the next month.

    the getdate method will return the day of the month it is.  Is there a way to just set this value to the end of the next month?


    I wrote the explanations out to make sure i understood the methods correctly, not thinking you needed them :)

    Phil
    Wednesday, July 15, 2009 2:09 PM
  • Months start from 0 so you'll need to add 2 to get the next month.

    Please have a look at the code below:

    //This should return the 30th of Next month. 
    var today = new Date();
    var month = today.getMonth() + 2 ;
    alert('30' + '/' + month + '/' + today.getFullYear()  );

    You'll need to add a check on the month before you set the close date to 30th. ( 30th Feb ?).

    Hope this helps.

    Hassan.


    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, July 15, 2009 2:26 PM
  • by check you mean to see if the month has 30 days or not?
    Wednesday, July 15, 2009 2:30 PM
  • Yes. You'll need a function to check the number of days for feb.


    function daysInFebruary (year){
        // February has 29 days in any year evenly divisible by four,
        // Except centurial years which are not also divisible by 400.
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
    }

    H.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, July 15, 2009 2:39 PM
  • here is what i have now:

    The function utilizes the ability to overflow the days of the month.


    var today = new Date();
    var month = today.getMonth()
    var dayNum = 0;

    dayNum = daysInMonth(month, today.getFullyear())
    alert(dayNum + '/' + month + 2 + '/' + today.getFullYear() );

    //Determine the number of days in the month using overflow.  If overflow by 4 then 28 days etc.
    function daysInMonth(iMonth, iYear)
     {
         return 32 - new Date(iYear, iMonth, 32).getDate();
     }
    Wednesday, July 15, 2009 2:43 PM
  • Works perfectly. 

    H.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, July 15, 2009 2:47 PM
  • last and final quesiton, what is the difference between alert and crmForm.all.<>.DataValue to set the data value of a field.  I thought alert was just used for alert windows and warnings?
    Wednesday, July 15, 2009 2:53 PM
  • That is correct.

    >>I thought alert was just used for alert windows and warnings? YES.
    >>crmForm.all.<>.DataValue to set the data value . YES.

    If you haven't already I recommend you download the ms crm sdk. The help file contains lots of sample javascript code and ways to access the ms crm form fields to read and set value.

    Link to download SDK.

    H.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, July 15, 2009 2:58 PM
  • is there a specific way to set the value for a date field?

    here is my current code running and I receieve an error
    "
    there was an error with this fields customized event
    Field: Window
    Event: onLoad
    Error: Object doesn't support hits property or method
    "

    I'm guessing because I am using the DataValue for the date and passing it a string value?

    switch(crmForm.FormType)
    {
      case CRM_FORM_TYPE_CREATE:

    dayNum = daysInMonth(month, today.getFullyear());
    crmForm.all.estimatedclosedate.DataValue = '30' + '/' + month + 2 + '/' + today.getFullYear();

    function daysInMonth(iMonth, iYear)
     {
         return 32 - new Date(iYear, iMonth, 32).getDate();
     }
    }
    Wednesday, July 15, 2009 3:10 PM
  • Hi Phil,

    Please try the code below.


    //This should return the 30th of Next month. 
    var today = new Date();
    var month = today.getMonth() + 2 ;

    var dayNum = daysInMonth(month, today.getFullYear());

    oDate = new Date( today.getFullYear() +'/'+  month + '/' + '30'  ); //You might need to tweek this to get the date right.

    crmForm.all.estimatedclosedate.DataValue = oDate;

    function daysInMonth(iMonth, iYear)
     {
         return 32 - new Date(iYear, iMonth, 32).getDate();
     }

    Hassan.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, July 15, 2009 3:44 PM
  • error on page

    Microsoft Dynamics CRM Error Report Contents

    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
    <Message>Expected &#39;&#125;&#39;</Message>
    <Line>67</Line>
    <URL>/GothamTechnologyGroup&#47;_forms&#47;FormScript.js.aspx&#63;etc&#61;3&#38;FormType&#61;main&#38;FormName&#61;crmForm</URL>
    <PageURL>/GothamTechnologyGroup&#47;sfa&#47;opps&#47;edit.aspx</PageURL>
    <Function></Function>
    <CallStack>
    </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
    <BrowserUserAgent>Mozilla&#47;4.0 &#40;compatible&#59; MSIE 7.0&#59; Windows NT 6.0&#59; Trident&#47;4.0&#59; SLCC1&#59; .NET CLR 2.0.50727&#59; .NET CLR 3.5.30729&#59; .NET CLR 3.0.30618&#59; InfoPath.2&#59; MS-RTC EA 2&#41;</BrowserUserAgent>
    <BrowserLanguage>en-us</BrowserLanguage>
    <SystemLanguage>en-us</SystemLanguage>
    <UserLanguage>en-us</UserLanguage>
    <ScreenResolution>1400x1050</ScreenResolution>
    <ClientName>Web</ClientName>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>1033</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>1033</UserCulture>
        <OrgID>&#123;DC31D7A3-EA60-4FB5-93F8-2CFCC5A13B2D&#125;</OrgID>
        <UserID>&#123;1D3F20C8-CFAF-DC11-95FF-0015C5EE60CA&#125;</UserID>
        <CRMVersion>4.0.7333.3</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>

    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
    <Message>Object expected</Message>
    <Line>261</Line>
    <URL>/GothamTechnologyGroup&#47;sfa&#47;opps&#47;edit.aspx</URL>
    <PageURL>/GothamTechnologyGroup&#47;sfa&#47;opps&#47;edit.aspx&#35;</PageURL>
    <Function>window.onload&#40;&#41;</Function>
    <CallStack>
     <Function>window.onload&#40;&#41;</Function>
    </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
    <BrowserUserAgent>Mozilla&#47;4.0 &#40;compatible&#59; MSIE 7.0&#59; Windows NT 6.0&#59; Trident&#47;4.0&#59; SLCC1&#59; .NET CLR 2.0.50727&#59; .NET CLR 3.5.30729&#59; .NET CLR 3.0.30618&#59; InfoPath.2&#59; MS-RTC EA 2&#41;</BrowserUserAgent>
    <BrowserLanguage>en-us</BrowserLanguage>
    <SystemLanguage>en-us</SystemLanguage>
    <UserLanguage>en-us</UserLanguage>
    <ScreenResolution>1400x1050</ScreenResolution>
    <ClientName>Web</ClientName>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>1033</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>1033</UserCulture>
        <OrgID>&#123;DC31D7A3-EA60-4FB5-93F8-2CFCC5A13B2D&#125;</OrgID>
        <UserID>&#123;1D3F20C8-CFAF-DC11-95FF-0015C5EE60CA&#125;</UserID>
        <CRMVersion>4.0.7333.3</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>


    is the log, basically from one of the first lines it is expect a }?  I added one at the bottom did nothing.  There is more than one on load script here so here is the complete code running with onLoad

    (other code was working before this one was added)

    var CRM_FORM_TYPE_CREATE = 1;
    var today = new Date();
    var month = today.getMonth() + 2 ;
    var dayNum = 0;


    if(crmForm.FormType==1)
    {
      crmForm.all.CFCEst_Service_Revenue.DataValue=0;
      crmForm.all.CFCEstimated_Product_Rev.DataValue=0;
    }

    switch (crmForm.FormType)
    {
      case CRM_FORM_TYPE_CREATE:
          crmForm.all.closeprobability.DataValue = 25;
          break;
    }

    switch(crmForm.FormType)
    {
    //This should return the 30th of Next month. 
    oDate = new Date(today.getFullYear() +'/'+  month + '/' + '30' ); //You might need to tweek this to get the date right.

    crmForm.all.estimatedclosedate.DataValue = oDate;

    }

    without the extra } on the end
    Wednesday, July 15, 2009 3:56 PM
  • Drop this code to the OnLoad event of your form. I've cleaned up the old code.

    var CRM_FORM_TYPE_CREATE = 1;
    var today = new Date();
    var month = today.getMonth() + 2 ;
    var dayNum = 0;

    switch (crmForm.FormType)
    {
      case CRM_FORM_TYPE_CREATE:
          crmForm.all.closeprobability.DataValue = 25;
          crmForm.all.CFCEst_Service_Revenue.DataValue=0;
          crmForm.all.CFCEstimated_Product_Rev.DataValue=0;
          //This should return the 30th of Next month. 
          var dayNum = daysInMonth(month, today.getFullYear()); 
          oDate = new Date( today.getFullYear() +'/'+  month + '/' + dayNum  ); 
          crmForm.all.estimatedclosedate.DataValue = oDate;
          break;
    }

    function daysInMonth(iMonth, iYear)
     {
         return 32 - new Date(iYear, iMonth, 32).getDate();
     }


    Hassan Hussain | http://hassanhussain.wordpress.com/
    • Proposed as answer by Hassan Hussain Wednesday, July 15, 2009 5:24 PM
    • Marked as answer by Phil_Banta Wednesday, July 15, 2009 5:46 PM
    • Edited by Hassan Hussain Thursday, July 16, 2009 12:25 PM Replaced 30 with dayNum
    Wednesday, July 15, 2009 4:09 PM
  • works man..thanks!!! I'm sure i'll be back!
    Wednesday, July 15, 2009 5:46 PM
  • Not a problem. Glad I could help.

    H.

    Hassan Hussain | http://hassanhussain.wordpress.com/
    Wednesday, July 15, 2009 8:26 PM