0

I'm trying to populate the last day of the year on the CloseDate (Date) field.

I've tried everything from this link:

https://stackoverflow.com/questions/13255114/retrieve-the-last-day-of-the-year-in-javascript/13255155

Salesforce did not seem to recognize any of these syntaxes. So this is what i ended up with:

        //get year
        var currentYear = $A.localizationService.formatDate(new Date(), "YYYY");
        //set the date i want
        var date = currentYear + "-12-31";
        //put the date in correct format
        var endDate = $A.localizationService.formatDate(date, "yyyy MM dd");
        var createRecordEvent = $A.get("e.force:createRecord");
        
        //change this when deploying
        var RecTypeID  = '0126C0000005BT9QAM';
        createRecordEvent.setParams({
            "entityApiName": 'Opportunity',
            "recordTypeId": RecTypeID,
            'defaultFieldValues': {
                'Type' : 'Building',
                'Budget_Status__c' : 'Pending',
                'CloseDate' : endDate
            }  

I get the date field to populate and i don't get any formatting errors but i do get the error: Complete this field, when trying to save the record. Does anyone have any information on how to achieve this or is this not possible?

(Yes i know, its frowned upon to set Static Id's, I cheated)

1 Answer 1

0

I'm not exactly sure why, but if you try to just set the currentYear variable in the Default Field:Close Date, Salesforce will not accept it. But if you create a date attribute in the component, store the exact same value in this attribute, then call the attribute in the Close Date field, Salesforce accepts it.

Component

<aura:attribute name="todaysDate" type="Date"/>

Controller

newRecord : function(component, event, helper) 
    { 
        var currentYear = $A.localizationService.formatDate(new Date(), "YYYY-12-31");
        component.set("v.todaysDate", currentYear);
        var createRecordEvent = $A.get("e.force:createRecord");
        //change this when deploying
        var RecTypeID  = '0126C0000005BT9QAM';
        createRecordEvent.setParams({
            "entityApiName": 'Opportunity',
            "recordTypeId": RecTypeID,
            'defaultFieldValues': {
                'Type' : 'Land',
                'Budget_Status__c' : 'Pending',
                'StageName' : 'Pursuit Identified',
                'CloseDate' : component.get('v.todaysDate')
            }   
            
            
        });
        createRecordEvent.fire();
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .