• Andrew Perez
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
I am getting an error when I try to search for anything using the date fields.

VF:
<apex:page controller="Engagements_Search_Controller" action="{!searchEng}" >
<script type="text/javascript">
    window.onload=function() {
    // document.getElementById("{!$Component.thePb.thepbs.engName}").focus();
    }   
</script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
         <script type="text/javascript">
            function resetForm(ele) {
                 $(ele).closest('form').find("input[type=text], textarea").val("");
                return false;
            }
</script>
 <apex:form id="pageForm">
  <apex:pageBlock id="thePb" title="Engagements Details To Search">
  
   <apex:pageblockSection id="thepbs" columns="3" collapsible="true">
    <apex:inputField value="{!eng.Name}" required="false" id="engName"/>
    <apex:inputfield value="{!eng.Company_Name__c}" id="companyName"/>
    <apex:inputfield value="{!eng.Ultimate_Parent_Company__c}" id="ultCompany"/>
   </apex:pageblockSection> 
   
   <apex:pageblockSection id="pbInfo" >
     <apex:inputfield value="{!eng.Unit_Name__c}" label="Unit" id="unitName"/>
     <apex:inputfield value="{!eng.Country__c}" id="country"/>
     <apex:inputField value="{!eng.Scope_of_BR_Area__c}" id="area" label="Area"/>
     <apex:inputfield value="{!eng.Scope_of_BR_Theme__c}" label="Theme" id="theme"/>
   </apex:pageblockSection> 
   
   <apex:pageblockSection columns="3" collapsible="true" id="pbSIC">
     <apex:inputField value="{!eng.Additional_SIC_Sector__c}" label="Sector" id="sector"/>
     <apex:inputfield value="{!eng.Additional_SIC_Industry__c}" label="Industry" id="industry" />
     
   </apex:pageblockSection>
   
   <apex:pageblockSection id="pbDate">
   <apex:inputfield value="{!eng.SIC_Final__c}" id="sicFinal" />
   <apex:outputLink value="https://www.osha.gov/pls/imis/sicsearch.html?p_sic=&p_search=" style="font-weight:bold;" target="_blank">SIC Lookup</apex:outputLink>

     <apex:inputfield value="{!eng.BR_Start_Date__c}" id="brDate" /><br></br>
     <apex:inputfield value="{!eng.Project_Start_Date__c}" id="projectDate"/>     
   </apex:pageblockSection>
   
   <apex:pageblockButtons location="bottom">
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <apex:commandButton value="Search"  action="{!searchEng}"/>
      <apex:commandButton value="Reset" action="{!reset}"/>
      
    </apex:pageblockButtons> 
    
  </apex:pageBlock>
    <apex:pageblock >
        <div align="center">
         <a href="https://cs25.salesforce.com/search/SearchResults?str=%22Sample+Text*%22&search=Search&sen=a0i&sen=a0i&searchType=2">Exact Keyword(s) Text Search (Note: replace the Sample Text)</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <a href="https://cs25.salesforce.com/search/SearchResults?searchType=2&str=Sample+Text&search=Search&sen=a0i">All Keyword(s) Text Search (Note: replace the Sample Text)</a>
        </div>
   </apex:pageblock> 
  
   <apex:pageBlock title="Engagements Details" id="noRec" rendered="{! IF( engagementList != null && engagementList.size ==0 , true, false)}" >
  <apex:outputPanel >
    <h1>No Records Found </h1>
</apex:outputPanel>
  </apex:pageBlock>

  
  <apex:pageBlock title="Engagements Details (Total List Size: {!total_size})" id="details" rendered="{! IF( engagementList != null && engagementList.size >0, true, false)}" >

   <apex:pageBlockTable value="{!engagementList}" var="a">
   <apex:column headerValue="Engagements Name">
    <apex:outputLink target="_blank" value="/{!a.id}">{!a.Name}</apex:outputLink> 
   </apex:column>   
    <!--  If you want facet style you can add like this.
   <apex:column >
     <apex:facet name="header">Link Name</apex:facet>
     <apex:outputLink target="_blank" value="/{!a.id}">{!a.Name}</apex:outputLink> 
    </apex:column>
    -->
    
    <apex:column value="{!a.Can_t_be_used_External__c}" headerValue="No Name Drop"/>
    <apex:column value="{!a.Pending_Go_No_Go_Canceled__c}" headerValue="Go/No-Go"/>
    <apex:column value="{!a.Additional_SIC_Sector__c}" headerValue="Sector"/>
    <apex:column value="{!a.Additional_SIC_Industry__c}" headerValue="Industry"/>
    <apex:column value="{!a.SIC_Final__c}" headerValue="SIC Final"/>
    <apex:column value="{!a.Unit_Name__c}" headerValue="Unit Name"/> 
    <apex:column value="{!a.Company_Name__c}" headerValue="Company Name"/>
    <apex:column value="{!a.Ultimate_Parent_Company__c}" headerValue="Ultimate Parent Company"/> 
    <apex:column value="{!a.Actual_Benefits__c}" headerValue="Actual Benefits"/> 
    <apex:column value="{!a.BR_Start_Date__c}" headerValue="BR Start"/> 
    <apex:column value="{!a.Project_Start_Date__c}" headerValue="Project Start"/>
   
    
   </apex:pageBlockTable>
   
    <apex:pageblockButtons >
        <apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!prev}"/>
        <apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>
        <apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>
        <apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!nxt}"/>
        <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
        
    </apex:pageblockButtons>
   
  </apex:pageBlock>
 </apex:form>
</apex:page>

Controller:
public with sharing class Engagements_Search_Controller {
public Engagements__c eng{get;set;}
public List<Engagements__c> engagementList {get;set;}

// create a list of strings to hold the conditions
List<string> conditions = new List<string>();
private integer totalRecs = 0;
private integer OffsetSize = 0;
private integer LimitSize= 25;
public integer total_size; //used to show user the total size of the list
public Integer page{get;set;}
public Integer startIdx{get;set;}
public Integer endIdx{get;set;}
public Integer totalPages{get;set;}
 public Integer totalRecords{get;set;}

public Engagements_Search_Controller(){
system.debug('==>Engagements_Search_Controller  is calling==>');
 eng = new Engagements__c();
 //engagementList  = new List<refount>();
}

public void searchEng(){
totalRecs = 0;
OffsetSize = 0;
if(engagementList !=null && engagementList.size()>0){
 engagementList=null;
}
searchEngagementss ();
conditions.clear();
}


public Void searchEngagementss(){

System.debug('Total Records is ==>'+totalRecs);
System.debug('OffsetSize is ==>'+OffsetSize);



if(engagementList != null && !engagementList.isEmpty()){
  engagementList.clear();
}
 String strQuery ='SELECT Name,Pending_Go_No_Go_Canceled__c,Additional_SIC_Sector__c, Additional_SIC_Industry__c,Can_t_be_used_external__c, SIC_Final__c, Unit_Name__c, Company_Name__c, Ultimate_Parent_Company__c, Actual_Benefits__c, Country__c, BR_Start_Date__c, Project_Start_Date__c,Scope_of_BR_Area__c,Scope_of_BR_Theme__c  From Engagements__c ';
 
 if(eng.Name !=null && eng.Name !=''){
   conditions.add('Name Like \'%' +eng.Name +'%\' ');
 }
 if(eng.Additional_SIC_Sector__c !=null && eng.Additional_SIC_Sector__c !=''){
    conditions.add('Additional_SIC_Sector__c Includes (\'' +eng.Additional_SIC_Sector__c +'\') ');
 }
 if(eng.Additional_SIC_Industry__c !=null && eng.Additional_SIC_Industry__c !=''){
    conditions.add('Additional_SIC_Industry__c Includes (\'' +eng.Additional_SIC_Industry__c +'\') ');
 }
  if(eng.Scope_of_BR_Area__c !=null && eng.Scope_of_BR_Area__c !=''){
    conditions.add('Scope_of_BR_Area__c Includes (\'' +eng.Scope_of_BR_Area__c +'\') ');
 }
 if(eng.Scope_of_BR_Theme__c !=null && eng.Scope_of_BR_Theme__c !=''){
    conditions.add('Scope_of_BR_Theme__c Includes (\'' +eng.Scope_of_BR_Theme__c +'\') ');
 }
 if(eng.SIC_Final__c !=null && eng.SIC_Final__c !=''){
   conditions.add('SIC_Final__c Like\'%' +eng.SIC_Final__c +'%\' ');
 }

 if(eng.Unit_Name__c !=null && eng.Unit_Name__c !=''){
   conditions.add('Unit_Name__c Like\'%' +eng.Unit_Name__c +'%\' ');
 }


if(eng.Company_Name__c !=null && eng.Company_Name__c !=''){
   conditions.add('Company_Name__c Like\'%' +eng.Company_Name__c +'%\' ');
 }
 if(eng.Ultimate_Parent_Company__c !=null && eng.Ultimate_Parent_Company__c !=''){
   conditions.add('Ultimate_Parent_Company__c Like\'%' +eng.Ultimate_Parent_Company__c +'%\' ');
 }
 if(eng.Country__c !=null && eng.Country__c !=''){
       conditions.add('Country__c Like\'%' +eng.Country__c +'%\' ');
 }

 if(eng.BR_Start_Date__c !=null){
    conditions.add('BR_Start_Date__c >= (\'' +eng.BR_Start_Date__c +'\') ');
  }
    
 if(eng.Project_Start_Date__c !=null){
    conditions.add('Project_Start_Date__c >= (\'' +eng.Project_Start_Date__c +'\') ');
 }
  if(eng.Can_t_be_used_external__c !=null && eng.Can_t_be_used_external__c != FALSE){
   conditions.add('Can_t_be_used_external__c == (\'' +eng.Can_t_be_used_external__c +'\') ');
 }
 
 

  if (conditions.size() > 0) {
   strQuery += '  WHERE ' + conditions[0];
   for (Integer i = 1; i < conditions.size(); i++)
            strQuery += '  AND ' + conditions[i];
  }
 if(totalRecs !=null && totalRecs ==0){
    List<Engagements__c> engTemp = Database.query(strQuery);
    totalRecs = (engTemp !=null &&engTemp.size()>0)?engTemp.size():0;
 }

 system.debug('strQuery ==>'+strQuery );
 // add sort and limits at the end  
  strQuery += ' ORDER BY Name  ASC, Unit_Name__c DESC LIMIT :LimitSize OFFSET :OffsetSize';
  
  engagementList  =Database.query(strQuery);
  
   

  //conditions.clear();
  //return engagementList.size();
}


public void FirstPage()
{
OffsetSize = 0;
searchEngagementss();
}
public void previous()
{
OffsetSize = (OffsetSize-LimitSize);
searchEngagementss();
}
public void next()
{
OffsetSize = OffsetSize + LimitSize;
searchEngagementss();
}
public void LastPage()
{
OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
searchEngagementss();
}
public boolean getprev()
{

if(OffsetSize == 0){

return true;
}
else {

return false;
}
}
public boolean getnxt()
{
if((OffsetSize + LimitSize) > totalRecs){

return true;
}
else {

return false;
}
}

 public Integer getTotal_size() {
      return totalRecs;
   }

   public Integer getPageNumber() {
      return OffsetSize/LimitSize + 1;
   }

   public Integer getTotalPages() {
      if (math.mod(totalRecs, LimitSize) > 0) {
         return totalRecs/LimitSize + 1;
      } else {
         return (totalRecs/LimitSize);
      }
   }

//reset button
public PageReference reset() {
   PageReference newpage = new PageReference(System.currentPageReference().getURL());    
   newpage.getParameters().clear();
   newpage.setRedirect(true);
   return newpage;
 }

}

 
I'm running into two problems on my search page.  Nothing is happening when I click the 'Reset' button and I am getting this error when I search using a date field: 

System.QueryException: expecting a colon, found '('
Error is in expression '{!searchEng}' in component <apex:commandButton> in page engagement_search: Class.Engagements_Search_Controller.searchEngagementss: line 94, column 1
Class.Engagements_Search_Controller.searchEng: line 26, column 1
Class.Engagements_Search_Controller.searchEngagementss: line 94, column 1
Class.Engagements_Search_Controller.searchEng: line 26, column 1

Here is my code:
public with sharing class Engagements_Search_Controller {
public Engagements__c eng{get;set;}
public List<Engagements__c> engagementList {get;set;}

// create a list of strings to hold the conditions
List<string> conditions = new List<string>();
private integer totalRecs = 0;
private integer OffsetSize = 0;
private integer LimitSize= 25;
public integer total_size; //used to show user the total size of the list
public Integer totalPages{get;set;}


public Engagements_Search_Controller(){
system.debug('==>Engagements_Search_Controller  is calling==>');
 eng = new Engagements__c();
 //engagementList  = new List<refount>();
}

public void searchEng(){
totalRecs = 0;
OffsetSize = 0;
if(engagementList !=null && engagementList.size()>0){
 engagementList=null;
}
searchEngagementss ();
conditions.clear();
}


public Void searchEngagementss(){

System.debug('Total Records is ==>'+totalRecs);
System.debug('OffsetSize is ==>'+OffsetSize);

if(engagementList != null && !engagementList.isEmpty()){
  engagementList.clear();
}
 String strQuery ='SELECT Name,Pending_Go_No_Go_Canceled__c,Additional_SIC_Sector__c, Additional_SIC_Industry__c,What_We_Found__c,What_We_Did__c, SIC_Final__c, Unit_Name__c, Company_Name__c, Ultimate_Parent_Company__c, Country__c, BR_Start_Date__c, Project_Start_Date__c,Scope_of_BR_Area__c,Scope_of_BR_Theme__c  From Engagements__c ';
 
 if(eng.Name !=null && eng.Name !=''){
   conditions.add('Name Like \'%' +eng.Name +'%\' ');
 }
 if(eng.Additional_SIC_Sector__c !=null && eng.Additional_SIC_Sector__c !=''){
    conditions.add('Additional_SIC_Sector__c Includes (\'' +eng.Additional_SIC_Sector__c +'\') ');
 }
 if(eng.Additional_SIC_Industry__c !=null && eng.Additional_SIC_Industry__c !=''){
    conditions.add('Additional_SIC_Industry__c Includes (\'' +eng.Additional_SIC_Industry__c +'\') ');
 }
  if(eng.Scope_of_BR_Area__c !=null && eng.Scope_of_BR_Area__c !=''){
    conditions.add('Scope_of_BR_Area__c Includes (\'' +eng.Scope_of_BR_Area__c +'\') ');
 }
 if(eng.Scope_of_BR_Theme__c !=null && eng.Scope_of_BR_Theme__c !=''){
    conditions.add('Scope_of_BR_Theme__c Includes (\'' +eng.Scope_of_BR_Theme__c +'\') ');
 }
 if(eng.SIC_Final__c !=null && eng.SIC_Final__c !=''){
   conditions.add('SIC_Final__c Like\'%' +eng.SIC_Final__c +'%\' ');
 }

if(eng.Unit_Name__c !=null && eng.Unit_Name__c !=''){
   conditions.add('Unit_Name__c Like\'%' +eng.Unit_Name__c +'%\' ');
 }
if(eng.Company_Name__c !=null && eng.Company_Name__c !=''){
   conditions.add('Company_Name__c Like\'%' +eng.Company_Name__c +'%\' ');
 }
 if(eng.Ultimate_Parent_Company__c !=null && eng.Ultimate_Parent_Company__c !=''){
   conditions.add('Ultimate_Parent_Company__c Like\'%' +eng.Ultimate_Parent_Company__c +'%\' ');
 }
 if(eng.Country__c !=null && eng.Country__c !=''){
   conditions.add('Country__c Like\'%' +eng.Country__c +'%\' ');
 }

 if(eng.BR_Start_Date__c !=null){
    conditions.add('BR_Start_Date__c Like (\'' +eng.BR_Start_Date__c +'\') ');
 }
 if(eng.Project_Start_Date__c !=null){
    conditions.add('Project_Start_Date__c Like (\'' +eng.Project_Start_Date__c +'\') ');
 }
if(eng.What_We_Found__c !=null && eng.What_We_Found__c !=''){
   conditions.add('What_We_Found__c Like\'%' +eng.What_We_Found__c +'%\' ');
 }
 if(eng.What_We_Did__c !=null && eng.What_We_Did__c !=''){
   conditions.add('What_We_Did__c Like\'%' +eng.What_We_Did__c +'%\' ');
 }
 
 

  if (conditions.size() > 0) {
   strQuery += '  WHERE ' + conditions[0];
   for (Integer i = 1; i < conditions.size(); i++)
            strQuery += '  AND ' + conditions[i];
  }
 if(totalRecs !=null && totalRecs ==0){
    List<Engagements__c> engTemp = Database.query(strQuery);
    totalRecs = (engTemp !=null &&engTemp.size()>0)?engTemp.size():0;
 }

 system.debug('strQuery ==>'+strQuery );
 // add sort and limits at the end  
  strQuery += ' ORDER BY Name  ASC, Unit_Name__c DESC LIMIT :LimitSize OFFSET :OffsetSize';
  
  engagementList  =Database.query(strQuery);
  
   

  //conditions.clear();
  //return engagementList.size();
}


public void FirstPage()
{
OffsetSize = 0;
searchEngagementss();
}
public void previous()
{
OffsetSize = (OffsetSize-LimitSize);
searchEngagementss();
}
public void next()
{
OffsetSize = OffsetSize + LimitSize;
searchEngagementss();
}
public void LastPage()
{
OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);
searchEngagementss();
}
public boolean getprev()
{

if(OffsetSize == 0){

return true;
}
else {

return false;
}
}
public boolean getnxt()
{
if((OffsetSize + LimitSize) > totalRecs){

return true;
}
else {

return false;
}
}

 public Integer getTotal_size() {
      return totalRecs;
   }

   public Integer getPageNumber() {
      return OffsetSize/LimitSize + 1;
   }

   public Integer getTotalPages() {
      if (math.mod(totalRecs, LimitSize) > 0) {
         return totalRecs/LimitSize + 1;
      } else {
         return (totalRecs/LimitSize);
      }
   }


}
VF Page:
<apex:page controller="Engagements_Search_Controller" action="{!searchEng}" >
<script type="text/javascript">
    window.onload=function() {
    // document.getElementById("{!$Component.thePb.thepbs.engName}").focus();
    }   
</script>
 <apex:form >
  <apex:pageBlock id="thePb" title="Engagements Details To Search">
   <apex:pageblockSection id="thepbs" columns="3" collapsible="true">
    <apex:inputField value="{!eng.Name}" required="false" id="engName"/>
    <apex:inputfield value="{!eng.Company_Name__c}"/>
    <apex:inputfield value="{!eng.Ultimate_Parent_Company__c}"/>
    <apex:inputfield value="{!eng.Unit_Name__c}"/>
    <apex:inputfield value="{!eng.Country__c}"/>
    <apex:inputfield value="{!eng.SIC_Final__c}"/>
   </apex:pageblockSection>  
   <apex:pageblockSection >
     <apex:inputField value="{!eng.Scope_of_BR_Area__c}"/>
     <apex:inputfield value="{!eng.Scope_of_BR_Theme__c}"/>
     <apex:inputField value="{!eng.Additional_SIC_Sector__c}"/>
     <apex:inputfield value="{!eng.Additional_SIC_Industry__c}"/>
     <apex:inputfield value="{!eng.BR_Start_Date__c}"/>
     <apex:inputfield value="{!eng.Project_Start_Date__c}"/>
     <apex:inputfield value="{!eng.What_We_Found__c}" style="width:85%;"/>
     <apex:inputfield value="{!eng.What_We_Did__c}" style="width:85%;"/>
   </apex:pageblockSection>
   <apex:pageblockButtons location="bottom">
      <apex:commandButton value="Search"  action="{!searchEng}"/>
      <apex:commandButton value="Reset" status="idStatus" onclick="this.form.reset();return false;" />
     </apex:pageblockButtons>  
  </apex:pageBlock>
  
   <apex:pageBlock title="Engagements Details" id="noRec" rendered="{! IF( engagementList != null && engagementList.size ==0 , true, false)}" >
  <apex:outputPanel >
    <h1>No Records Found </h1>
</apex:outputPanel>
  </apex:pageBlock>

  
  <apex:pageBlock title="Engagements Details (Total List Size: {!total_size})" id="details" rendered="{! IF( engagementList != null && engagementList.size >0, true, false)}" >

   <apex:pageBlockTable value="{!engagementList}" var="a">
   <apex:column headerValue="Engagements Name">
    <apex:outputLink target="_blank" value="/{!a.id}">{!a.Name}</apex:outputLink> 
   </apex:column>   
    <!--  If you want facet style you can add like this.
   <apex:column >
     <apex:facet name="header">Link Name</apex:facet>
     <apex:outputLink target="_blank" value="/{!a.id}">{!a.Name}</apex:outputLink> 
    </apex:column>
    -->
    
    <apex:column value="{!a.Pending_Go_No_Go_Canceled__c}" headerValue="Pending/Go/No-Go/Canceled"/>
    <apex:column value="{!a.Additional_SIC_Sector__c}" headerValue="Additional SIC Sector"/>
    <apex:column value="{!a.Additional_SIC_Industry__c}" headerValue="Additional SIC Industry"/>
    <apex:column value="{!a.SIC_Final__c}" headerValue="SIC Final"/>
    <apex:column value="{!a.Unit_Name__c}" headerValue="Unit Name"/> 
    <apex:column value="{!a.Company_Name__c}" headerValue="Company Name"/>
    <apex:column value="{!a.Ultimate_Parent_Company__c}" headerValue="Ultimate Parent Company"/> 
    <apex:column value="{!a.Country__c}" headerValue="Country"/> 
    <apex:column value="{!a.BR_Start_Date__c}" headerValue="BR Start Date"/> 
    <apex:column value="{!a.Project_Start_Date__c}" headerValue="Project Start Date"/>
   
    
   </apex:pageBlockTable>
   
    <apex:pageblockButtons >
        <apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!prev}"/>
        <apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>
        <apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>
        <apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!nxt}"/>
        <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
        
    </apex:pageblockButtons>
   
  </apex:pageBlock>

 </apex:form>
</apex:page>

 
I am trying to have a field (Meeting_Type_RM__c) on Event auto increment based on the number of Events a Contact has and the Record Type (RM).  So the first Event should have a meeting type of RM1, the next RM2 and so on.  The numbering series needs to be unique for each Contact which is why I just can't use the auto number field type.

With the code that I have right now every Event is just showing RM1 as the meeting type instead of increasing by 1 each time. Also, I don't think it is unique for each Contact.  This is what I have right now:

trigger EventAutoNumber on Event (before insert) {
    list<Event> e = new list<Event>
    ([select Meeting_Type_RM__c from Event order by Meeting_Type_RM__c desc limit 1]);
    
   
    String startNumber = '0';
    String Word = 'RM';



    for(Event ev:trigger.new) {

        if(ev.RecordTypeID == '012f0000000D1fG'){
        startNumber = String.valueOf(Integer.valueOf(startNumber) + 1);
        startNumber = '0'.substring(0, 1-startNumber.length()) + startNumber;
        ev.Meeting_Type_RM__c = Word + startNumber;
        }
    }
}
I'm trying to create an event based on a field from a task. I have a workflow in place that will change a task field to Completed once it gets in a certain date range which will then trigger a new event.  I want this new event to copy all the data from the task to the new event.

Here is what I have, doesn't seem to be working:

trigger createEvent on Task (after insert) {   
    List<Event> NewEvent = new List<Event>();
            for (Task newTask: Trigger.New)
               if (newTask.Status == 'Future Completed'){
                NewEvent.add (new Event(
                    OwnerID = newTask.OwnerID,
                    Subject = newTask.Subject,
                    Meeting_Status__c = newTask.Meeting_Status__c,
                    Meeting_Type__c = newTask.Meeting_Type__c,
                    Confirmed_with__c = newTask.Confirmed_with__c,
                    WhatId = newTask.WhatId,
                    WhoId = newTask.WhoId,
                    Priority__c = newTask.Priority__c,
                    Show_Time_As__c = newTask.Show_Time_As__c,
                    Client_Location_Meeting_Date__c = newTask.Client_Location_Meeting_Date__c,
                    Client_Location_Start_Time__c = newTask.Client_Location_Start_Time__c,
                    Client_Location_End_Time__c = newTask.Client_Location_End_Time__c,
                    Location = newTask.Location__c,
                    Directions_to_the_Meeting_Location__c = newTask.Directions_to_the_Meeting_Location__c,
                    Meeting_Confirmed__c = newTask.Meeting_Confirmed__c,
                    Confirmation_Notes__c = newTask.Confirmation_Notes__c,
                    More_Comments__c = newTask.More_Comments__c,
                    Summary__c = newTask.Summary__c,
                    Next_Steps__c = newTask.Next_Steps__c));
                    }
  insert NewEvent;
}
I am trying to have a field (Meeting_Type_RM__c) on Event auto increment based on the number of Events a Contact has and the Record Type (RM).  So the first Event should have a meeting type of RM1, the next RM2 and so on.  The numbering series needs to be unique for each Contact which is why I just can't use the auto number field type.

With the code that I have right now every Event is just showing RM1 as the meeting type instead of increasing by 1 each time. Also, I don't think it is unique for each Contact.  This is what I have right now:

trigger EventAutoNumber on Event (before insert) {
    list<Event> e = new list<Event>
    ([select Meeting_Type_RM__c from Event order by Meeting_Type_RM__c desc limit 1]);
    
   
    String startNumber = '0';
    String Word = 'RM';



    for(Event ev:trigger.new) {

        if(ev.RecordTypeID == '012f0000000D1fG'){
        startNumber = String.valueOf(Integer.valueOf(startNumber) + 1);
        startNumber = '0'.substring(0, 1-startNumber.length()) + startNumber;
        ev.Meeting_Type_RM__c = Word + startNumber;
        }
    }
}
I'm trying to create an event based on a field from a task. I have a workflow in place that will change a task field to Completed once it gets in a certain date range which will then trigger a new event.  I want this new event to copy all the data from the task to the new event.

Here is what I have, doesn't seem to be working:

trigger createEvent on Task (after insert) {   
    List<Event> NewEvent = new List<Event>();
            for (Task newTask: Trigger.New)
               if (newTask.Status == 'Future Completed'){
                NewEvent.add (new Event(
                    OwnerID = newTask.OwnerID,
                    Subject = newTask.Subject,
                    Meeting_Status__c = newTask.Meeting_Status__c,
                    Meeting_Type__c = newTask.Meeting_Type__c,
                    Confirmed_with__c = newTask.Confirmed_with__c,
                    WhatId = newTask.WhatId,
                    WhoId = newTask.WhoId,
                    Priority__c = newTask.Priority__c,
                    Show_Time_As__c = newTask.Show_Time_As__c,
                    Client_Location_Meeting_Date__c = newTask.Client_Location_Meeting_Date__c,
                    Client_Location_Start_Time__c = newTask.Client_Location_Start_Time__c,
                    Client_Location_End_Time__c = newTask.Client_Location_End_Time__c,
                    Location = newTask.Location__c,
                    Directions_to_the_Meeting_Location__c = newTask.Directions_to_the_Meeting_Location__c,
                    Meeting_Confirmed__c = newTask.Meeting_Confirmed__c,
                    Confirmation_Notes__c = newTask.Confirmation_Notes__c,
                    More_Comments__c = newTask.More_Comments__c,
                    Summary__c = newTask.Summary__c,
                    Next_Steps__c = newTask.Next_Steps__c));
                    }
  insert NewEvent;
}