You need to sign in to do that
Don't have an account?

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>
You'll also want to add some error handling to your controller and visualforce as well.
Hope that helps!
hi James Loghry
Thank you for your reply. Its throughing an errror like "System.QueryException: List has no rows for assignment to SObject".
Please find the code below:
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