+ Start a Discussion
IX Salesforce AdminIX Salesforce Admin 

Extract value from date field

Hello everyone,

I am having issues getting value of the date field from the Opportunity object. I need to get Start date and End date, store them in a variable to send them as a part of an XMLH request. I have tried few things:
var startDate  = [SELECT Start_Date__c FROM Opportunity where Id = 'XYZ']; 
var startDate = {!Opportunity.Start_Date__c}; - this is a weird one I get a long number, but it's not the milliseconds, and it starts with 0.00049578....

var c = new sforce.SObject("Opportunity");
var startDate = c.Start_Date__c;

nothing works. I have a feeling I am very close, but can't quite get there. Any help apprechiated. 
 
UC InnovationUC Innovation
Are you coding in apex? I'm assuming yes, in which case you can do this:

Opportunity opp =  [SELECT Start_Date__c FROM Opportunity where Id = 'XYZ']; 
Date startDate = opp.Start_Date__c;

startDate should hold the date value.
IX Salesforce AdminIX Salesforce Admin
Hi Ken,

thank you for your response. I am coding behavoiur of a button, so I'm using OnClick Java script. When I try with your code I'm getting "Unexpected identifyer" error. Error is gone when I comment out Opportunity opp =[SELECT Start_Date__c FROM Opportunity where Id = 'XYZ"];. Alse is there a way to specify This Opportunity? So to pass the value of Start_Date__c button of the current opportunity to a variable?

this is what I have so far:
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
var newRecords = [];

//get opportunity ID
var c = new sforce.SObject("Opportunity"); 
c.id ="{!Opportunity.Id}";
var sfUserID = {!Opportunity.User_ID__c};
Opportunity opp =[SELECT Start_Date__c FROM Opportunity where Id = 'XYZ'];
Date startDate = opp.Start_Date__c;
var getData = new XMLHttpRequest();
var getToken= new XMLHttpRequest();
var getUnsold = new XMLHttpRequest();
var getPrivate = new XMLHttpRequest();
var payload = new Object();
var filter = new Object();
filter['startDate'] = 'startDate';
filter['endDate'] = 'endDate';
payload['userID'] = sfUserID;
payload['aggregation'] = 'month';
payload['filters'] = filter;