function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashok0572Ashok0572 

visualforce - Prepopulate field with default value on pressing NewQuote Button...

The below is action method in Controller
Sold_To__c {Lookup(Account)} & Channel_Partner__c {Lookup(Account)} are 2 fields and i need to get predefault Sold_To__c ,Channel_Partner__c values


public PageReference appendSaveURL()
   {
        PageReference pageRef;
        String tempURL = String.valueof(ApexPages.currentPage());
        system.debug('****tempURL***'+tempURL);
        string finalURL = URL.getSalesforceBaseUrl().toExternalForm() + (URL.getSalesforceBaseUrl().toExternalForm().contains('visual.force') ? '' : '/partner') +'/0Q0/e';
        //string finalURL = '/0Q0/e';
        if(tempURL.contains('?'))
        {  try
            {
            tempURL = tempURL.replace('?','!@#');
            String[] paramArray = tempURL.split('!@#');
            String saveURL = paramArray[1];
            system.debug('*****saveURL****'+saveURL);
            if(tempURL.contains('save_new'))
            {
                String[] tempArray = saveURL.split('save_new');
                for(integer i=0;i<tempArray.size();i++)
                    finalURL = finalURL +'?'+tempArray[i];
                finalURL = finalURL +'&nooverride=1&saveURL=' + (URL.getSalesforceBaseUrl().toExternalForm().contains('visual.force') ? '' : '/partner') + '/apex/DeleteQuoteLineItems';
            }
            else
                finalURL = finalURL +'?'+saveURL+'&nooverride=1&saveURL='+ (URL.getSalesforceBaseUrl().toExternalForm().contains('visual.force') ? '' : '/partner') + '/apex/DeleteQuoteLineItems';
            }
            catch(exception ex)
            {
                finalURL = finalURL +'?nooverride=1&saveURL='+ (URL.getSalesforceBaseUrl().toExternalForm().contains('visual.force') ? '' : '/partner') + '/apex/DeleteQuoteLineItems';
            }
        }
        else
        {
            finalURL = finalURL +'?nooverride=1&saveURL='+ (URL.getSalesforceBaseUrl().toExternalForm().contains('visual.force') ? '' : '/partner') + '/apex/DeleteQuoteLineItems';
        }
        system.debug('****finalURL****'+finalURL );
        Map<String,QuoteFieldIds__c> quoteIds = QuoteFieldIds__c.getAll(); // Custom Setting to store the Field id of Contact
        String accountId = 'CF';
        if(quoteIds != null && quoteIds.size() > 0 && quoteIds.containsKey('ContactName')){
            string contactId=quoteIds.get('ContactName').Field_Id__c;
           
            //get Primary AccountContactRole of Quote's Opportunity
            list<AccountContactRole> lstAccountCR=[SELECT ContactId,contact.name FROM AccountContactRole where IsPrimary=true and accountid in (select  accountid from opportunity where id =:ApexPages.currentPage().getParameters().get('oppid'))];
            if(lstAccountCR != null && lstAccountCR.size() > 0){
                finalURL=finalURL + '&' + contactId + '=' + lstAccountCR[0].contact.name + '&' + contactId + '_lkid=' + lstAccountCR[0].ContactId; 
            }
        }
        
        //code Added by Dilip ape TA577 on date 06/04/2014
        List<Opportunity> oppList = new List<Opportunity>();
        oppList= [ select Organization__c,name, accountid, Account.Name,Account.BillingCountry from opportunity where id =:ApexPages.currentPage().getParameters().get('oppid')];
        if(oppList!=null && oppList.size()>0)
        {
            finalURL=finalURL + +'&Name='+EncodingUtil.urlEncode(oppList[0].name, 'UTF-8');
            if(quoteIds != null && quoteIds.size() > 0 && quoteIds.containsKey('AccountId')){
                accountId += quoteIds.get('AccountId').Field_Id__c;
                finalURL=finalURL + '&' + accountId + '=' + EncodingUtil.urlEncode(oppList[0].Account.Name , 'UTF-8') + '&' + accountId + '_lkid=' + oppList[0].AccountId;                        
            }
        }
        //**TA513 added by Dilip on 15/10/2014
        if(quoteIds != null && quoteIds.size() > 0 && quoteIds.containsKey('Payment_Terms') && oppList!=null && oppList.size()>0)
        {  
            List<Sales_Area_Data__c> paymentTermsList = new List<Sales_Area_Data__c>();
            paymentTermsList = [select Payment_Terms__r.Name,Payment_Terms__c from Sales_Area_Data__c where Account__c =:oppList[0].AccountId and Organization__c =: oppList[0].Organization__c limit 1];
            system.debug('****paymentTermsList***'+paymentTermsList); 
            if(paymentTermsList !=null && paymentTermsList.size()>0 && paymentTermsList[0].Payment_Terms__c!=null)
            {
                finalURL=finalURL+'&' + quoteIds.get('Payment_Terms').Field_Id__c + '='+ paymentTermsList[0].Payment_Terms__r.Name+ '&' +quoteIds.get('Payment_Terms').Field_Id__c+ '_lkid=' +paymentTermsList[0].Payment_Terms__c;
            }
        }
        //**TA604
        Date dt= system.today();
        Map<String,String> countryNumberOfDaysMap = new Map<String,String>();
        List<InterfaceCodeConversion__c> countryNumberOfDays =[select key__c,Value__c from InterfaceCodeConversion__c where Field_Name__c='ExpirationDate' and Object_Name__c='Quote'];
        for(InterfaceCodeConversion__c codeCov:countryNumberOfDays)
        {
            countryNumberOfDaysMap.put(codeCov.key__c,codeCov.Value__c);
        }
        Date newDate;
        if(oppList[0].Account.BillingCountry!=null && countryNumberOfDaysMap.containsKey(oppList[0].Account.BillingCountry))
        {
            newDate=dt.addDays(Integer.valueOf(countryNumberOfDaysMap.get(oppList[0].Account.BillingCountry)));
        }
        else
        {
            newDate=dt.addMonths(1);
        }
        
        //system.debug('****dt***'+dt); 
        
        //system.debug('****dt***'+newDate); 
        //system.debug('****dt.format()***'+newDate.format()); 
        //finalURL=finalURL+'&ExpirationDate='+dt.day()+'/'+dt.month()+'/'+dt.year();
        finalURL=finalURL+'&ExpirationDate='+newDate.format()+'&'+quoteIds.get('Account__c').Field_Id__c+ '=' + EncodingUtil.urlEncode(oppList[0].Account.Name , 'UTF-8') + '&' + quoteIds.get('Account__c').Field_Id__c+ '_lkid=' + oppList[0].AccountId;
        system.debug('****'+finalURL);
        pageRef = new PageReference(finalURL);
        pageRef.getParameters().put('oppid',ApexPages.currentPage().getParameters().get('oppid'));
        system.debug('****pageRef***'+pageRef); 
        return pageRef;
   }