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
Kevin StrangeKevin Strange 

Prepend a visualforce inputtext field with a text value if it's not blank -

Hi, 
  I have a visualforce page with various inputText fields that are being concatenated into a single field - everything is working 
  However, I would like to "Prepend" or add text to the submitted value but only if it is filled in. Should this be done in the Controller 
 or can it be achieved in visualforce or both. 

  Example: I have 2 inputtext fields.   One called Fruit the other Vegetables 
  If someone filled out the Fruit Field with "Apple" but left the Vegetable field blank  I would like the result to  be:
   Fruit: Apple 
  Vegetable did not pass because it was blank. 





 
Visualforce 
<apex:page controller="SubmitCaseController">
<h1>Submit New Case</h1>
    <apex:form >
        <apex:pageMessages />
        <table>
            <tr>
                <th>Your Name:</th>
                <td><apex:inputText value="{!c.SuppliedName}"/></td>
            </tr>
            <tr>
                <th>Your Email:</th>
                <td><apex:inputText value="{!c.SuppliedEmail}"/></td>
            </tr> 
            <tr>
                <th>Subject:</th>
                <td><apex:inputText required="true" value="{!c.Subject}"/></td>
            </tr>
            <tr>
                <th>Field1:</th>
                <td><apex:inputText value="{!Fruit}"/></td>
            </tr>
             <tr>
                <th>Field2:</th>
                <td><apex:inputText required="true" value="{!Vegetable}"/></td>
            </tr>                
            <tr>
                <td><apex:commandButton value="Submit Case" action="{!submitCase}"/></td>
            </tr>
        </table>
    </apex:form>
</apex:page>

******************


Apex Controller 

public class SubmitCaseController {
    public Case c { get; set; } 
    public SubmitCaseController() {
        c = new Case();
    }
    
    //Variables
    public String Fruit {get; set;}
    public String Vegetables {get; set;}

    
    public PageReference submitCase() {
   
            try {
                // Insert the case
                c.Description = 'Fruit\n' + 'Vegetable';
                INSERT c;
                return null;
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
    }
}


 Any help would be much appreciated. 
 Thank You
Raj VakatiRaj Vakati
You can try some thing like below 
 
String description =''; 

if(Fruit !=null && Fruit !=''){
	description  = description+Fruit+'/n' ; 
}
if(Vegetables  !=null && Vegetables  !=''){
	description  = description+Vegetables +'/n' ; 
}
description = description.removeEnd('/n');


c.Description = description ;

 
Kevin StrangeKevin Strange
Hi, Thank you appreciate the help - would this still be a good method if there were say 20+ form fields ? 
 I was reading up on isBlank(inputString)  -  My form ultimately will have 20 or so inputtextfields - which will all concatenate into the Case Description field - 
Raj VakatiRaj Vakati
If you have 20 fields, keep all of them in set or map and process it once in for loop 
Kevin StrangeKevin Strange
Hi Raj,
    Thank you for your help!  Could you point me toward an example or two?   many thanks....
Raj VakatiRaj Vakati
Map<String,String> str = new Map<String,String>();
str.put('filed' ,'value') ; 
... Add all fields and values

String description ='';

for(String s : str.values(){
    description = description+Fruit+'/n' ;

}

 
Kevin StrangeKevin Strange
Thank you - but I don't see where you can "prepend"  text if the field is not blank - I couldn't get your example to work.