1

I have a Visualforce and Apex I would like to share but I think I am doing this wrong. Not wrong code but I think more of a wrong approach.

GOAL: be able to search history of any "RELATED TO: Property" activity history based on Contact and get the date of when they were LAST CALLED. based on Call Result search or Subject "Call" Search. The result has to be only 1 per property.

Visual: (Sorry I have to blackout the names) Activity History

Visualforce:

<apex:page controller="contactActivityHistory" sidebar="true">
<apex:form >
      <apex:pageBlock title="Filter">
          <apex:pageBlockButtons >     
          <apex:commandButton value="Fetch" action="{!fetch}" reRender="pagination"/>
      </apex:pageBlockButtons>
          <apex:pageBlockSection >
          <apex:inputText value="{!contactSearch}" label="Contact"/>
              <apex:inputText value="{!status}" label="Status"/>
              <apex:inputText value="{!subject}" label="Subject"/>
              <apex:inputText value="{!propertySearch}" label="Property"/>
          </apex:pageBlockSection>
      </apex:pageBlock>
      <apex:pageBlock title="Activity History" id="pagination">
      <apex:pageBlockSection >
          <apex:pageBlockTable style="width:1100px" styleClass="tablesorter" value="{!ListTasks}" var="tsk">%
              <apex:column style="width:1000px" headerValue="Contact" value="{!tsk.WhoId}"/>
              <apex:column style="width:250px" headerValue="Subject" value="{!tsk.Subject}" />
              <apex:column style="width:250px" headerValue="Status" value="{!tsk.Status}"/>
              <apex:column style="width:250px" headerValue="Modified Date" value="{!tsk.LastModifiedDate}"/>
              <apex:column style="width:2500px" headerValue="Property" value="{!tsk.WhatID}"/>
              <apex:column style="width:250px" headerValue="Comments" value="{!tsk.Description}"/>
              <apex:column style="width:250px" headerValue="Primary Contact Phone" value="{!tsk.Primary_Contact_Phone_Number__c}"/>
            <apex:column headerValue="Primary Contact Email" value="{!tsk.Primary_Contact_Email_Address__c}"/>
            <apex:column headerValue="Last Call Result" value="{!tsk.Call_Result__c}"/>
            <apex:column headerValue="Last Call Date" value="{!tsk.Last_Call_Date__c}"/>
            <apex:column headerValue="Last Call Agent" value="{!tsk.Last_Call_Agent__c}"/>
          </apex:pageBlockTable>
      </apex:pageBlockSection> 
  </apex:pageBlock>
</apex:form>
</apex:page>

APEX:

public class contactActivityHistory {

    public Boolean fetchCalled{get;set;}
    public String taskList{get;set;}
    public String status{get;set;}
    public String subject{get;set;}
    public String description{get;set;}
    public String propertySearch{get;set;}
    public String contactSearch{get;set;}
    public String soql {get;set;}

    public contactActivityHistory() {
        fetchCalled = false;
    }

    public String currentUser=UserInfo.getUserId();

    public void fetch() {
       // OLD     taskList = 'SELECT WHOID, Subject, Status, LastModifiedDate, WhatID FROM Task WHERE Status LIKE \'%'+status+'%\'';
       soql='SELECT WhoID, Subject, Status, LastModifiedDate, WhatID,      Description, Primary_Contact_Phone_Number__c, Primary_Contact_Email_Address__c, Call_Result__c, Last_Call_Date__c, Last_Call_Agent__c FROM Task WHERE ID !=null';
        if(!status.equals('')) soql+=' and status LIKE \''+String.escapeSingleQuotes(status)+'%\'';
        if(!subject.equals('')) soql+= ' and subject LIKE \''+String.escapeSingleQuotes(subject)+'%\'';

        //search for the property that has the same id as the name that was entered, then search for this id
        if(!propertySearch.equals('')) 
        {
            List<Property__c> propertyQ = Database.query('SELECT id FROM Property__c WHERE ID !=null and name LIKE \''+String.escapeSingleQuotes(propertySearch)+'%\' LIMIT 1'); 
            try {
                String returnedId= propertyQ[0].id; 
                System.debug('#########returnedID'+returnedId);
                soql+= ' and WhatID = \''+returnedId+'\'';
            }
            catch(exception e) {
                soql='SELECT WhoID, Subject, Status, LastModifiedDate, WhatID FROM Task WHERE ID =null';
                //returns no results if the query does not match
            }
        }

        //search for the Contact that has the same id as the name that was entered, then search for this id
        if(!contactSearch.equals('')) 
        {
            List<Contact> contactQ = Database.query('SELECT id FROM Contact WHERE ID !=null and name LIKE \''+String.escapeSingleQuotes(contactSearch)+'%\' LIMIT 1'); 
            try {
                String returnedContactId= contactQ[0].id; 
                System.debug('#########returnedContactID'+returnedContactId);
                soql+= ' and WhoID = \''+returnedContactId+'\'';
            }
            catch(exception e) {
                soql='SELECT WhoID, Subject, Status, LastModifiedDate, WhatID FROM Task WHERE ID =null';
                //returns no results if the query does not match
            }
        }

        //make taskList out of the whole combined soql string we just made
        taskList = soql;
        System.debug('#########taskList'+ taskList);
        List<Task> tskList = Database.query(taskList);
        fetchCalled = true;
    }

    public Task[] getListTasks() {
        List<Contact> cntct = [SELECT Id, Name FROM Contact];
        List<Id> contactIds = new List<Id>();
        for(Contact cn : cntct) {
            contactIds.add(cn.Id);
        }

        if (fetchCalled) {
            fetch();
        }
        else {
            taskList = 'select whoid,subject,status,LastModifiedDate, WhatID, Description, Primary_Contact_Phone_Number__c, Primary_Contact_Email_Address__c, Call_Result__c, Last_Call_Date__c, Last_Call_Agent__c from task where whoid in : contactIds order by LastModifiedDate desc';   
            //taskList = 'SELECT CreatedByID, WhoID, Subject, Status, LastModifiedDate, WhatID FROM Task WHERE CreatedByID = '+String.escapeSingleQuotes(currentUser)+' ';
        }
        List<Task> tskList = Database.query(taskList);
        return tskList;     
    }

    //public Property__c[] propertyStates = Database.query('SELECT id, state       FROM Property__c WHERE ID =:whatID LIMIT 1'); /
    //
}

So as you can see in the visual, I have columns and information of ONE (1) contact but how I need it to be is a list of Properties, one contact per property and the date last called. The date last called is important because if the result "Marketing, letter sent, etc." should not be shown as part of the history. Strictly calls only. Think of it as a Sales Call system where the user can see the when was the last time he called a property and leave actvity on the contact associated with the property. Any ideas on how I should approach this?

0

You can use ORDER BY Clause with your datefield to fetch the latest data from database of activity

Refer to the documentation on how to use ORDER BY in your SOQL

2
  • Yes. but this only shows 1 record. I need a list of ALL unique properties and show the last date they were called. Any thoughts on this?
    – Ninja King
    Dec 17 '15 at 0:12
  • You will need to wrap that in a wrapper class Dec 17 '15 at 4:28

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.