function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NishaCNishaC 

Selected Date from the datepicker to the apex controller

i have used this datepicker. wanted to show my selected date in the apex controller

 

<td> Date<br></br><input id="t" name="datee" onfocus="DatePicker.pickDate(false,

't', false);" size="12" tabindex="28" type="text" /></td>

 

 I am not able to pass the selected date to the apex controller.. could you help me on this :)

AmitSahuAmitSahu
write the code to pass the value when onclick event is captured. Take a geter setter variable in controller and pass the onclick value to it.
NishaCNishaC

thanks for ur reply,i get the result

i have changed date to string then i get the result 

but now i again want to convert string to date field for comparison

any idea?

AmitSahuAmitSahu
Use date conversion methods such as :
Date myDate = date.newinstance(1960, 2, 17);

Where the year, month and date part can be extracted and passed to this method.
NishaCNishaC

i have tried it like this

 

string dateFrom;

Date myDate = date.newinstance(dateFrom);

 

but it gives  error:

Method does not exist or incorrect signature: date.newinstance(String)

AmitSahuAmitSahu
As I said just extract the date parts from the string and pass them :

Example :

String dt = dateFrom.substring(0,2); //may return you date
String Yr = dateFrom.substring(4,8); //may return you year
String mnt = dateFrom.substring(2,4); //may return you month

Date myDate = date.newinstance(Yr,dt,mnt);

NOTE : Above example is just for understanding purpose only.
NishaCNishaC
Invalid date: 27/08/20

String dt = dateFrom.substring(0,2); //may return you date
String Yr = dateFrom.substring(4,8); //may return you year
String mnt = dateFrom.substring(2,4); //may return you month

string stringDate = dt + '' + mnt+ '' + Yr;
Date myDate = date.valueOf(stringDate);

 

AmitSahuAmitSahu

Date myDate = date.valueOf(stringDate); // This may not work

Try

Date myDate = date.newinstance(Yr,dt,mnt); //just suffle these and you should get the proper result

NishaCNishaC

i have used this mehtod and it works

 

String[] myDateOnly = dateFrom.split(' ');
String[] strDate = myDateOnly[0].split('/');
Integer myIntDate = integer.valueOf(strDate[1]);
Integer myIntMonth = integer.valueOf(strDate[0]);
Integer myIntYear = integer.valueOf(strDate[2]);
Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);

 

but there is one problem. i have used date picker which is blank initially

then it is displaying error: List index out of bounds at line Integer myIntDate = integer.valueOf(strDate[1]);

any idea?

AmitSahuAmitSahu
Can you pass me the code of your VF and COntroller ?
NishaCNishaC

i am sending you the code..

VF Page

<apex:page controller="DashPortalController" sidebar="false">
<apex:pageMessages ></apex:pageMessages>
<!-- Google API inclusion -->
<apex:includeScript id="a" value="https://www.google.com/jsapi" />

<apex:sectionHeader title="Google Charts" subtitle="Chart 1"/>

<!-- Google Charts will be drawn in this DIV -->
<apex:form >
<table align="center">
<td><b>Date From:</b>
<input id="t" name="datee" onfocus="DatePicker.pickDate(false,'t', false);"
size="12" tabindex="28" type="text" />

</td><td></td>
<td></td>
<td></td>
<td></td>
<td><b>Date To:</b>
<apex:outputText value="{0,date,dd/MM/yyyy}">
<apex:param value="{!NOW()}" />
</apex:outputText></td>
<td><button onclick="initCharts()">Go</button></td>

</table>
</apex:form>
<div id="chartBlock" style="width: 500px; height: 500px;"/>

<script type="text/javascript">

google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(initCharts);

 
function initCharts() {
var dateFrom= $('#t').val();
DashPortalController.loadCustomerServiceSuccessRate(
dateFrom,function(result, event){
if (event.status && event.result) {

// for each result, apply it to template and append generated markup
// to the results table body.
var visualization = new google.visualization.PieChart(document.getElementById('chartBlock'));
var data = new google.visualization.DataTable();
data.addColumn('string','Success or Failure');
data.addColumn('number','Percentage');

var finalBean= result;
data.addRows([

['Service Success Rate', finalBean.totalServiceColl],
['Service Failure Rate', finalBean.totalCases]]);

} else {
alert(event.message);
}

visualization.draw(data,{title:'Service Success Rate',legend : {position: 'bottom', textStyle: {color: 'blue', fontSize: 10}}, width:window.innerWidth,vAxis:{textStyle:{color:'red', fontSize: 15}}});
}, {escape:true});
}
</script>

</apex:page>

 

 

===============Controller==============

global with sharing class DashPortalController{

@RemoteAction
global static CaseFailBean loadCustomerServiceSuccessRate(String dateFrom){


List<Service__c> sr1 = new List<Service__c>();

String[] myDateOnly = dateFrom.split(' ');
String[] strDate = myDateOnly[0].split('/');
Integer myIntDate = integer.valueOf(strDate[1]);
Integer myIntMonth = integer.valueOf(strDate[0]);
Integer myIntYear = integer.valueOf(strDate[2]);
Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);

Map<Id,CaseFailBean> caseBeanMap=new Map<Id,CaseFailBean>();
Map<Id,Integer> countMap = new Map<Id,Integer>();
List<Case> caseList=[SELECT Id,CaseNumber, Reason,c.Account.Grand_Parent_Account__c,CreatedDate
FROM Case c where reason in ('Bags not delivered','Bin not delivered' ,'Bin not removed','Complaint from customer',
'Complaint from the Council','Customer wants to cancel','Delayed Collection','Missed Collection','Missing Bin')
AND c.account.id !=null AND CaseNumber!=null AND CreatedDate>=:d];

for(Case caseObj: caseList)
{
Id parentAccId = caseObj.Account.Grand_Parent_Account__c;
if(countMap.get(parentAccId)==null)
{
countMap.put(parentAccId,1);
}
else
{
integer cn = countMap.get(parentAccId);
countMap.put(parentAccId,cn+1);
}
}

map<id,Decimal> mapgrandParentsCounts = new map<id,Decimal>();

Decimal ServiceSuccessRate=0;
Decimal ServiceFailureRate=0;
Decimal ServiceSuccess;
Decimal ServiceFailure;
Decimal TotalServColl;

//if(DateFrom>=system.today() && DateTo<=system.today()){
sr1 = [select id, Name, Customer__c,Status__c, s.Customer__r.Grand_Parent_Account__c,
Week_Number__c,Count_of_Pickup_Days__c,Service_Start_Date__c
from Service__c s where Status__c = 'Active'
AND Count_of_Pickup_Days__c!=null AND Customer__c!=null AND Week_Number__c!=null
AND Service_Start_Date__c>=:d];
for(Service__c srObj: sr1){
if(mapgrandParentsCounts.containskey(srObj.Customer__r.Grand_Parent_Account__c) && srObj.Name!=null)
{
Decimal tCount = mapgrandParentsCounts.get(srObj.Customer__r.Grand_Parent_Account__c);
tCount = tCount + (srObj.Count_of_Pickup_Days__c * srObj.Week_Number__c);
mapgrandParentsCounts.put(srObj.Customer__r.Grand_Parent_Account__c,tCount);
}
else
{
mapgrandParentsCounts.put(srObj.Customer__r.Grand_Parent_Account__c,(srObj.Count_of_Pickup_Days__c * srObj.Week_Number__c));
}
}

//To print Results
for(id gpAccId : mapgrandParentsCounts.keyset()) {
system.debug('============mapgrandParentsCounts.get(gpAccId)===='+mapgrandParentsCounts.get(gpAccId));
TotalServColl = mapgrandParentsCounts.get(gpAccId);
system.debug('========countMap.get(gpAccId)======'+countMap.get(gpAccId));
if(mapgrandParentsCounts.get(gpAccId)!=null && countMap.get(gpAccId)!=null){
if(TotalServColl>0){
ServiceFailure = math.abs((countMap.get(gpAccId)/TotalServColl));
ServiceSuccess = math.ABS(1 - ServiceFailure);
}
}
if(ServiceSuccess!=null){
ServiceSuccessRate = ServiceSuccessRate + ServiceSuccess;
}
if(ServiceFailure!=null){
ServiceFailureRate = ServiceFailureRate + ServiceFailure;
}
}

CaseFailBean beanList=new CaseFailBean();

for(id gpAcc : mapgrandParentsCounts.keyset()) {

system.debug('===========ServiceSuccessRate======='+ServiceSuccessRate);
system.debug('===========ServiceFailureRate======='+ServiceFailureRate);
CaseFailBean beanObj=new CaseFailBean(ServiceSuccessRate,ServiceFailureRate);
beanList=beanObj;

}

system.debug('==========beanList=========='+beanList);
return beanList;
}

global class CaseFailBean{
public Decimal totalCases{get;set;}
public Decimal totalServiceColl{get;set;}


public CaseFailBean(Decimal ServiceSuccessRate,Decimal ServiceFailureRate){
this.totalServiceColl=ServiceSuccessRate;
this.totalCases=ServiceFailureRate;
}

public CaseFailBean(){

}
}
}

 

Help me!!!

NishaCNishaC

i Have tried with this method but getting error: invalid Integer

 

String[] myDateOnly = dateFrom.split(' ');
String[] strDate = myDateOnly[0].split('/');    //date showing as        (01,09,2012)
Integer myIntDate = integer.valueOf(strDate[1]);  //Getting error:invalid integer    showing 1 instead of 01
Integer myIntMonth = integer.valueOf(strDate[0]);   //Showing 9 instead of 09
Integer myIntYear = integer.valueOf(strDate[2]);      //showing 2012
Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);

AmitSahuAmitSahu
invalid inter error is on which line ?
NishaCNishaC

getting error on this line:

Integer myIntDate = integer.valueOf(strDate[0]);

AmitSahuAmitSahu
If your date is showing as (01,09,2012)

Then you should split it on comma not slash

Try split(",")

NishaCNishaC

at starting

it is displaying as

 

string dateFrom = '01/01/2012';     //example because i am using to select date from calendar then that date is shown in controller for comparison

 

String[] myDateOnly = dateFrom.split(' ');  // here result as 01/01/2012 
String[] strDate = myDateOnly[0].split('/');    //date showing as        (01,09,2012)
Integer myIntDate = integer.valueOf(strDate[1]);  //Getting error:invalid integer 
Integer myIntMonth = integer.valueOf(strDate[0]);   //Showing 9 instead of 09
Integer myIntYear = integer.valueOf(strDate[2]);      //showing 2012
Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);

AmitSahuAmitSahu
string dateFrom = '01/01/2012'; // if this is what you are getting
String[] strDate = dateFrom.split('/');
Integer myIntDate = integer.valueOf(strDate[1]); //Getting error:invalid integer
Integer myIntMonth = integer.valueOf(strDate[0]); //Showing 9 instead of 09
Integer myIntYear = integer.valueOf(strDate[2]); //showing 2012
Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);
NishaCNishaC

done with the method which u have told me but same error is coming

 

debugs:

 

======strDate==========(03, 09, 2012)
=======myIntDate==========3
===myIntMonth==========9
====myIntYear==========2012

i really dont know why this error is coming

AmitSahuAmitSahu

Check your private message..

Anil MeghnathiAnil Meghnathi

Nisha

 

is this issue solved??

 

ANil

NishaCNishaC

yes solved