• SFmaverick
  • NEWBIE
  • 25 Points
  • Member since 2010

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 15
    Questions
  • 53
    Replies

Summary: I've created a button that is displayed on a custom object. When clicked, I want this button to redirect the user to a Visualforce page with some parameters being passed from the current record. Here's the applicable section of code from the Button, VF Page, and Controller:

 

Button (This code is taking the value of the current records Week Start Date and Position and passing it into variables on the visualforce page):

 

 

/apex/scheduleview?BegDate={!Shift__c.Week_Start_Date__c}&Positions={!Shift__c.Position__c}

 

Visualforce Code:

Normally 'BegDate' and 'Positions' are generated from user input. BegDate is a text field (that receives dates: MM/DD/YYYY) and Positions is a drop down box that matches the position field on each Shift Object.

 

 

 

    <strong>Please enter the first day of the schedule you would like to see (mm/dd/yyyy):</strong> <apex:inputText value="{!BegDate}"/>
    <apex:selectList value="{!Positions}" Size="1">
        <apex:selectOptions value="{!items}"/>
        <apex:actionSupport event="onchange"></apex:actionSupport>
    </apex:selectList><p/>

 

 

Here is the controller code for handling this input:

 

 

    public String getPositions() {
        return Positions;
    }
    
    public void setPositions(String Positions) {
        this.Positions = Positions;
    }

    //Initializes and provides a get method for the user inputted date
    String BegDate;
    public String getBegDate (){
        return BegDate;
    }

     public void setBegDate (string BegDate) {
         this.BegDate = BegDate;
     }

 

 

Then of course these variables are used throughout the controller - but this is how they are brought in and initialized. What I'd like is for the current records values to be passed to the page when clicking the button - but I want the user to maintain the ability to also go to the visualforce page and enter their own input. This page is used to view and edit our schedule.

 

Any help would be greatly apprecaited - I read and attempted it quite a bit, but I've reset my code back to this default because it wasn't working.

 

 

 

 

 

 

 

Currently I have a visualforce page that takes a Date as user input and returns a table of records. Each column of the table represents a list of Shifts, the first list being the list of shifts that occurs on the date the user input - and then each column after incrementing that date by one.

 

Here's a quick visual of what I described without the actual records showing (confidential information):

 

 

Below each day, the records are output in an HTML Table. Each day is currently a seperate query that looks like this:

 

 

    public List<Shift__c> getTheList2() {
        Date NewDate = IncrementDayDate(1);
        If (Positions == 'Tech'){
            List<Shift__c> TheList2 = [SELECT Shift_Cancelled__c, Notes__c, ID, BGCOLOR__c, Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :NewDate AND (Position__C = 'Tech' OR Position__C = 'Pharmacy Support' OR Position__C = 'Material Handler')  ORDER BY Shift_Summary__c, Shift_Summary_2__c];
            return TheList2;
        } else {
            List<Shift__c> TheList2 = [SELECT Shift_Cancelled__c, Notes__c, ID, BGCOLOR__c, Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :NewDate AND Position__C = 'RPh'  ORDER BY Shift_Summary__c, Shift_Summary_2__c];
            return TheList2;
        }
    }

 

 

This ends up meaning 7 queries to the server for the lists, although the only difference in the filter is the Date. Ideally I'd only pull this list once and then seperate out each of the 7 days. Any ideas on how to do this?

 

Can you build a list, from a list? If Master_List exists as a List of all of the records for each day, could I pull each individual list from it like this? I want to avoid making 7 calls to the server to improve performance and code efficiency.

 

 

            List<Shift__c> TheList2 = [SELECT Shift_Cancelled__c, Notes__c, ID, BGCOLOR__c, Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM :Master_List WHERE Date__c = :NewDate AND Position__C = 'RPh'  ORDER BY Shift_Summary__c, Shift_Summary_2__c];
return TheList2;
}
}

 It would be even better if I could pull the Master List, and use something in my Visualforce code to spit out each day. The shorter my Apex code becomes - the easier future modification comes. I'm getting close to reaching governor limits on this page due to other queries, so I wanted to find room to improve the code.

 

Thanks for your help!

 

Background: I have a custom Object called Shift. It has lookup field to Contact. Each 'record' of the object Shift is just what you'd expect, it contains a Date and time that a Contact is scheduled to work.

 

Problem: I need to prevent users from scheduling the same contact for two shifts on the same date.

 

If there is a Shift where:  Contact = John Smith; Date = 9/15/2010, I don't want the user to be able to create another shift with those same values. I don't want to double book anybody.

 

I'm not sure of the simplest way to prevent this; or even a possible way. Thanks for any help / advice!

I have a page that intially will look like this that asks the user for a date:

 

 

After the user enters a date, the page generates a a table with 7 nested tables (one per column) that each pull from a list. Here's an example with user input.

 

 

The issue I'm having is that I can only get it to increment one day. Each column should go to the next day, but I can't wrap my head around why it's not doing that and how I can make it do that, without creating a seperate List method for each column and a seperate method for incrementing as well. The lists are selected based off of the date the user enters. The first column is all the records from the date they entered. The second column is all the records from the day they entered + 1. Ideally this trend will continue without making the code and hence the processing 7 times the labor. I know it can be done more simply, but I'm not familiar enough with Java and Apex to wrap my head around it. Any help is appreciated, my current code is below.

 

 

<apex:page showHeader="false" standardStyleSheets="false" controller="ScheduleViewController">
  <apex:form >
    <strong>Please enter the first day of the schedule you would like to see (mm/dd/yyyy):</strong> <apex:inputText value="{!BegDate}"/> 
  </apex:form>

  <apex:outputPanel >
    <table border="0" bordcolor="00000" cellspacing="0" width="80%" bgcolor ="#000000"> <!-- Table that contains 7 day of weektables -->
        <tr>
        <td>
        <apex:dataList value="{!TheList}" var="item"> <!-- Gets the list of shifts -->
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </table>
        </apex:dataList>
        </td>
        <apex:outputpanel id="Day2">
        <td>
        <apex:dataList value="{!TheList2}" var="item"> <!-- Gets the list of shifts -->
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </table>
        </apex:dataList>
        </td>
        </apex:outputpanel>
        
        <td>
        <apex:dataList value="{!TheList2}" var="item"> <!-- Gets the list of shifts -->
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </table>
        </apex:dataList>
        </td>
        
        <td>
        <apex:dataList value="{!TheList2}" var="item"> <!-- Gets the list of shifts -->
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </table>
        </apex:dataList>
        </td>
        
        <td>
        <apex:dataList value="{!TheList2}" var="item"> <!-- Gets the list of shifts -->
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </table>
        </apex:dataList>
        </td>
        
        <td>
        <apex:dataList value="{!TheList2}" var="item"> <!-- Gets the list of shifts -->
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </table>
        </apex:dataList>
        </td>
        
        <td>
        <apex:dataList value="{!TheList2}" var="item"> <!-- Gets the list of shifts -->
        <table border="1" bordercolor="00000" width="100%" bgcolor="00000">
            <tr>
            <td bgcolor="#30C452">{!item.Shift_Summary__c}
            <br></br>{!item.Shift_Summary_2__c}
            <br></br>{!item.Shift_Summary_3__c}
            <br></br>{!item.Day_of_week__c}</td>
            </tr>
        </table>
        </apex:dataList>
        </td>
        </tr>
    </table>
  </apex:outputPanel>
</apex:page>

 

public class ScheduleViewController {

    //Initializes and provides a get method for the user inputted date
    public String BegDate { get; set; }

    public Date DayDate;
    // Turns the User's string input into a date
    public Date getDayDate(){
        if (BegDate==null) {
            return null;
        } else {
            DayDate = stringToDate(BegDate);
            return DayDate;
        }
    }
    
    public Date IncrementDayDate(){
        if (DayDate!=null) {
            DayDate = DayDate + 1;
        }
        return DayDate;
    }

    
    //Converts a string from mm/dd/yy to a date
    public Date stringToDate(String s){
      //Input Date String is in the format mm/dd/yyyy
      if(s.length()== 0)
      {
      return NULL;
      }
      else{
      String[] stringDate = s.split('/');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
      }
    }
    
    //Gets TheList - an array of records from Shift__c where the Date equals DayDate
    public List<Shift__c> getTheList() {
        Date theDate=getDayDate();
        List<Shift__c> TheList = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :DayDate];
        System.Debug('MY LIST:' + TheList);
        System.Debug('DayDate:' + DayDate);
        return TheList;
    }
    
    public List<Shift__c> getTheList2() {
        Date NewDate = IncrementDayDate();
        List<Shift__c> TheList2 = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :NewDate];
        return TheList2;
    }
}

 

 

I'm generating a list of an SObject called Shift. The list is being pulled based on the date field. If I use the standard Today(), It correctly shows me the list of records for today.

 

My page takes user input as text and translates it to a Date called DayDate. I'd like to pull this list based on DayDate instead of Today().

 

Here's my Page code with the relevant parts highlighted:

 

<apex:page showHeader="false" standardStyleSheets="false" controller="ScheduleViewController">
<apex:form >
<apex:inputText value="{!BegDate}"/>
<!-- DayDate comes out correctly after inputting a date! -->
<apex:outputText value="{!DayDate}"/>
</apex:form>

<apex:outputPanel >
<apex:dataList value="{!TheList}" var="item"> <!-- Gets the list of shifts -->
<table border="0" bordcolor="00000" cellspacing="0" width="80%" bgcolor ="#000000"> <!-- Table that contains 7 day of weektables -->
<tr>
<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>
</tr>
</table>
</apex:dataList>
</apex:outputPanel>
</apex:page>

 

 

 

 My Controller Code:

 

 

public class ScheduleViewController {
//Initializes and provides a get method for the user inputted date
public String BegDate { get; set; }

public Date DayDate;
// Turns the User's string input into a date
public Date getDayDate(){
if (BegDate==null) {
return null;
} else {
return stringToDate(BegDate);
}
}

//Gets TheList - an array of records from Shift__c where the Date equals DayDate (it works if I use Today(),
//but not DayDate, even when I verify that DayDate has the same value as Today()!

public List<Shift__c> getTheList() {
List<Shift__c> TheList = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :DayDate];
System.Debug('MY LIST:' + TheList);
return TheList;
}
//Converts a string from mm/dd/yy to a date
public Date stringToDate(String s){
//Input Date String is in the format mm/dd/yyyy
if(s.length()== 0)
{
return NULL;
}
else{
String[] stringDate = s.split('/');
Integer m = Integer.valueOf(stringDate[0]);
Integer d = Integer.valueOf(stringDate[1]);
Integer y = Integer.valueOf(stringDate[2]);
return date.newInstance(y,m,d);
}
}
}

 

 

 

 

Basically throughout my visualforce page, I'd like to take a variable that's been set, and I'd like to increment it by 1 several different times. I'm sure there's an easy way to make a call like this that I'm just not considering. Here's the current code:

 

Page Code - It's simply taking in a text value called BegDate and outputting a Date value called DayDate

 

 

<apex:page showHeader="false" standardStyleSheets="false" controller="TestViewController">
  <apex:form >
    <apex:inputText value="{!BegDate}"/>
    
    <apex:outputText value="{!DayDate}"/>
    
    <!-- This is where I'd like to output DayDate a second time, but incremented by 1>
    <apex:outputText value="{!DayDate}"/>
  </apex:form>
</apex:page>

 

Controller Code: It's taking the user input and converting it from text to a date format. I'd like to somehow make call to the controller from the visualforce page to increment DayDate by 1.

 

 

public class TestViewController {

    public String BegDate { get; set; }

    // Turns the User's string input into a date
    public Date getDayDate(){
        if (BegDate==null) {
            return null;
        } else {
            return stringToDate(BegDate);
        }
    }
    
    //Converts a string from mm/dd/yyyy to a date
    public Date stringToDate(String s){
      //Input Date String is in the format mm/dd/yyyy
      if(s.length()== 0)
      {
      return NULL;
      }
      else{
      String[] stringDate = s.split('/');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
      }
    }
}

 

 

 

Ok, very simply I'm having trouble with a part of my visualforce page and controller where I try to take a user's input (Text format date in the form of DD/MM/YYYY) and then output it as a date.

 

I understand that BegDate is initialized to 'Null' when the page is loaded because the controller is compiled before the page, what I don't know is how to fix that.

 

I'm getting the error:

 

 

System.TypeException: Invalid integer: BegDate

Class.TestViewController.stringToDate: line 19, column 20 Class.TestViewController.getDayDate: line 7, column 16 External entry point


 

 

My VF Page Code is:

 

 

<apex:page showHeader="false" standardStyleSheets="false" controller="TestViewController">
  <apex:form >
    <apex:inputText value="{!BegDate}"/>
    
    <apex:outputText value="{!DayDate}"/>
  </apex:form>
</apex:page>

 My Controller Code is:

 

 

public class TestViewController {

    public String BegDate { get; set; }

    // Turns the User's string input into a date
    Public Date getDayDate(){
        return stringToDate('BegDate');
    }
    
    //Converts a string from mm/dd/yyyy to a date
    public Date stringToDate(String s){
      //Input Date String is in the format mm/dd/yyyy
      if(s.length()== 0)
      {
      return NULL;
      }
      else{
      String[] stringDate = s.split('/');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
      }
    }
}

 

 

 

 

This is killing my brain, why is it telling me the variable doesn't exist? It's defined within the controller and used in the method before this!

Error: Compile Error: Variable does not exist: BegDate at line 9 column 40

 

 

public class ScheduleViewController {
    //Initializes and provides a get method for the user inputted date
    public String[] BegDate;
    public String[] getBegDate()
    {
      return BegDate;
    }

//Initalizes a date field to put the string into for use as a filter criteria
Static Date DayDate = stringToDate(BegDate);

//Adds one day to the current DayDate field in order to provide consecutive days' lists public void setDayDate() { DayDate = DayDate + 1; }
//Gets TheList - an array of records from Shift__c where the Date equals DayDate public List<Shift__c> getTheList() { List<Shift__c> TheList = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = TODAY]; System.Debug('MY LIST:' + TheList); return TheList; }
//Converts a string from mm/dd/yyyy to a date public Static Date stringToDate(String s){ //Input Date String is in the format mm/dd/yyyy String[] stringDate = s.split('/'); Integer m = Integer.valueOf(stringDate[0]); Integer d = Integer.valueOf(stringDate[1]); Integer y = Integer.valueOf(stringDate[2]); return date.newInstance(y,m,d); } }

 

 

Currently my controller looks like this:

 

 

public class ScheduleViewController {
    public List<Shift__c> getTheList() {
        List<Shift__c> TheList = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c FROM Shift__c WHERE Date__c = TODAY];
        System.Debug('MY LIST:' + TheList);
        return TheList;
    }
}

 

The list is filtered by the date, where the Date is equal to today. Instead, I'd like the user to be able to specify a date and have the list pulled from that date. I'm not sure how I can pass the date as a parameter to the controller method.\

 

My current call to the list is simply:

 

 

    <apex:dataList value="{!TheList}" var="item">

 

How would I go about querying that list based on a user specified date?

 

 

Alright, I'm simply trying to make a table based on a custom object called Shift__c.

 

I'd like the table to be titled with a field Department__c followed by the word Schedule.

I'd like 7 columns of records; Each record will populate three lines, ideally in a single row, but that's unimportant at this point.

 

Each column will consist of all of the records of the Shift__c object that have a specified value in their Date__c field.

Each "cell" in that column will consist of three fields from the Shift__c Object, Ideally one field per line, otherwise each field would need a seperate cell.

 

I have no trouble generating a List, I just have trouble formatting the Data into the view I'd like. I'm doing it via a custom controller.

 

Here is an example of the ideal output:

 

 

Here's the raw version of that, what fields are where:

 

 

 

Any help would be GREATLY appreciated. Thanks!

 

 

Background - I have an Object in Salesforce called Shifts, where the user will enter a shift, they are required to enter:


Date

Start Time

End Time

Department

 

The Employee isn't always entered yet, because somebody has to be assigned, but a lot of times it's there. The reports from Salesforce are limited in how you can display them, it's a lot harder to see where the open shifts are and when people are free if you view these shifts in a list view. Prior to Salesforce, we used Excel and we had something like this (this is just a snippet, It's MUCH larger with many more departments and employees)

 

Schedule Snippet

 

I have all the things you see in this view available in Salesforce. I have a formula field that generages everything in the little boxes. I need some advice / help on how I can write in visualforce, to put the Shift summary formula field under a column based on that shift's Date value. Basically, from left to right I'd like them organized by date, with one day per column. From top to bottom I'd like them organized alphabetically.

 

Any ideas / suggestions?

 

Thanks!

Here's the trigger code:

 

trigger ConsecutiveShifts on Shift__c (after insert) {

//Counter will be used to increment the Date for each new shift
Integer Counter = 1;

for(Shift__c a : trigger.new)
{
    Decimal Consec_Shifts = a.Consecutive_Shifts__C;
    While(Consec_Shifts > 1){
        Shift__c b = New Shift__c();
        b.markets__c = a.markets__c;
        b.date__c = a.date__c + Counter;
        b.client__c = a.client__c;
        b.position__c = a.position__c;
        b.shift_start__c = a.shift_start__c;
        b.shift_end__c = a.shift_end__c;
        b.contractor__c = a.contractor__c;
        b.notes__c = a.notes__c;
        b.status__c = a.status__c;
        b.shift_cancelled__c = a.shift_cancelled__c;
        b.last_minute__c = a.last_minute__c;
        b.pic__c = a.pic__c;
        b.Consecutive_Shifts__c = 1;
        Counter++;
        insert(b);
        Consec_Shifts--;
    }
}
}

 

and here is what I thought would be sufficient testing for it...

 

 

 

@isTest
private class TestShift {

    static testMethod void TestShiftInsert() {
        // TO DO: implement unit test
          system.debug('in test method');
      Shift__c l = new Shift__c(Markets__c = 'a0VA00000005DLh', Client__c = '001A0000002KIGB',
      Position__c = 'RPH', Shift_Start__c = '11:00', Shift_End__C = '12:00', Contractor__c = '003A0000001rWdW', Notes__c = 'Testing',
      Consecutive_Shifts__C = 5, Status__C = 'Confirmed', Shift_Cancelled__C = True, Last_Minute__c = True, PIC__c = True);
        try
        {
        insert l;
        }
        catch(DmlException e){
          system.debug(e.getMessage() + 'testing the insertion of a shift.');
        }
    }
}

 

 

Since the trigger is procced off of an inserted shift, I made sure to insert one in my test class. The trigger functions great on my sandbox, I just need to have the testing in place to implement it into Salesforce. It's not showing any coverage for the trigger and I can't figure out why.

 

Thanks for any help!

I'm asking if this will work...

 

trigger ConsecutiveShifts on Shift__c (before insert) {

 

Integer Counter = 1;


for(Shift__c a : trigger.new)
{
    for(Integer I = a.Consecutive_Shifts; I > 1; I--){
        Shift__c b = New Shift__c();
        b = a;

        b.date__c += Counter;

        b.Consecutive_Shifts = 1;

        Counter++;

        insert(b);

    }

    a.Consecutive_Shifts = 1;

}

 

Basically, the user is going to enter a lot of information to create a record for the Object Shift. The field Consecutive shift will by default be one, so by default only one shift will be created. However, if the user enters something higher than 1, then I want this trigger to take all the information they put in intially and create another object of type Shift for each number over 1. The only difference in these records will be the date, each record will have a date value of one day more than the last.

 

My idea is that the line:

b = a;

Will transfer everything in a and put it in b. Afterwards, the only fields I don't want the same in b are date and consecutive shifts, so I change them after inserting the values of a. I create the shift, and then I test the loop counter again.

Here's my 'problem' -

 

I'm not new to programming, but I'm quite new to Salesforce and Apex. My programming experience lies mainly in C++ and VB.

 

I'd like to create a custom button to replace the standard 'Save' button for a custom object. I have an object that is used as a schedule, each record of this object type is one shift, on one day, for one employee. With over 100 employees, there are a lot of shifts to enter. I find that it will save a LOT of time if users can enter multiple shifts at once, and I see an opportunity for this when the only thing that changes between two shifts is the date by an increment of one day. Lots of employees work in the same department at the same hours, 5 days in a row.

 

Ideally the button would make these almost identical shifts easier to enter. I'll create a field where the user can enter how many consecutive days the shift will be happening and they'll enter the start date. The default for the field for consecutive days will always "1", so that unless changed, a single record will be created. If the user enters 5, I want 5 total records to be created, the first will start on the "start date" and each record thereafter will start one day later.

 

Example: Start Date = 10/1/10

Consecutive Days: 3

 

Should create three records, all the other fields on the record the same, and the dates being 10/1/10, 10/2/10 and 10/3/10.

 

Like I said, I'm really knew to Salesforce and Apex, so something somewhat detailed would be great. Thanks!

I have the same problem. I want to lock the picklist field from being edited after the picklist value "Completed" is selected.

 

Picklist = Drugscreen

"Need To Submit"

"Submitted"

"Completed"

 

After "Completed" is selected I want to lock the picklist from being edited. 

 

Summary: I've created a button that is displayed on a custom object. When clicked, I want this button to redirect the user to a Visualforce page with some parameters being passed from the current record. Here's the applicable section of code from the Button, VF Page, and Controller:

 

Button (This code is taking the value of the current records Week Start Date and Position and passing it into variables on the visualforce page):

 

 

/apex/scheduleview?BegDate={!Shift__c.Week_Start_Date__c}&Positions={!Shift__c.Position__c}

 

Visualforce Code:

Normally 'BegDate' and 'Positions' are generated from user input. BegDate is a text field (that receives dates: MM/DD/YYYY) and Positions is a drop down box that matches the position field on each Shift Object.

 

 

 

    <strong>Please enter the first day of the schedule you would like to see (mm/dd/yyyy):</strong> <apex:inputText value="{!BegDate}"/>
    <apex:selectList value="{!Positions}" Size="1">
        <apex:selectOptions value="{!items}"/>
        <apex:actionSupport event="onchange"></apex:actionSupport>
    </apex:selectList><p/>

 

 

Here is the controller code for handling this input:

 

 

    public String getPositions() {
        return Positions;
    }
    
    public void setPositions(String Positions) {
        this.Positions = Positions;
    }

    //Initializes and provides a get method for the user inputted date
    String BegDate;
    public String getBegDate (){
        return BegDate;
    }

     public void setBegDate (string BegDate) {
         this.BegDate = BegDate;
     }

 

 

Then of course these variables are used throughout the controller - but this is how they are brought in and initialized. What I'd like is for the current records values to be passed to the page when clicking the button - but I want the user to maintain the ability to also go to the visualforce page and enter their own input. This page is used to view and edit our schedule.

 

Any help would be greatly apprecaited - I read and attempted it quite a bit, but I've reset my code back to this default because it wasn't working.

 

 

 

 

 

 

 

Currently I have a visualforce page that takes a Date as user input and returns a table of records. Each column of the table represents a list of Shifts, the first list being the list of shifts that occurs on the date the user input - and then each column after incrementing that date by one.

 

Here's a quick visual of what I described without the actual records showing (confidential information):

 

 

Below each day, the records are output in an HTML Table. Each day is currently a seperate query that looks like this:

 

 

    public List<Shift__c> getTheList2() {
        Date NewDate = IncrementDayDate(1);
        If (Positions == 'Tech'){
            List<Shift__c> TheList2 = [SELECT Shift_Cancelled__c, Notes__c, ID, BGCOLOR__c, Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :NewDate AND (Position__C = 'Tech' OR Position__C = 'Pharmacy Support' OR Position__C = 'Material Handler')  ORDER BY Shift_Summary__c, Shift_Summary_2__c];
            return TheList2;
        } else {
            List<Shift__c> TheList2 = [SELECT Shift_Cancelled__c, Notes__c, ID, BGCOLOR__c, Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :NewDate AND Position__C = 'RPh'  ORDER BY Shift_Summary__c, Shift_Summary_2__c];
            return TheList2;
        }
    }

 

 

This ends up meaning 7 queries to the server for the lists, although the only difference in the filter is the Date. Ideally I'd only pull this list once and then seperate out each of the 7 days. Any ideas on how to do this?

 

Can you build a list, from a list? If Master_List exists as a List of all of the records for each day, could I pull each individual list from it like this? I want to avoid making 7 calls to the server to improve performance and code efficiency.

 

 

            List<Shift__c> TheList2 = [SELECT Shift_Cancelled__c, Notes__c, ID, BGCOLOR__c, Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM :Master_List WHERE Date__c = :NewDate AND Position__C = 'RPh'  ORDER BY Shift_Summary__c, Shift_Summary_2__c];
return TheList2;
}
}

 It would be even better if I could pull the Master List, and use something in my Visualforce code to spit out each day. The shorter my Apex code becomes - the easier future modification comes. I'm getting close to reaching governor limits on this page due to other queries, so I wanted to find room to improve the code.

 

Thanks for your help!

 

Background: I have a custom Object called Shift. It has lookup field to Contact. Each 'record' of the object Shift is just what you'd expect, it contains a Date and time that a Contact is scheduled to work.

 

Problem: I need to prevent users from scheduling the same contact for two shifts on the same date.

 

If there is a Shift where:  Contact = John Smith; Date = 9/15/2010, I don't want the user to be able to create another shift with those same values. I don't want to double book anybody.

 

I'm not sure of the simplest way to prevent this; or even a possible way. Thanks for any help / advice!

I'm generating a list of an SObject called Shift. The list is being pulled based on the date field. If I use the standard Today(), It correctly shows me the list of records for today.

 

My page takes user input as text and translates it to a Date called DayDate. I'd like to pull this list based on DayDate instead of Today().

 

Here's my Page code with the relevant parts highlighted:

 

<apex:page showHeader="false" standardStyleSheets="false" controller="ScheduleViewController">
<apex:form >
<apex:inputText value="{!BegDate}"/>
<!-- DayDate comes out correctly after inputting a date! -->
<apex:outputText value="{!DayDate}"/>
</apex:form>

<apex:outputPanel >
<apex:dataList value="{!TheList}" var="item"> <!-- Gets the list of shifts -->
<table border="0" bordcolor="00000" cellspacing="0" width="80%" bgcolor ="#000000"> <!-- Table that contains 7 day of weektables -->
<tr>
<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>

<td>
<table border="1" bordercolor="00000" width="100%" bgcolor="00000">
<tr>
<td bgcolor="#30C452">{!item.Shift_Summary__c}
<br></br>{!item.Shift_Summary_2__c}
<br></br>{!item.Shift_Summary_3__c}</td>
</tr>
</table>
</td>
</tr>
</table>
</apex:dataList>
</apex:outputPanel>
</apex:page>

 

 

 

 My Controller Code:

 

 

public class ScheduleViewController {
//Initializes and provides a get method for the user inputted date
public String BegDate { get; set; }

public Date DayDate;
// Turns the User's string input into a date
public Date getDayDate(){
if (BegDate==null) {
return null;
} else {
return stringToDate(BegDate);
}
}

//Gets TheList - an array of records from Shift__c where the Date equals DayDate (it works if I use Today(),
//but not DayDate, even when I verify that DayDate has the same value as Today()!

public List<Shift__c> getTheList() {
List<Shift__c> TheList = [SELECT Shift_Summary__c, Shift_Summary_2__c, Shift_Summary_3__c, Day_of_week__c, Date__c FROM Shift__c WHERE Date__c = :DayDate];
System.Debug('MY LIST:' + TheList);
return TheList;
}
//Converts a string from mm/dd/yy to a date
public Date stringToDate(String s){
//Input Date String is in the format mm/dd/yyyy
if(s.length()== 0)
{
return NULL;
}
else{
String[] stringDate = s.split('/');
Integer m = Integer.valueOf(stringDate[0]);
Integer d = Integer.valueOf(stringDate[1]);
Integer y = Integer.valueOf(stringDate[2]);
return date.newInstance(y,m,d);
}
}
}

 

 

 

 

Basically throughout my visualforce page, I'd like to take a variable that's been set, and I'd like to increment it by 1 several different times. I'm sure there's an easy way to make a call like this that I'm just not considering. Here's the current code:

 

Page Code - It's simply taking in a text value called BegDate and outputting a Date value called DayDate

 

 

<apex:page showHeader="false" standardStyleSheets="false" controller="TestViewController">
  <apex:form >
    <apex:inputText value="{!BegDate}"/>
    
    <apex:outputText value="{!DayDate}"/>
    
    <!-- This is where I'd like to output DayDate a second time, but incremented by 1>
    <apex:outputText value="{!DayDate}"/>
  </apex:form>
</apex:page>

 

Controller Code: It's taking the user input and converting it from text to a date format. I'd like to somehow make call to the controller from the visualforce page to increment DayDate by 1.

 

 

public class TestViewController {

    public String BegDate { get; set; }

    // Turns the User's string input into a date
    public Date getDayDate(){
        if (BegDate==null) {
            return null;
        } else {
            return stringToDate(BegDate);
        }
    }
    
    //Converts a string from mm/dd/yyyy to a date
    public Date stringToDate(String s){
      //Input Date String is in the format mm/dd/yyyy
      if(s.length()== 0)
      {
      return NULL;
      }
      else{
      String[] stringDate = s.split('/');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
      }
    }
}

 

 

 

Hi all,

 

I am trying to move a Trigger into Production but I am getting a 0% coverage when I validate it through Eclipse/Force.com.  I have 100% coverage when I run all tests in the sandbox.

 

The trigger will move the owner of an event to match the new lead owner once the lead is moved from a lead queue owner.

 

Here is the trigger and class.  I already received help from the discussion board on the trigger, thanks again in advance.

 

Any suggestions?

 

 

**** Trigger ****

trigger ownerupdate on Lead (after Update) {

List<Event> aplist = new List<Event>();

for(Lead t : Trigger.new){

IF(
(t.LeadSource == 'Telemarketing'
    && t.OwnerId != '00G30000001iNeKEAU' 
     && t.OwnerId != '00G30000001iNtxEAE'
      && t.OwnerId != '00G30000001uiO5'
       && t.OwnerId != '00G30000001iCwM'
        && t.OwnerId != '00G30000001uh70'
         && t.OwnerId != '00G30000001iNt5EAE'
          && t.OwnerId != '00G30000001ui8wEAA'
           && t.OwnerId != '00G30000001iNuC'
            && t.OwnerId != '00G30000001iNsL'
             && t.OwnerId != '00G30000001iNvZ'
              && t.OwnerId != '00G30000001jtEi'
               && t.OwnerId != '00G30000001tiXR'
                && t.OwnerId != '00G30000001ui8w'
                 && t.OwnerId != '00G30000001iNt5'
))

{List<Event> aps = [Select WhoId, Whatid, OwnerId from Event where WhoId = :t.Id];
   for (Event ap : aps)
   {       
      ap.OwnerId = t.OwnerId;

      aplist.add( ap );
   }

}}
update aplist;
}
trigger ownerupdate on Lead (after Update) {
    && t.OwnerId != '00G30000001iNeKEAU' 
**** Class Test Method **** public class ownerupdate { static testMethod void T1() { Id lId; Id eId; //CREATE 1 LEAD Lead l = new Lead(); l.LeadSource = 'Telemarketing'; l.lastname = 'Doe'; l.company = 'Test Company'; l.ownerId = '00530000003IFwV'; l.status = 'Open'; l.market__c = 'Atlanta'; Database.insert(l); lId = l.Id; //CREATE 2 Event Event e = New Event (); e.DurationInMinutes = 60; e.ActivityDateTime = date.today(); e.ownerId = l.ownerId; e.WhoId = l.Id; Database.SaveResult result = Database.insert(e); eId = e.Id; update e; {List<Lead> aps = [Select Leadsource, owner_convert_queue__c, id, OwnerId from Lead where id = :e.whoId]; system.debug('@@@apo=' + aps); } }}

 

Alex

 

  • September 01, 2010
  • Like
  • 0