Skip to content

Instantly share code, notes, and snippets.

@logontokartik
Last active January 4, 2016 06:28
Show Gist options
  • Save logontokartik/8581712 to your computer and use it in GitHub Desktop.
Save logontokartik/8581712 to your computer and use it in GitHub Desktop.
Controller for Searching Records
public class SearchAccountsCon{
@RemoteAction
public static List<ObjectWrapper> searchAccounts(String searchString, String objectName){
List<ObjectWrapper> aWrapper = new List<ObjectWrapper>();
List<SObject> records = new List<SObject>();
// Dynamically build the soql
String soql = 'Select Id, OwnerId, Name, '; // Get these regardless.
for(Schema.FieldSetMember f : SObjectType.Account.FieldSets.getMap().get('AccountListViewFS').getFields()){ // Use the System Label to identify the fieldset and get the fields
soql += f.getFieldPath() + ',';
}
// Also get the Account Owners list
soql = soql.removeEnd(',');
soql += ' from Account where Name Like \'%' + String.escapeSingleQuotes(searchString) + '%\'';
records = Database.query(soql);
// Build the wrapper
for(SObject a : records){
boolean isMyObject = false;
if(a.get('OwnerId') == UserInfo.getUserId())
isMyObject = true;
aWrapper.add(new ObjectWrapper(a,isMyObject));
}
return aWrapper;
}
// Wrapper class.
public class ObjectWrapper {
public boolean isMyObject {get;set;}
public SObject record {get;set;}
public ObjectWrapper(SObject a, boolean isMyObject){
this.record = a;
this.isMyObject = isMyObject;
}
}
}
<apex:page controller="SearchAccountsCon" doctype="html-5.0">
<apex:sectionHeader title="Salesforce Account Search"/>
<head>
<title>Account Search</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Latest compiled and minified JavaScript -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<style>
.box-table-a
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 45px;
width: 480px;
text-align: left;
border-collapse: collapse;
}
.box-table-a th
{
font-size: 13px;
font-weight: normal;
padding: 8px;
background: #b9c9fe;
border-top: 4px solid #aabcfe;
border-bottom: 1px solid #fff;
color: #039;
}
.box-table-a td
{
padding: 8px;
background: #e8edff;
border-bottom: 1px solid #fff;
color: #669;
border-top: 1px solid transparent;
}
.box-table-a tr:hover td
{
background: #d0dafd;
color: #339;
}
.box-table-b
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 20px;
width: 300px;
text-align: left;
border-collapse: collapse;
}
.box-table-b th
{
font-size: 13px;
font-weight: normal;
padding: 8px;
background: #b9c9fe;
border-top: 4px solid #aabcfe;
border-bottom: 1px solid #fff;
color: #039;
}
.box-table-b td
{
padding: 8px;
background: #e8edff;
border-bottom: 1px solid #fff;
color: #669;
border-top: 1px solid transparent;
}
.box-table-b tr:hover td
{
background: #d0dafd;
color: #339;
}
.myAcctClass {
color:#FF4040;
}
/* Start by setting display:none to make this hidden.
Then we position it in relation to the viewport window
with position:fixed. Width, height, top and left speak
speak for themselves. Background we set to 80% white with
our animation centered, and no-repeating */
.modal {
display: none;
position: fixed;
z-index: 1000;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: rgba( 255, 255, 255, .8 )
url('assets/img/loading.gif')
50% 50%
no-repeat;
}
/* When the body has the loading class, we turn
the scrollbar off with overflow:hidden */
body.loading {
overflow: hidden;
}
/* Anytime the body has the loading class, our
modal element will be visible */
body.loading .modal {
display: block;
}
</style>
<script>
$body = $("body");
/* Table initialisation */
$(document).ready(function() {
regInputHandlers();
});
function regInputHandlers(){
$('#idSearch').change(function(){
doSearch();
});
$('#idSearchBtn').click(function(){
doSearch();
});
}
function doSearch(){
var searchString = $('#idSearch').val();
// Call the JS Remoting to get the Accounts
SearchAccountsCon.searchAccounts(searchString,'Account',function(result,event){
console.log(result);
myrecs = [];
recs = [];
var i=0;
var j=0;
var tbody = $('#accTable tbody');
$.each(result,function(r){
var tmp = [];
var tr = $('<tr>');
//Build the array based on the Fieldset Used.
console.log(result[r].isMyObject);
tmp.push(result[r].record.Id);
<apex:repeat value="{!$ObjectType.Account.FieldSets['AccountListViewFS']}" var="f">
if(typeof result[r].record['{!f}'] != 'undefined'){
tmp.push(result[r].record['{!f}']);
$('<td>').html(result[r].record['{!f}']).appendTo(tr);
}else{
tmp.push(' ');
$('<td>').html(' ').appendTo(tr);
}
</apex:repeat>
tbody.append(tr);
});
});
}
</script>
</head>
<body>
<apex:pageBlock>
<apex:pageBlockSection>
<input id="idSearch" maxlength="100" placeholder="Search..." size="30" title="Search..." type="text"/>
<input value="Search" id="idSearchBtn" type="button"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock title="Accounts View">
<table cellpadding="0" cellspacing="0" border="0" class="box-table-a" id="accTable">
<thead>
<tr>
<th>Id</th>
<apex:repeat value="{!$ObjectType.Account.FieldSets['AccountListViewFS']}" var="f">
<th>{!f.Label}</th>
</apex:repeat>
</tr>
</thead>
<tbody id="acctblBody">
</tbody>
</table>
</apex:pageBlock>
</body>
<div class="modal"><!-- Place at bottom of page --></div>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment