Create Information Message based on Date field in the past.

I have been trying to create a simple on screen message if the expired date of a Warranty is less than the current date by applying the following script into the custom content of a custom machine entity but it doesn't appear to work. Any advice on where I am going wrong would be greatly appreciated.

<script>
crm.ready(function()
{
var warrantyexpired = new Date(mach_warrantyexpireddate);
var dNow = new Date();
if(warrantyexpired
{
crm.infoMessage('Machine's Warranty has Expired');
}
})
</script>

Thanks

  • 0

    Hi,

    I am not sure that:

    var warrantyexpired = new Date(mach_warrantyexpireddate);

    Will do what you want it to. You could get the date that is loaded to the page with:

    $('#_HIDDENmac_warrantyexpireddate').val()

    Just check the element to get its exact name.

    The other issue you have is the format of the date, I think you need to format it, running the following on the console:

    As you can see, it thinks that date is September, not June as it should be. So I would get the date string out, and format it as you need.

    You are also missing the last semicolon from this line:

    crm.infoMessage('Machine's Warranty has Expired');
    }
    })


    The below code should sort it for you, I tested this on 7.3b using the oppo_opened value as a substitute for your custom field.


    function CheckWarranty()
    {
    var dNow = new Date();
    var Opened = $('#_HIDDENoppo_opened').val(); //Add your field here
    var OpenedDate = new Date(Opened);
    var Day = OpenedDate.getDate();
    var Month = OpenedDate.getMonth()+1; //January is 0.
    var Year = OpenedDate.getFullYear();

    var FormatDate = Day+"/"+Month+"/"+Year;
    var warrantyexpired = new Date('"'+FormatDate+'"');
    if(warrantyexpired
    {
    crm.infoMessage("Machines Warranty has Expired");
    }

    }


    crm.ready(function()
    {
    CheckWarranty();
    });

  • 0

    HI Toby

    Firstly thank you kindly for your detailed response. I have attempted to apply the script to the clients test system against the Custom Contents section of the screen and replacing the oppo_opened field with my custom mach_warrantyexpireddate field but nothing happened. I then endeavoured to apply to the opportunity detail screen using your standard code but that had no effect either. Is there something simple I am missing.

  • 0

    Hi Peter,

    No you have not missed anything, I am just an idiot. I will try and correct my error then post the code.

    Thanks

  • 0

    Sorry about that, the issue we have is being British and the way that javascript wants the date format.

    What you are going to have to do (I don't think you can use this blog.stevenlevithan.com/.../date-time-format in this scenario) is then check the user preferences, split them out and build a valid date for comparison.

    This is for users who have the date format dd.mm.yyyy you will have to build in if statements to set the variables for each scenario:

    function CheckWarranty()

    {

    var DateFormat = CurrentUser.userpref_dateformat;

    var dNow = new Date();

    if(DateFormat == 'dd.mm.yyyy')

    {

    var Opened = $('#_HIDDENoppo_opened').val();

    var SplitFirst = Opened.split('.');

    var day = SplitFirst[0];

    var month = SplitFirst[1];

    var yeartime = SplitFirst[2];

    var ValidDate = month+"/"+day+"/"+yeartime

    var warrantyexpired = new Date(ValidDate);

    }

    if(warrantyexpired

    {

    crm.errorMessage("Machines Warranty has Expired");

    }

    }

    crm.ready(function()

    {

    CheckWarranty();

    });

  • 0

    Hi Toby

    I appreciate all your help on this topic. I have applied the following script to the custom content field but again no message. Here is the script I have applied with all users only 12 have their date formatted in the same way dd/mm/yyyy. so edited script to if(DateFormat == 'dd/mm/yyyy')


    function CheckWarranty()
    {
    var DateFormat = CurrentUser.userpref_dateformat;
    var dNow = new Date();
    if(DateFormat == 'dd/mm/yyyy')
    {
    var Opened = $('#_HIDDENmach_warrantyexpireddate').val();
    var SplitFirst = Opened.split('.');
    var day = SplitFirst[0];
    var month = SplitFirst[1];
    var yeartime = SplitFirst[2];
    var ValidDate = month+"/"+day+"/"+yeartime
    var warrantyexpired = new Date(ValidDate);
    }
    if(warrantyexpired
    {
    crm.infoMessage("Machines Warranty has Expired");
    }
    }


    crm.ready(function()
    {
    CheckWarranty();
    });



  • 0

    You need to change this:

    var SplitFirst = Opened.split('.');

    You are splitting on a period and specifying slashes.

    PS - in your screen shot, it would not trigger, the date is in 2016 for the warranty expiry.

    PPS - I would build in a proper set of if statements, just in case a user does not have the date format.

    PPPS - Use the consoles to trouble shoot your code, then enter in the variables and call them one at the time, here, you can see until you amend the split, it won't split, and that causes an invalid date:

    Here it is working....

  • 0

    As an alternative, to avoid the javascript dates, amend vSummary[Entity] so it has the following in it (I have used the opportunity again, so sub it out as applicable):

    CASE WHEN oppo_opened 'Y' ELSE 'N' END AS oppo_Expired


    This just creates a field to tell you if the date in your field is less than GetDate(), if it is then write a 'Y' else an 'N'. Then on the create script of your field, you can add:

    var ID = CRM.GetContextInfo("opportunity","oppo_opportunityId");
    var OpRec = CRM.FindRecord("Opportunity,vSummaryOpportunity","oppo_opportunityId = "+ID);
    var Exp = OpRec.oppo_Expired;
    if(Exp=='Y')
    {
    errorstr = "Expired";
    }


    Not as nice as you cannot get at the client side api in create script, as it is not client side, but you can show an error string with it in instead.