+ Start a Discussion
John Neilan 2John Neilan 2 

Pull Account Information Into Case Using VF Controller

Hello,

I am overriding my New Case button with a VF page. I am trying to pre-populate fields on Cases created with a certain record type with values from the Account (when the Case is created from the Account), but I'm not sure how to pull them into the controller. Can anyone help?

Controller:
//Controller to Override Case creation to allow for Custom VF pages for 1 record type but maintain standard pgs for all other cases
public with sharing class VF_Controller_CaseAcctSvcsNew{

public Case c1;

    public VF_Controller_CaseAcctSvcsNew(ApexPages.StandardController controller){
        this.c1 = (Case)controller.getRecord();
    }


    public PageReference CaseRedirect() {
        if(ApexPages.currentPage().getParameters().get('RecordType') == '012L0000000DPwQ'){
            DateTime d = Date.Today();
            c1.OwnerId='00GL0000001SUhm';
            c1.Use_Case__c = 'Ad Hoc';
            c1.Case_Reason__c = 'Screen';
            c1.Due_Date_AcctSvcs__c = System.now() + 24;
            c1.Status = 'New';
//            c1.Subject = 'Screen Request for ' + ApexPages.currentPage().getParameters().get('AccountId') + ' ' + d.format('MM/dd/yyyy');
            PageReference pageRef = new PageReference('/apex/VF_AcctSvcsNew');
            return pageRef;
        }
        else if(ApexPages.currentPage().getParameters().get('AccountId') == null){
            PageReference pageRef2 = new PageReference('https://cs8.salesforce.com/500/e?RecordType='+c1.RecordTypeId+'&ent=Case&nooverride=1');
            return pageRef2;
        }
        else{
            PageReference pageRef3 = new PageReference('https://cs8.salesforce.com/500/e?def_account_id='+c1.AccountId+'&RecordType='+c1.RecordTypeId+'&ent=Case&nooverride=1');
            return pageRef3;
        }
    }
}
Rohit Sharma 66Rohit Sharma 66
When u override the Case new button with VF page and click on "New case" button from the Account related list, you will get the account_Id in the URL. U just need to fetch that account_id from the url and ther query any number of fields from that account_id.
Try the following code:

public with sharing class caseOverrideController {
    
    public string accountId;
    public caseOverrideController(ApexPages.StandardController controller) {
       // def_account_id is the parameter u'll get in the URL having account Id
        accountId = ApexPages.currentPage().getParameters().get('def_account_id');
        system.debug('**********'+accountId ); // u''ll get the account id
    }
    
    public caseOverrideController(){
        
    }
    
    public void updateCaseRecord(){
        // query account records if the account id is not null
        if(accountId != null ){
            Account acc = [Select Id, Name from Account where Id =: accountId];
           // now u can do further processing here, update and case record fields from the Account data.
        }
    }
    
}

// Let me know if you find any issues.