Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Sunil02kumar/309c2e72a64136073a3295aa2c88de35 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/309c2e72a64136073a3295aa2c88de35 to your computer and use it in GitHub Desktop.
Generating URL dynamically pre-populate custom fields values while creating new records
/*Now consider a scenario where we want to pre-populate below contact fields:
LastName
Email
Birthdate
Level__c (custom field)
Category__c(custom lookup field)
*/
string redirectUrl='';
String baseURL = Url.getSalesforceBaseUrl().toExternalForm();
String objectKeyPrefix = Schema.getGlobalDescribe().get('Contact').getDescribe().getKeyPrefix();
string bdate = EncodingUtil.urlEncode('25/05/1988', 'UTF-8' );
string lname = EncodingUtil.urlEncode('Sunil Kumar', 'UTF-8' );
string emailFieldvalue = EncodingUtil.urlEncode('[email protected]', 'UTF-8' );
Id CategoryFieldId = 'a079000000lA1aP';
string categoryFieldValue = 'test cat';
string TypeCustomFieldValue = EncodingUtil.urlEncode('New', 'UTF-8' );
//populating birthdate and last name
redirectUrl = baseURL + '/' + objectKeyPrefix + '/e?con7=' + bdate + '&name_lastcon2=' + lname ;
//populating email value
redirectUrl = redirectUrl + '&con15=' + emailFieldvalue;
//finding custom field ids
Map<string,string> fieldToIdmap= ToolingAPIUtility.findCustomFieldsid('Contact');
if(fieldToIdmap.get('Type__c') != null){
redirectUrl= redirectUrl + '&'+ fieldToIdmap.get('Type__c') + '=' + TypeCustomFieldValue;
}
//for custom lookup fieldId
if(fieldToIdmap.get('Category__c') != null){
redirectUrl= redirectUrl+ '&CF'+ fieldToIdmap.get('Category__c') + '=' + categoryFieldValue;
redirectUrl= redirectUrl + '&CF'+ fieldToIdmap.get('Category__c') + '_lkid=' + CategoryFieldId;
}
redirectUrl = redirectUrl + '&retURL=%2F'+ objectKeyPrefix + '%2Fo';
system.debug('*****redirectUrl:'+redirectUrl);
public class ToolingAPIUtility{
public static Map<string,string> findCustomFieldsid(string ObjName){
Map<string,string> FieldToFieldidMap = new Map<string,string>();
string customObjectId;
string TableEnumOrIdValue;
String baseURL = Url.getSalesforceBaseUrl().toExternalForm();
if(ObjName.endswith('__c')){
ObjName = ObjName.substring(0,Objname.length()-3);
system.debug('*******Objname:'+Objname);
String query = 'SELECT Id From CustomObject Where DeveloperName = \'' + ObjName+ '\'';
string endpoint = baseURL + '/services/data/v38.0/tooling/query/?q=';
endpoint += EncodingUtil.urlEncode(query, 'UTF-8');
Map<string, string> httpHeaders=new Map<string, string>();
httpHeaders.put('Authorization', 'Bearer ' + UserInfo.getSessionId());
httpHeaders.put('Content-Type', 'application/json');
HttpRequest request = createHTTPRequest(endpoint, 'GET', httpHeaders);
HttpResponse response = sendHTTPRequest(request);
system.debug('*****object**response:'+response);
customObjectId = response.getBody().substringAfter('"Id":"').substringBefore('"');
system.debug('*****object**customObjectId :'+customObjectId );
TableEnumOrIdValue = customObjectId ;
}else{
TableEnumOrIdValue = ObjName;
}
if(TableEnumOrIdValue !=null && TableEnumOrIdValue !=''){
String query = 'SELECT Id, DeveloperName From CustomField Where TableEnumOrId = \'' + TableEnumOrIdValue+ '\'';
string endpoint = baseURL + '/services/data/v38.0/tooling/query/?q=';
endpoint += EncodingUtil.urlEncode(query, 'UTF-8');
Map<string, string> httpHeaders=new Map<string, string>();
httpHeaders.put('Authorization', 'Bearer ' + UserInfo.getSessionId());
httpHeaders.put('Content-Type', 'application/json');
HttpRequest request = createHTTPRequest(endpoint, 'GET', httpHeaders);
HttpResponse res = sendHTTPRequest(request);
String response=res.getBody();
system.debug('*******response:'+response);
SYSTEM.JSONParser parser = SYSTEM.JSON.createParser(response);
while (parser.nextToken() != null) {
string fieldName='';
string fieldId='';
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
//system.debug('*********parser.getText():'+parser.getText());
if(parser.getText()=='Id'){
parser.nextToken();
fieldId= parser.getText();
system.debug('*************fieldId:'+fieldId);
parser.nextToken();
}if(parser.getText()=='DeveloperName'){
parser.nextToken();
fieldName=parser.getText();
}
if(fieldName!='' && fieldId !=''){
FieldToFieldidMap.put(fieldName+'__c',fieldId.substring(0,15));
}
}
}
system.debug('*********FieldToFieldidMap:'+FieldToFieldidMap);
}
return FieldToFieldidMap;
}
public static HTTPRequest createHTTPRequest(string endPointURL, string HTTPMethod, Map<string,string> HTTPHeaders){
HttpRequest req = new HttpRequest();
req.setEndpoint(endPointURL);
req.setMethod(HTTPMethod);
for(string ss : HTTPHeaders.keyset()){
req.setHeader(ss, HTTPHeaders.get(ss));
}
return req;
}
public static HTTPResponse sendHTTPRequest(HTTPRequest req){
HttpResponse res;
if(!SYSTEM.Test.isRunningTest()){
Http h = new Http();
res = h.send(req);
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment