0

Desired output: Loop through selected custom sObjects and insert child objects determined by a CELING value of the difference between the parent record's two date fields.

Problem: The Button compiler errors out at this line:

var weekCount = {!CEILING( (crtvtracker.Creative_Start_Date__c - crtvtracker.Creative_Start_Date__c)/7) };

Error returned:

Field crtvtracker.Creative_End_Date__c does not exist. Check spelling.

Code:

    //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);

    ...
    //skipping some data checking code

    //Loops thru each selected Creative ID and identifies each as a row
    CrtvTrackers.forEach(function(crtvtracker){
        var weekCount = {!CEILING( (crtvtracker.Creative_Start_Date__c - crtvtracker.Creative_Start_Date__c)/7) };

        //The alert returns the value just fine!!
        alert(crtvtracker.Creative_Start_Date__c)

        var schedToInsert = new sforce.SObject("Creative_Schedule__c");

        for (weeknum = 0; weeknum < 1 ; 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);
        }
});

The Creative_Start_Date__c and Creative_End_Date__c are fields on the parent record that is being looped through-- why is the Salesforce compiler not recognizing them?

Per my comments, the alert(crtvtracker.Creative_Start_Date__c) does recognize the Creative_Start_Date__c (and End_Date) field and returns the correct value for it.

  • 2
    They're javascript variables, so they won't work in merge fields. You need to manipulate them via javascript to get the correct value. You might be able to get the greater value from two Date instances, but I can't check currently. – Adrian Larson Jan 4 '17 at 0:05
  • @AdrianLarson thanks! Any pointers on how to manipulate the fields-- it sounds like I need to wrangle some kind of Javascript Ceiling function against the looping record's field values, correct? – GPP Jan 4 '17 at 1:06
  • @AdrianLarson I think I have Math.ceil JS function figured out, do you know how the Date fields in Salesforce are interpreted by the JS-- do I need to do some kind of Date parse to those fields to get them read as Date values? – GPP Jan 4 '17 at 2:03
2

They're javascript variables, so they won't work in merge fields. You need to manipulate them via javascript to get the correct value. The first step is getting a Date instance.

var startDate = new Date(crtvtracker.Creative_Start_Date__c);

There are a lot of niceties of date parsing in Javascript. While the above may work, it is probably unreliable and I just put it first because I find it the easiest to grok. You need to take the string and construct a Date. Have a read of this post, for example. You'll need to modify their code to strip out the time portion, but that's easy.

var parseISOLocal = function (s) {
        var b = s.split(/\D/);
        return new Date(b[0], b[1]-1, b[2]);
    },
    startDate = parseISOLocal(crtvtracker.Creative_Start_Date__c);

In regards to your other question (which I still think you should just close), you can also look at this post for how to add days to that date. I haven't tested their solution extensively, but it seems simple enough to integrate into the above.

This material should be enough to get you started down the right path. Most of the issues you are likely to face manipulating date values in Javascript (should there be further issues) are likely to be off topic here, and much more appropriate on Stack Overflow. In fact, the same can be said of this question (purely Javascript issue), but like I said, I'm just trying to help you get started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.