Created
August 29, 2013 10:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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