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
AniltAnilt 

Case Record Creation With SOQL

Hi Everyone,

 

I want to insert a record in Case with fields Name, Email.

 

I'm confused how to write SOQL query for retrieving the account name (=case name) and Contact Email (=case email).

 

Please help me in writing a SOQL query for this.

 

 

Mohith Kumar ShrivastavaMohith Kumar Shrivastava
Select c.Contact.Email, c.Contact.AccountId, c.Contact.Id, c.ContactId From Case c;

This will get you the Contact Email from Case and also Account Id .

Now you will need to use this Account Id and query for Account information.

 Let me know if you need more explanation on this .

ManjunathManjunath

Hi,

 

Are you Creating case using trigger?

What is the senario.

 

Regards,

Manjunath

AniltAnilt

Hi Manjunath,

 

Thanks for your response.

 

Here is my code:

 

<apex:page StandardController="Case" Extensions="ONEN_CTRL_WebToCase" action="{!submitCase}">
<apex:form >
<table>
<tr>
<th>Your Name:</th>
<td><apex:inputText value="{!Case.Contact.LastName}"/></td>
</tr>
<tr>
<th>Your Email:</th>
<td><apex:inputText value="{!Case.Contact.Email}"/></td>
</tr>
<tr>
<td align="right">
<apex:outputLabel value="Reason for Contact" for="txtPostalCode" styleClass="label" /><br/>
</td>
<td>
<apex:selectlist value="{!Case.Description}" id="txtPostalCode" styleclass="select">
<apex:selectOptions value="{!reason4Contact}"/>
</apex:selectlist>
</td>
</tr>
<tr>
<td><apex:commandButton value="Save" action="{!save}"/></td>
</tr>
</table>
</apex:form>

</apex:page>

 

public class ONEN_CTRL_WebToCase {
Public Contact Contact {get; set;}
Public Case cas {get; set;}
public List<SelectOption> getReason4Contact() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Select a Topic','Select a Topic'));
return options;
}
public PageReference save() {
return null;
}
public PageReference submitCase() {
Map<string,string> postParameters = ApexPages.currentPage().getParameters();
Contact=new Contact();
if (!postParameters.isEmpty()) {
String reason4Con = ApexPages.currentPage().getParameters().get('reason4Con');
if (ApexPages.currentPage().getParameters().get('Last_Name')==null || ApexPages.currentPage().getParameters().get('Last_Name')=='') {
Contact.LastName = '[not provided]';
} else {
Contact.LastName = ApexPages.currentPage().getParameters().get('Last_Name');
}
Contact.Email = ApexPages.currentPage().getParameters().get('Email');
Insert Contact;
}
return null;
}
public ONEN_CTRL_WebToCase(ApexPages.StandardController controller) {

}
}

 

Here, I'm able to insert the Contact.

 

So here my scenario is to create the case by providing field values of case in url.

 

like ex: '/apex/CreateCase_URL' is the url of my VF Page

 

So I'll insert a record in case by providing values like '/apex/CreateCase_URL/Last_Name=xxxxx&Email=yyyyyyy' which inserts the record in case with those values (but here happening is that I'm able to insert only contact not the actual case object record).

 

Can you please let me know where am I doing wrong.

AniltAnilt

Thanks for your quick reply. I'll go through this and let you know for any further assistance.

ManjunathManjunath

Hi,

 

Your code will execute every time url is entered. (Am not sure you are doing that.).

I believe that you have add name and email  custom field in your case object .


But I modified your code as follows. Just give the contact id in your URL

Ex : /apex/CreateCase_URL?cid='Your Contact rec id'

 

Controller Code goes as follows  

 
public class ONEN_CTRL_WebToCase {
Public Contact Contact {get; set;}
Public Case cas {get; set;}
public List getReason4Contact() {
List options = new List();
options.add(new SelectOption('Select a Topic','Select a Topic'));
return options;
}
public PageReference save() {
return null;
}
public PageReference submitCase() {
Cas = new Case();
id Contid=ApexPages.currentPage().getParameters().get('cid');
Contact C = [Select Account.Name ,Email from contact where id=:contid];
Cas=C.Account.Name;
Cas=C.Email;
insert Cas;

return null;
}
public ONEN_CTRL_WebToCase(ApexPages.StandardController controller) {

}
}

 

 

Regards,

Manjunath

AniltAnilt

Hi Manjunath,

 

Thanks for your help. but as we are integrating the case fields with the client's html fields..we have no choice to go in another way rather than our way.

 

Please suggest whether it is possible to implement the things in my way, as I know I'm very close to the solution (I'm able to insert the contact record and not able to insert the case record).

 

I'm sorry to tell you the above things.

jd123jd123

Hi 

you are passing Contact Name and Email in the Url but where you are getting and inserting these values.

i didn't see can you post /apex/CreateCase_URL page code.

ManjunathManjunath

Hi,

 

Still am no clear with your requirement.

 

Based on my understanding.

Create a case object in your submitCase() function insert the Last_name and email to the case object as you do for contact (Fill all the required fields of the case object).

 

 public PageReference submitCase()
{
    Map<string,string> postParameters = ApexPages.currentPage().getParameters();
    Contact=new Contact();

    cas=new Case();
    if (!postParameters.isEmpty())
    { String reason4Con = ApexPages.currentPage().getParameters().get('reason4Con');
        if (ApexPages.currentPage().getParameters().get('Last_Name')==null || ApexPages.currentPage().getParameters().get('Last_Name')=='')
        {
            Contact.LastName = '[not provided]';

            cas.LastName='[not provided]';
        }
        else {
            Contact.LastName = ApexPages.currentPage().getParameters().get('Last_Name');
            cas.LastName= ApexPages.currentPage().getParameters().get('Last_Name');
        }
        Contact.Email = ApexPages.currentPage().getParameters().get('Email');
        cas.Email= ApexPages.currentPage().getParameters().get('Email');
        Insert Contact;
        insert cas;
    } return null;
}

 

Regards,

Manjunath

AniltAnilt

Hi Mahender,

 

Please go through my code pasted in the above comments...!

 

@Manjunath: My client webpage has 3 fields (2 text fields, 1 picklist) which must be redirect to the Case fields (LastName, Email, Description).

 

 

 

Please go through my answers to your red marked questions.

 

 cas=new Case(); //To have an Id of a case object.

 

cas.LastName='[not provided]'; // If customer has not entered text in this field..as it is a manditory field, it will error out.

else

cas.LastName= ApexPages.currentPage().getParameters().get('Last_Name'); //If Customer entered this..It will get the values from url of the current page.

 

cas.Email= ApexPages.currentPage().getParameters().get('Email'); //same as the Last_Name...but it is not manditory.

jd123jd123

HI

Have you written below line.

 

Insert cas;

 

if you written even thought the case is not inserting then go and check it in the Developer Console. 

ManjunathManjunath

Hi,

 

Those red lines are not question. Those are the things you need to do in your controller (make sure you add data to all the required fields of the case).

 

Regards,

Manjunath.