+ Start a Discussion
Amack DawgAmack Dawg 

How to subtract Field Date Values in On-Click Javascript button

Scenario: Users need to be able to insert n-number of child records to the selected Parent record where n-number is determined by the integer difference of a Ceiling on two Date fields in the record.

Challenge: How can I retrieve an integer value by subtracting the record's 2 date values in Javascript? I am not even sure the value being returned is a date value (is it a string value)?

Specific code string: 
var weekCount = Math.ceil( (crtvtracker.Creative_End_Date__c - crtvtracker.Creative_Start_Date__c) );

Code block:
{!REQUIRESCRIPT("/soap/ajax/38.0/connection.js")}

//Retrieve and store the selected accounts
var CrtvTrackerIds = {!GETRECORDIDS( $ObjectType.Creative_ID__c )};

//Do a deeper query selecting required Parent fields
var CrtvTrackers = sforce.connection.retrieve("Id,Name,Creative_Start_Date__c,Creative_End_Date__c","Creative_ID__c",CrtvTrackerIds);

//Creating an array to store insertedCrtvScheds
var insertedCrtvScheds = [];

//... skipping some code checking here

CrtvTrackers.forEach(function(crtvtracker){        

        alert(crtvtracker.Creative_End_Date__c);
        
        var CreativeEndDay = crtvtracker.Creative_End_Date__c.day();
        
        alert(crtvtracker.Creative_End_Date__c.day());

        var weekCount = Math.ceil( (crtvtracker.Creative_End_Date__c - crtvtracker.Creative_Start_Date__c) );
        alert(weekCount);

        var schedToInsert = new sforce.SObject("Creative_Schedule__c");
        alert( crtvtracker.Creative_Start_Date__c.daysBetween(crtvtracker.Creative_End_Date__c) );

        for (weeknum = 0; weeknum < 0 ; weeknum++ ) {
        schedToInsert.Creative_ID__c = crtvtracker.Id;             
        schedToInsert.Traffic_Status_updated__c = "On";
        schedToInsert.Creative_Start_Date_updater__c = crtvtracker.Creative_Start_Date__c + (weeknum * 7);
        
        insertedCrtvScheds.push(schedToInsert);
        }
        //alert(crtvtracker.Creative_Start_Date__c);

        //Pass in the 'Creative Start Date' of the Creative ID    
        //schedToInsert.Creative_Start_Date_updater__c = crtvtracker.Creative_Start_Date__c;
        
        
    });

How can I converte my Creative End Date and Creative Start Date fields to be date fields to achieve the mathematical operation I need?

 
Amack DawgAmack Dawg
Since I can't edit my post, here's the error dialog message I keep receiving:
 
A problem with the OnClick JavaScript for this button or link was encountered:

crtvtracker.Creative_End_Date__c.day is not a function

It's specifically referencing this line:
alert(crtvtracker.Creative_End_Date__c.day());

But I have no idea how Salesforce's Date methods work in the Javascript button environment.

How can I convert a record's Date field values into something that I can perform math operations on?

 
Abhilash Mishra 13Abhilash Mishra 13
i guess you need to use javascript date methods for this,
yogesh_sharmayogesh_sharma

Hello Amack,

Try this one:

var day = crtvtracker.Creative_End_Date__c.getDate().toString();
Alert(day);


I hope it helps you.

Thanks,

Yogesh Sharma