# Pre-populate Form Fields with Data from External Source

You may initialize a new form with some fields pre-populated using Bean Shell Form Binder.

Figure 1 below shows an example of a form whose first 3 fields are to be pre-populated. Figure 1: Form with Fields to Pre-populate

The quick and easy approach in addressing this requirement is to make use of the Beanshell Form Binder in the section's Load Binder. Edit the section. Figure 2: Configuring Section Properties to Determine How Data Will Be Handled

In Load Binder, select "Bean Shell Form Binder" as the Load Binder. Figure 3: Choose Beanshell Form Binder as the Load Binder

Configure the Bean Shell Form Binder with your own coding to populate relevant fields, as shown in the figure below.

Code used in this example:

```import org.joget.apps.app.service.*;
import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import java.util.*;

public FormRowSet getData() {

//-----------------------------------------------------------------------------------
//In this part of code, it trying to load the original data from form data table.

FormRowSet results = null;
if (primaryKey != null && primaryKey.trim().length() > 0) {
AppService appService = (AppService) FormUtil.getApplicationContext().getBean("appService");
Form form = FormUtil.findRootForm(element);
if (form.equals(element) && form.getParent() != null) {
form = FormUtil.findRootForm(form.getParent());
}
if (form != null) {
}
}
//------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------
//In this second part of code, it will load the data from external source by using
//JDBC. It will run only when the first part of code fail to retrieve data from
//form data table. This example use dir_user table of Joget as external source.

if (results == null) {
results = new FormRowSet();

Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jwdb2?characterEncoding=UTF-8", "root", "root");

if(!con.isClosed()){
String sql = "SELECT firstName, lastName, email FROM dir_user WHERE username=?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, pId);

ResultSet rs = stmt.executeQuery();
while (rs.next()) {
FormRow row = new FormRow();
row.put("firstName", (rs.getString(1) != null)?rs.getString(1):"");
row.put("lastName", (rs.getString(2) != null)?rs.getString(2):"");
row.put("email", (rs.getString(3) != null)?rs.getString(3):"");
}
}

} catch(Exception ex) {
System.err.println("Exception: " + ex.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
//------------------------------------------------------------------------------------

return results;
}

return getData();``` Figure 4: Populate Beanshell Form Binder with the Necessary Codes

If the coding is properly written and tested, you should get this result: Figure 5: The 3 Pre-populated Fields After a New Form is Loaded

• No labels
{"ft_lang_label":"__label__en","ft_lang_prob":0.5076079,"math_prob":0.5914799,"size":3181,"snap":"2022-40-2023-06","text_gpt3_token_len":684,"char_repetition_ratio":0.19578218,"word_repetition_ratio":0.0,"special_character_ratio":0.34360263,"punctuation_ratio":0.24236985,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9606796,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,2,null,2,null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-09T00:22:38Z\",\"WARC-Record-ID\":\"<urn:uuid:822c97fc-1c54-4bf0-8026-9a068a28b747>\",\"Content-Length\":\"61365\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f570836f-78db-474a-95f3-22a659850539>\",\"WARC-Concurrent-To\":\"<urn:uuid:2dd1f96f-a79d-4e54-ae20-f3a5f543bfa7>\",\"WARC-IP-Address\":\"54.244.27.116\",\"WARC-Target-URI\":\"https://dev.joget.org/community/display/KBv6/Pre-populate+Form+Fields+with+Data+from+External+Source\",\"WARC-Payload-Digest\":\"sha1:SQ3KT452VMUL2GYQUM32WPYNUYR77LZP\",\"WARC-Block-Digest\":\"sha1:D2222RYAFFFKOO5SXTJDUPWMXYCV47PS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500983.76_warc_CC-MAIN-20230208222635-20230209012635-00036.warc.gz\"}"}