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
Padmini S 26Padmini S 26 

Date Comparision in Apex Controller

Hi All,

I have implemented one Visualforce Page which display all the user with certain below condition.

User event startdate should not match with WorkOrder startdate. I am able to get list of user but not depend on criteria.

I wrote  query for getting the user list :  select Name, firstname,lastname, Skill__C, Technician_Location__c,Technician_Location__Latitude__s,Technician_Location__Longitude__s from User where Skill__C = :wo.Problem__c 

I have written the query to get the all the task : select Id, Subject, isAllDayEvent, StartDateTime, EndDateTime from Event

I have retrieved the current record workorder by using the below query :SELECT Skill_Comparision__c ,Problem__c, Distance__c,Contact.Customer_Location__c,Contact.Customer_Location__Latitude__s,Contact.Customer_Location__Longitude__s,StartDate,EndDate FROM WorkOrder WHERE ID =: controller.getId()

Now I am not getting how to compare the WorkOrder startdate to Event Startdate. Please help on this issue.

Thanks in Advance.

Nayana KNayana K
This may help :
List<Event> lstEvent = new List<Event>();
List<WorkOrder> lstWorkOrder = new List<WorkOrder>([SELECT Skill_Comparision__c ,Problem__c,                                                        Distance__c,Contact.Customer_Location__c,Contact.Customer_Location__Latitude__s,
                                FROM WorkOrder 
                                WHERE ID =: controller.getId()]);

for(Event objE : [SELECT Id, Subject, isAllDayEvent, StartDateTime, EndDateTime,
                        OwnerId,  Owner.Name, Owner.firstname, Owner.lastname, Owner.Skill__C, Owner.Technician_Location__c, Owner.Technician_Location__Latitude__s, Owner.Technician_Location__Longitude__s 
                FROM Event
                WHERE StartDateTime != :lstWorkOrder[0].StartDate
                AND Owner.Skill__c = : lstWorkOrder[0].Problem__c])
    // collect all events
Padmini S 26Padmini S 26
Hi Nayana K,

Thank you for your reply. 

I have tried with your code. But it is throwing error as No Such Column Skill__c.  For all User object custom fields it is throwing error. I am not getting why user object custom fields are throwing error. 
Nayana KNayana K
Please check in your org whether that field is actually exists or may be API name is not proper
Padmini S 26Padmini S 26
Hi Nayana K,

The Skill__c field is exists in my org. API name also Correct. Please Check below Visualforce Page.

VF Page:
<apex:page standardController="WorkOrder" extensions="userassigncontroller" >
    <apex:form >
        <apex:pageBlock > 
            <apex:pageblockTable value="{!techWrapperList}" var="t">  
                    <apex:column headervalue="Select">
                      <input type="radio" name="sel"  />
                                   <apex:actionsupport action="{!process}" event="onchange" rerender="abc">
                                 <apex:param name="select"  value="{!}"  assignTo="{!str}"/>
                  <apex:column headervalue="Technician Name">
                        <apex:outputText value="{!}" />
                    <apex:column headervalue="Distance">
                        <apex:outputText value="{!t.distance} KM" />
                    <apex:column headervalue="Distance">
                        <apex:outputText value="{!}" />
         <apex:commandButton value="Save" action="{!SaveOwner}" disabled="{!flag}" id="abc"/>

public class userassigncontroller {
    public WorkOrder wo;
    Public Boolean flag {get;set;}
    Public string str {get;set;}
    public List<techWrapper> techWrapperList{get;set;}
    public List<user> techList=new list<user>();
    public List<Event> techactivitylist = new list<Event>();
    public userassigncontroller(ApexPages.StandardController controller) {
        techWrapperList=new List<techWrapper>();
        wo = [SELECT Skill_Comparision__c ,Problem__c, Distance__c,Contact.Customer_Location__c,Contact.Customer_Location__Latitude__s,
                            Contact.Customer_Location__Longitude__s,StartDate,EndDate FROM WorkOrder WHERE ID =: controller.getId()  ];
       techList= [select Name, firstname,lastname, Skill__C, Technician_Location__c,Technician_Location__Latitude__s,Technician_Location__Longitude__s
                from User where Skill__C = :wo.Problem__c  ];                     
      techactivitylist = [select Id, Subject, isAllDayEvent, StartDateTime, EndDateTime from Event where StartDateTime != :wo.StartDate  ];
       for(user u : techList){
        Double totalDistance =calculateHaversineDistance(wo.Contact.Customer_Location__Latitude__s,wo.Contact.Customer_Location__Longitude__s,u.Technician_Location__Latitude__s,u.Technician_Location__Longitude__s);
            techWrapperList.add(new techWrapper(u,totalDistance));
     public Decimal calculateHaversineDistance(Decimal lat1, Decimal lon1, Decimal lat2, Decimal lon2){
        // Earth's radius varies from 6356.752 km at the poles to 6378.137 km at the equator
        Double radius = 6371.00;
        Double dLat = toRadians(lat2-lat1);
        Double dLon = toRadians(lon2-lon1);
        Double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
            Math.sin(dLon/2) * Math.sin(dLon/2);
        Double c = 2 * Math.asin(Math.sqrt(a));
        //double kmToMiles = 0.621371;
    //return radius * c * kmToMiles;
    return radius*c;
   private Double toRadians(Decimal degree){
        return degree * 3.1415926 / 180;
     public class techWrapper{       
        public User tech{get; set;}
        public Decimal distance{get;set;} 
        public Boolean Datecomparision {get;set;}
       public techWrapper(User u, Decimal d  ){ 
            tech= u;
    public PageReference process()
       flag = false;
          return null;
       public PageReference SaveOwner()
          flag =true;
         if(str != null)
         WorkOrder tUser = new WorkOrder(id = wo.ID , Ownerid = str); 
             update tUser; 
             PageReference page = new PageReference('/' + wo.ID);
             return page; 
      return null;

Nayana KNayana K
Code seems fine. I hope Skill field is not from managed package.
Padmini S 26Padmini S 26
No.Skill field is not from Managed Package. Skill is a just custom field on User Object.