web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Opportunity age calculation code review please?

(0) ShareShare
ReportReport
Posted on by

Iprompt.GIF"/api/data/v9.1/msdyn_richtextfiles(16DDA1DF-A3BD-436E-B1F3-FC8E374069A7)/msdyn_imageblob/$value?size=full">I am using Microsoft Dynamics CRM 2011 on prem.

I am trying to write some code to find the age of a record from created on to sold.

We DO NOT use the out of the box CRM 'close as won' or 'close as lost' fields.....

We use an option set called 'Sales Stage' with multiple options, one of which is the value of 'Sold'...

I am trying to say, when sales stage value is set to sold, calculate the days between Created On Date and the Sold Date (custom date field), and then put the number in a field called 'Pipeline Age' (whole number field). Here's my code :

function ageofrecord_onchange() {
 
var createddate = Xrm.Page.getAttribute("createdon").getValue();
var solddate = Xrm.Page.getAttribute ("new_soldtobankdate").getValue();
var salesstage = Xrm.Page.getAttribute("new_salesstagercd").getValue();

 if (salesstage == 100000008) {
Xrm.Page.getAttribute("new_pipelineage").setValue(solddate - createddate);
}

}

I'm hesitant because I'm trying to take a date field and put the difference in a whole number field. Will this work or am I off here?

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Thanks in advance for the help.

  • Verified answer
    Dinesh Nanayakkara Profile Picture
    on at

    Hi Adam,

    I would personally recommend to use a JavaScript library for your date manipulation. I could recommend Date.js or Moment.js (i prefer Moment.js) for date manipulation. It has many examples so you could never go wrong. Hope this helps.

    Regards,

    Dinesh

  • Community Member Profile Picture
    on at

    I am receiving two errors with the code I am using listed in the original post -

    When selecting a sales stage value (any) from the option set

    3010.error2.GIF

    When I pick a Sold Date,

    3010.error2.GIF

  • Community Member Profile Picture
    on at

    I debugged and I get the date of createdon, the date of sold date (when entered), and sales stage proper value..... It can't be the conditional statement because salesstage does equal 100000008.. SO that leaves the calculations and setvalue... help please :)

  • Community Member Profile Picture
    on at

    I noticed and extra parentheses' behind the number, removed, retesting.

  • Rajkumar Rajaraman Profile Picture
    on at

    Try this:

    var createddate = Xrm.Page.getAttribute("createdon").getValue().getDay();

    var solddate = Xrm.Page.getAttribute ("new_soldtobankdate").getValue().getDay();

    var salesstage = Xrm.Page.getAttribute("new_salesstagercd").getValue();

    var calc = solddate-createddate;

    if (salesstage == 100000008)) {

    Xrm.Page.getAttribute("new_pipelineage").setValue(calc);

    }

  • Community Member Profile Picture
    on at

    not sure what's wrong, here is the code I am down to -

    function ageofrecord_onchange() {

    var createddate = Xrm.Page.getAttribute("createdon").getValue();

    var salesstage = Xrm.Page.getAttribute("new_salesstagercd").getValue();

    var solddate = Xrm.Page.getAttribute ("new_soldtobankdate").getValue();

    if (salesstage == 100000008) {

            alert("score!!");

       }

                  }

    I get the same errors in the screen shot above. stuck, please help.

  • Community Member Profile Picture
    on at

    trying this now Raj thanks

  • Community Member Profile Picture
    on at

    same two errors as listed above unfortunately :(

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans