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
sashamsasham 

Converting to string from multiselect pick list. Any help on this Pls

It is  a Apex code 
My pick list value  for TimeFrame __c  is  12:00 - 08:00 AM.  
I  need to insert into string  starttime__c  as 12 Am and  endTime__C as  8 AM from that value that i m getting ffor TimeFrame __c
Best Answer chosen by sasham
Glyn Anderson 3Glyn Anderson 3
OK.  I still think using maps to look up the start and end times is the way to go.  You'll have to add your 6th value to the maps, as you didn't post it.

<pre>
public static Map<String,String> startTimes = new Map<String,String>
{   '08:00 - 10:00 AM' => '8 AM',
    '10:00 - 12:00 AM' => '10 AM',
    '01:00 - 02:00 PM' => '1 PM',
    '02:00 - 03:00 PM' => '2 PM',
    '03:00 - 04:00 PM' => '3 PM'
};
public static Map<String,String> endTimes = new Map<String,String>
{   '08:00 - 10:00 AM' => '10 AM',
    '10:00 - 12:00 AM' => '12 AM',
    '01:00 - 02:00 PM' => '2 PM',
    '02:00 - 03:00 PM' => '3 PM',
    '03:00 - 04:00 PM' => '4 PM'
};

String timeFrame = recordA.TimeFrame__c.substringBefore( ';' ); 
recordB.StartTime__c = startTimes.get( timeFrame );
recordB.EndTime__c = endTimes.get( timeFrame );
</pre>
 

All Answers

Glyn Anderson 3Glyn Anderson 3
How many different picklist values are there?  (And is it really multi-select?  Your example isn't a multi-select example.)  If there is a small number of picklist values, you can create a couple of maps:

<pre>
public static Map<String,String> startTimes = new Map<String,String>
{   '12:00 AM - 08:00 AM' => '12 AM',
    '08:00 AM - 04:00 PM' => '8 AM',
    '04:00 PM - 12:00 AM' => '4 PM'
};
public static Map<String,String> endTimes = new Map<String,String>
{   '12:00 AM - 08:00 AM' => '8 AM',
    '08:00 AM - 04:00 PM' => '4 PM',
    '04:00 PM - 12:00 AM' => '12 AM'
};

myRecord.StartTime__c = startTimes.get( myRecord.TimeFrame__c );
myRecord.EndTime__c = endTimes.get( myRecord.TimeFrame__c );
</pre>
sashamsasham
Thanks Glyn, 
Yes It is multi Select pick list value
I just gave one example for that value .
This value for the  Multi select pick list field TimeFrame __c   is quering from  a object  which already has  avalue 
and  
And the  i need to insert  record into  an other  object  with 2  string fields  starttime and endtime 

Ex:TimeFrame __c  is  12:00 - 08:00 AM. 
starttime: 12 AM
endtime  : 8 AM
 
sashamsasham
I tried  like this But not sure if i need to use map

string s = '12:00 - 08:00 PM';
integer num = s.length();
if (num >0){

String start = s.substring(0,2);
String endt =  s.substring(8,10);
String r = s.substring(14,16);
//integer num = s.length();
//if (num < 0)

system.debug(start);
system.debug(endt);
system.debug(r);

if (start.substring(0,1)=='0'){
    start=start.substring(1,1);
   system.debug(start);   
}
 
if (endt.substring(0,1)=='0'){
    endt=endt.substring(1,2);
   system.debug(endt);   
}

start = start + ' '+ r;
endt = endt + ' ' +  r;
  
    
}
sashamsasham
And also 5 diffent picklist values for the Timeframe__c field 
Glyn Anderson 3Glyn Anderson 3
Please post all 5 picklist values.
sashamsasham
Actually it is 6 values 
08:00 - 10:00 AM
10:00 - 12:00 AM
01:00 - 02:00 PM
02:00 - 03:00 PM
03:00 - 04:00 PM
 
Glyn Anderson 3Glyn Anderson 3
OK, well you posted 5 of them.  Is it possible for TimeFrame__c to contain more than one of these at a time, like this:

TimeFrame__c = '08:00 - 10:00 AM;01:00 - 02:00 PM;02:00 - 03:00 PM';

Can a user select more than one value at a time?
sashamsasham
I already checked this one.
Yes able to select  morethan one at a time since it is multiselct . But recoed is inserted with the first one  and time frame value is the first one 
if they selected TimeFrame__c = '08:00 - 10:00 AM;01:00 - 02:00 PM;02:00 - 03:00 PM' this one.,the value for  timefarme  in that object is  '08:00 - 10:00 AM.. So i always get one value for time frame. 
This is how data i can see in that object. ( developed by someone)
 
Glyn Anderson 3Glyn Anderson 3
OK.  I still think using maps to look up the start and end times is the way to go.  You'll have to add your 6th value to the maps, as you didn't post it.

<pre>
public static Map<String,String> startTimes = new Map<String,String>
{   '08:00 - 10:00 AM' => '8 AM',
    '10:00 - 12:00 AM' => '10 AM',
    '01:00 - 02:00 PM' => '1 PM',
    '02:00 - 03:00 PM' => '2 PM',
    '03:00 - 04:00 PM' => '3 PM'
};
public static Map<String,String> endTimes = new Map<String,String>
{   '08:00 - 10:00 AM' => '10 AM',
    '10:00 - 12:00 AM' => '12 AM',
    '01:00 - 02:00 PM' => '2 PM',
    '02:00 - 03:00 PM' => '3 PM',
    '03:00 - 04:00 PM' => '4 PM'
};

String timeFrame = recordA.TimeFrame__c.substringBefore( ';' ); 
recordB.StartTime__c = startTimes.get( timeFrame );
recordB.EndTime__c = endTimes.get( timeFrame );
</pre>
 
This was selected as the best answer
sashamsasham
Thnaks Glyn I like the map ,In the future , if some one update the pick list with one more values, i need to update the map as well.
But i got the requirement , without touching the coding , they wiil be able to add more values to the picklist. and it should work 

 
Glyn Anderson 3Glyn Anderson 3
If you really must parse the string, here's a way to do it with Regular Expressions.  This will let you incorporate the minutes part of the times in the future, if any time frames include them (e.g. 9:30 - 11:30 AM).  Note that this does not detect whether the time frame crosses a meridian boundary (e.g. 11:00 - 01:00 PM -- start time should be 11 AM, but will compute as 11 PM).  That's a benefit of using the maps - you can map the correct meridians to both times.

<pre>
String timeFrame = recordA.timeFrame__c.substringBefore( ';' );
Matcher matcher = Pattern.compile( '(\\d\\d):(\\d\\d) - (\\d\\d):(\\d\\d) (AM|PM)' ).matcher( timeFrame );
if ( matcher.matches() )
{
    String startHour = matcher.group( 1 ).removeStart( '0' );
    String endHour = matcher.group( 3 ).removeStart( '0' );
    String meridian = matcher.group( 5 );
    recordB.StartTime__c = startHour + ' ' + meridian;
    recordB.EndTime__c = endHour + ' ' + meridian;
}
</pre>
 
sashamsasham
Thank you Glyn for that details about using the map for this. I will be using that