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

How to create and insert Case from VF Page using custom controller. -- pls Help...!!!

I am creating a public Web form in Visual Force for the site. The user is not logged in. When he fill the form and click the submit button,  the form will create the a contact and Account records (under Contact and Account Standard object), and create a case for that contact(under case standard object).


I am able to create the contact but not able to create and  insert the case for that contact.


The controller code I  am using is listed below:


public class TechnicalServiceController {
    public String companyName{set; get;}
    public String firstName{set; get;}
    public String lastName{set; get;}
    public String title{set; get;}
    public String phoneNumber{set; get;}
    public String emailId{set; get;}
    public String department{set; get;}
    public String firstAddress{set; get;}
    public String secondAddress{set; get;}
    public String cityOrTown{set; get;}
    public String state{set; get;}
    public String postalCode{set; get;}
    public String country{set; get;}
    public String typeOfRequest{set; get;}


 public PageReference saveRecords() {
       Contact contactToStore = new Contact();
           contactToStore.FirstName = firstName;
           contactToStore.LastName = lastName;
           contactToStore.Phone = phoneNumber;
           contactToStore.Email = emailId;
           contactToStore.MailingCountry = String.valueOf(country);
           insert contactToStore;

//new Object of type Account Standard Object    

 Account accountToCreate = new Account();
           accountToCreate.Name = companyName;
           insert accountToCreate;
      //new Object of type Case Standard Object
      Case caseToCreate = new Case();                     //don't know how to go onward from here. getting error here
            caseToCreate.Contact = contactToStore.lastName;    

            caseToCreate.Account = companyName;       
           return null;        
        catch(Exception e)
            return null;


For the code block ab ove:


Contact and Account are Lookupfields on Case record  but in my controller/webpage these are text fields and I am trying to store these text fields in the above 2 lookup fields

when I try to store them in the corrresponding fields on the case, it gives me following error message:
Error: Illegal assignment from String to SOBJECT

Is there anyone who can help me on it. I am not using Web to case because in that case I have to display
the fields from my case object on the web page which is not required as some fields are picklist on my webpage and they
are stored as text on case object record.


Also I need to insert the Contact and Case Related List on the Account Record; And Cased Related list in the Contact record for the corresponding Account, Contact , and Case  I create.



Is there anyone who can please help me on this...!!!!





Lets start from the  below line

Case caseToCreate = new Case();                     //don't know how to go onward from here. getting error here



caseToCreate.Contact = <contactToStore.lastName>;    

caseToCreate.Account = <companyName>;    

Expect IDs not names/strings.


SO you have to write as below ,


caseToCreate.Contact =;     

caseToCreate.Account =; 


Let me know if this does not work.









It worked in that way but before assigning the values to the lookup fields of Case I have to retrieve the data by quering it against Id. Anyway thanks for ur help.


I need to know one more thing. Before storing the Contact Record that the user enter in the fore, I have to check weather the Email entered by the user already exist. If it exist, then just case will be created and it will be linked with the existing Contact having the same Email I, the user entered. And If the  Email entered by the user doesn't exist, I have to createbothe the Contact and the Case. I have completed this second part, but don't know how to implement the first one.


Please help me on it...!!!