Skip to content

Instantly share code, notes, and snippets.

@sohalloran
Created November 21, 2013 00:12
Show Gist options
  • Save sohalloran/7573668 to your computer and use it in GitHub Desktop.
Save sohalloran/7573668 to your computer and use it in GitHub Desktop.
Example Search Before Create Apex Controller. This is a sample implementation which interacts with a Transaction Hub which have 'search' and 'publish' REST APIs which return the results in JSON format.
public without sharing class searchAccounts {
public String name { get; set; }
public String op { get; set; }
public String mdId { get; set; }
public String accId {get; set;}
public Account acc {get; set;}
public List<Acc> accs { get; set; }
// Hub Account Object
public class Acc {
public String name {get; set;}
public String Id {get; set;}
public String MDId {get; set;}
public String BillingCity {get; set;}
public String BillingStreet {get; set;}
public String AccountNumber {get; set;}
}
public searchAccounts(ApexPages.StandardController controller) {
acc = (Account)controller.getRecord();
accId = acc.Id;
name=Apexpages.currentPage().getParameters().get('acc2');
if(accId==null) {
accId=Apexpages.currentPage().getParameters().get('acc1');
}
if(name==null && accId!=null) {
name = [select name from account where id=:accId][0].name;
}
}
public searchAccounts() {
accs = new List<Acc>();
}
//##############################
// WS Callout to Search Account in MDM System
//##############################
public List<Acc> getAccounts() {
if (name==null) return null;
accs = new List<Acc>();
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint(System.Label.MDM_URL + '/api/find?name='+EncodingUtil.urlEncode(name,'UTF-8'));
request.setMethod('GET');
// Make callout
HttpResponse response = httpProtocol.send(request);
try {
if(response!=null && response.getBody()!=null) {
// Parse JSON Results to a List of objects
List<Object> m = (List<Object>)JSON.deserializeUntyped(response.getBody());
for(Object k:m) {
Map<String,Object> o = (Map<String,Object>)k;
Acc acc = new Acc();
acc.Id=(String)o.get('Id');
acc.MdId=(String)o.get('_id');
acc.name=(String)o.get('Name');
acc.accountNumber=(String)o.get('AccountNumber');
acc.billingStreet=(String)o.get('BillingStreet');
acc.billingCity=(String)o.get('BillingCity');
accs.add(acc);
}
}
} catch (Exception e) {
system.debug(e);
}
return accs;
}
//#################################
//##############################
// WS Callout to Publish an Account from the MDM System
//##############################
public PageReference addAccount() {
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
String endpoint = System.Label.MDM_URL + '/publish/'+Apexpages.currentPage().getParameters().get('mdId');
request.setEndPoint(endpoint);
request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
return null;
}
//#################################
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment