+ Start a Discussion
SFDC n12SFDC n12 

New case record creation on button click visualforce page

Hi,

I am having a following requirement for which i need help,

1) I am having a text box (comments) and followed by a button in my visualforce page

2) when i enter the value in the comments and click on the button a new case has to be created and the comments that i have entered should be mapped to the case and case subject is to be "Lead override" 



My controller :

public class AF_ScriptController {
  
    public String status {get;set;}
    public Lead_Object__c obj {get; set; }
 
   /**
   ** Constructor
   **/
   public AF_ScriptController (ApexPages.StandardController controller) {
 
      String newId = ApexPages.currentPage().getParameters().get('leadId');
      System.debug ('new Id *** :' + newId);

      if (newId != '') {
         obj = [Select Id,Lead_Override_Comments__c,First_Name__c,Last_Name__c,Dealer_Type__c,Products_Dealership__c,F_I_Manager__c,Contracts_Electronically__c,Dealer_Track_RouteOne__c,What_percent_of_inventory_has_a_selling__c,What_percent_of_your_inventory_has_less__c,What_percent_of_your_inventory_is_newer__c,How_many_front_line_ready_units_do_you_c__c,What_are_your_average_monthly_used_vehic__c,Service_Department_on_site__c,Dealership_Retail__c,Dealership_Permanent_Building__c,Dealership_Payed__c,How_many_years_have_you_been_in_business__c,Leadstatus__c, test__c, salutation__c  from Lead_Object__c where id = :newId limit 1];
        
         if (obj.LeadStatus__c == 'Qualified' ) {
            System.debug ('Qualified *** : ' + status);
            status = System.Label.AF_QualifiedScript;
         }
         else {
            status = System.Label.AF_UnqualifiedScript;
            System.debug ('UnQualified *** :' + status);
         }
        
      }

   }

}



Please help me how to do this

Thanks in Advance

Best Answer chosen by SFDC n12
Sumitkumar_ShingaviSumitkumar_Shingavi
You need to do below in apex:

public Case csInstance {get; set;}

public PageReference createCase() {

	try {
          csInstance.Subject = 'Lead override'; 
          csInstance.Lead__c = (Id)newId;
	  //Add additional mapping from Lead Object to Case here
	  insert cs; 
	  return new PageReference('/'+cs.Id);

	} catch(Exception e) {
	  System.debug(LoggingLevel.Error, 'Erorr = '+e.getMessage());
	}
}


Then inside your constructor do below:
csInstance = new Case();
in VF:

<apex:inputText value="{!csInstance.Description}" style="width: 270px; height: 100px;" required="true"/>
<apex:commandButton action="{!createCase}" value="Save" id="createCase"/>

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
Just add below code to your Apex class and VF

Apex Class Additional code:
private Case csInstance {get; set;} 
public PageReference createCase() {
	
	try { 
		csInstance = new Case();
		csInstance.Subject = 'Lead override';
		csInstance.Description = status;
		//Add additional mapping from Lead Object to Case here
		insert cs;
		
		return new PageReference('/'+cs.Id);
	
	} catch(Exception e) {
		System.debug(LoggingLevel.Error, 'Erorr = '+e.getMessage());
	}	
}
VF Additional Code:
<apex:commandButton action="{!createCase}" value="Save" id="createCase"/>

You can modify the above code to meet your requirement, I just posted it in order to give you idea on how you can do it! Hope this helps!

PS: if this answers your question then hit Like and mark it as solution!
SFDC n12SFDC n12
I have few clarifications  which i need ur help,

1) I want to populate the text that i enter in the comments field on my visualforce page to description of my case , right now its not coming , what should i use in the below component so that its getting stored in the description field of my case 

<b> <apex:inputText   style="width: 270px; height: 100px;" required="true"/></b> 


2) I am having a custom field in my lead object called as "leadobject__c" which captures all the values from a custom object (lead) to the standrad object lead

Note: I have a custom object lead where i am populating all my values in vf page and using a controller i am copying all the values to standard lead object again


Now the case is getting created , but its not assigned to the lead record that is getting created, i want the custom field " lead__c " on the "case" object to be filled in with the lead id that is getting created.

Help me how to achieve these 2 things alone pls

Thanks for ur help

Sumitkumar_ShingaviSumitkumar_Shingavi
You need to do below in apex:

public Case csInstance {get; set;}

public PageReference createCase() {

	try {
          csInstance.Subject = 'Lead override'; 
          csInstance.Lead__c = (Id)newId;
	  //Add additional mapping from Lead Object to Case here
	  insert cs; 
	  return new PageReference('/'+cs.Id);

	} catch(Exception e) {
	  System.debug(LoggingLevel.Error, 'Erorr = '+e.getMessage());
	}
}


Then inside your constructor do below:
csInstance = new Case();
in VF:

<apex:inputText value="{!csInstance.Description}" style="width: 270px; height: 100px;" required="true"/>
<apex:commandButton action="{!createCase}" value="Save" id="createCase"/>

This was selected as the best answer
SFDC n12SFDC n12
when i add the below in my controller,

csInstance.Lead__c = (Id)newId;

i am getting

Error: Compile Error: Variable does not exist: newId at line 40 column 32


and in my vf page when i add the bwlo statement , i am gettig the following error


<b> <apex:inputText value="{!csInstance.Description}"    style="width: 270px; height: 100px;" required="true"/></b>

Error: Invalid field csInstance for SObject Lead_Object__c