Skip to content

Instantly share code, notes, and snippets.

@ykurnia
Created August 29, 2013 10:41
Show Gist options
  • Save ykurnia/6376604 to your computer and use it in GitHub Desktop.
Save ykurnia/6376604 to your computer and use it in GitHub Desktop.
This is Controller example how to override New button, and provide some default value for Name field and Record Type selection in salesforce.com
// transition page to create new page with default Name field
public class NewQuotation_Controller
{
public NewQuotation_Controller(ApexPages.StandardController controller)
{
//CreateNew();
} // constructor
// function to process
public Pagereference CreateNew()
{
String strObject = 'Quotation__c';
String strPrefix = Schema.getGlobalDescribe().get(strObject).getDescribe().getKeyPrefix();
System.debug('DEBUG: prefix ' + strPrefix);
// get record type
String strNewPage = '/' + strPrefix + '/e?Name=Autonumber&nooverride=1';
String idRecordType = ApexPages.CurrentPage().getParameters().get('RecordType');
if (idRecordType != null) strNewPage += '&RecordType=' + idRecordType;
Set<String> setParams = ApexPages.CurrentPage().getParameters().keySet();
String idOpp = '';
String strOppPrefix = '';
String[] arrTmp;
/*
for (String S: setParams)
{
if (S.contains('_lkid'))
{
idOpp = ApexPages.CurrentPage().getParameters().get(S);
arrTmp = S.split('_');
strOppPrefix = arrTmp[0];
}
// System.debug('DEBUG: S = ' + S);
}
if (idOpp != '') // search default opportunity, account and contact
{
for(Opportunity_Custom__c O: [SELECT Id, Name, Account__r.Name, Contact__r.Name FROM Opportunity_Custom__c tunity WHERE Id = :idOpp LIMIT 1])
{
idOpp = O.Name;
// TODO : find default for account and contact
}
strNewPage += '&' + strOppPrefix + '=' + idOpp;
}
*/
//System.debug('DEBUG: ' + strNewPage);
PageReference newPage = new PageReference(strNewPage);
return newPage;
} // function
// test method
public static testMethod void mySelfTest()
{
Quotation__c objQ = new Quotation__c();
ApexPages.StandardController tmpPage = new ApexPages.StandardController(objQ);
NewQuotation_Controller objC = new NewQuotation_Controller(tmpPage);
objC.CreateNew();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment