+ Start a Discussion
shobana shobana 1shobana shobana 1 

Creating a case by custom button

My scenario is to create a Custom button in Account object while clicking that button it show a form which has   Case field and i have  created two custom fields in Case  Account_Name__c(reads the value of Name from Account),Account_Type__C(reads the value of Type from Account) then I want to add submit custom button  in that form after clicking that it will create a case record for a related account.

 Actually Case is creating but i want to update the Account_Name__c(reads the value of Name from Account),Account_Type__C(reads the value of Type from Account) before case is created which means i want to see those data in a form itself. But for me after creating case only it showing the Account_Name__c,Account_Type__C
 

public class CreateCaseController {
public Case objCase { get; set; }
public Id AId;  
public Account c { get; set; }
public CreateCaseController() {
AId = ApexPages.currentPage().getParameters().get('id');
objCase = NEW Case();
}
public PageReference createCase() {
c =[SELECT Name,Id,Type
                    FROM Account WHERE Id =: AId];
objCase.AccountId = AId;
objCase.Account_Name__c = c.Name;
objCase.Account_Type__c = c.Type;
insert objCase;
PageReference pRef = NEW PageReference('/'+objCase.Id);
pRef.setRedirect(TRUE);
return pRef;
    }
}

VF Page: 

<apex:page controller="CreateCaseController">
<apex:form >
<apex:PageBlock title="Create Case" >
<apex:pageBlockSection columns="2">
<apex:inputField value="{!objCase.Account_Name__c}" />
<apex:inputField value="{!objCase.Status}"/>
<apex:inputField value="{!objCase.Origin}"/>
<apex:inputField value="{!objCase.Account_Type__c}"/>
<apex:inputField value="{!objCase.Type}"/>
<apex:inputField value="{!objCase.Reason}"/>
<apex:inputField value="{!objCase.ContactId}"/>
<apex:inputField value="{!objCase.Description}"/>               
</apex:pageBlockSection>
<apex:pageBlockButtons location="Bottom" >
<apex:commandButton value="Submit" action="{!createCase}" />
</apex:pageBlockButtons>
</apex:PageBlock>
</apex:form>
</apex:page>
James LoghryJames Loghry
Move lines 12-14 from your createCase method and stick them in the constructor instead.  See my example below.  However, why not just have them as formula fields instead?  Don't think you would want users modifying / updating those fields, unless you have a special use case for it.
 
public class CreateCaseController {
    public Case objCase { get; set; }
    public Id AId;  
    public Account c { get; set; }
    public CreateCaseController() {
        AId = ApexPages.currentPage().getParameters().get('id');
        objCase = NEW Case();

        c = [SELECT Name,Id,Type
                    FROM Account WHERE Id =: AId];
        objCase.AccountId = AId;
        objCase.Account_Name__c = c.Name;
        objCase.Account_Type__c = c.Type;
    }

    public PageReference createCase() {
        insert objCase;
        PageReference pRef = NEW PageReference('/'+objCase.Id);
        pRef.setRedirect(TRUE);
        return pRef;
    }
}

You'll also want to add some error handling to your controller and visualforce as well.

Hope that helps!
shobana shobana 1shobana shobana 1

hi James Loghry

Thank you for your reply. Its throughing an errror like "System.QueryException: List has no rows for assignment to SObject".
 

Mahesh DMahesh D
Hi Shobana,

Please find the code below:
 
public class CreateCaseController {
    public Case caObj { get; set; }
    public Id accId;  
    public Account acc { get; set; }
    public CreateCaseController() {
        accId = ApexPages.currentPage().getParameters().get('id');
        caObj = new Case();
		
		if(accId != null && accId != '') {
			acc = [SELECT Id, Name, Type FROM Account WHERE Id =: accId];
			caObj.AccountId = accId;
			caObj.Account_Name__c = acc.Name;
			caObj.Account_Type__c = acc.Type;
		}
    }

    public PageReference createCase() {
        insert caObj;
        PageReference pRef = NEW PageReference('/'+caObj.Id);
        pRef.setRedirect(TRUE);
        return pRef;
    }
}

Please make sure that you are passing the id while opening the corresponding VF page.

Please do let me know if it helps you.

Regards,
Mahesh