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 

Create Case with URL Parameters

Hi Everyone,

 

I think everyone is doing well.

 

Here is my code:

 

VisualForce Page:

 

<apex:page StandardController="Case" Extensions="ONEN_CTRL_WebToCase" action="{!submitCase}">
<apex:form >
<table>
<tr>
<th>Your Name:</th>
<td><apex:inputText value="{!Case.Account.Name}"/></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>

 

Apex Code:

 

public class ONEN_CTRL_WebToCase1 {
Public Account Account {get; set;}
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();
String LastName = ApexPages.currentPage().getParameters().get('Last_Name');
String Email = ApexPages.currentPage().getParameters().get('Email');
Account=New Account();
Contact=New Contact();
Cas=New Case();
If(Email==Contact.Email)
{

Cas.Account.name=Last_Name;
Cas.Contact.Email=Email;
}
insert Cas;
return null;
}
public ONEN_CTRL_WebToCase1(ApexPages.StandardController controller) {

}
}

 

Here my requirement is that if url parameter email='xxx@yyy.com' and Contact.Email='xxx@yyy.com' it must fetch Account.name and Contact.Email from contact and has to insert a record in the case.

 

Example

 

1. URL of the VFPage: \apex\VFPage?Last_Name='Anil'&Email='anil@talluri.com'

2. Contact.Email='anil@talluri.com'

 

3. In 2nd point, Contact.Email=Email

=> Fetch the account.name, contact.email from contact

 

4. Insert the record in case with the above account.name, contact.email

 

Please help me in implementing this. (I'm not at all satisfied with my code)

 

 

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

Hi,

 

In your code below

 

List<Contact> contactTemp = [Select email, Account.Name from contact where Email=:Email];

if(contactTemp.size()>0){
Cas.Account.Name=contactTemp.get(0).Account.Name;
Cas.Contact.Email=contactTemp.get(0).Email;

insert Cas;
}

 

You are inserting a Case, but you're referring to fields on Account and Contact, it doesn't work like this. It should be some field on Case where you'll assign this values. Ideally, you should be doing this:

 

List<Contact> contactTemp = [Select Id, AccountId from contact where Email=:Email];

if(contactTemp.size()>0){
Cas.AccountId = contactTemp.get(0).AccountId;
Cas.ContactId = contactTemp.get(0).Id;

insert Cas;
}

 

And yes, incase of multiple Contacts returned for the same email, this will consider only the first one.

All Answers

ManjunathManjunath

Hi,

 

Try this- The words marked in red should be case object field name.

One more thing, what if you have multiple records with same email in the contact object.( In my code I have made to fetch only the first rec that is shown in orange).

 

public PageReference submitCase() {
    Map<string,string> postParameters = ApexPages.currentPage().getParameters();
    String LastName = ApexPages.currentPage().getParameters().get('Last_Name');
    String Email = ApexPages.currentPage().getParameters().get('Email');
    Account=New Account();
    Contact=New Contact();
    
    List<Contact> contactTemp = [Select email, Account.Name from contact where Email=:Email];
    if(contactTemp.size()>0){
        Cas=New Case();
        Cas.fieldName=contactTemp.get(0).Account.Name;
        Cas.fieldName=contactTemp.get(0).Email;
        
        insert Cas;
    }
    return null;
}

AniltAnilt

Hi Manjunath,

 

Thanks buddy for helping me out in tough times.

 

I've followed your instructions...but getting below error..Please help me out from this as well which you usually do...I'm working on this from the past week.

 

"System.NullPointerException: Attempt to de-reference a null object

Error is in expression '{!submitCase}' in component <apex:page> in page createcase_url

 

 

Class.ONEN_CTRL_WebToCase.submitCase: line 18, column 1"

 

I know that we are pointing to Account, Contact Object which are null...but here I dont want to insert Account and Contact everytime, So do we have any other alternative for this.

 

Please help me regarding this as well.

 

FYI...copied the controller below:

 

public class ONEN_CTRL_WebToCase {
Public Account Account {get; set;}
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 submitCase() {
Map<string,string> postParameters = ApexPages.currentPage().getParameters();
String LastName = ApexPages.currentPage().getParameters().get('Last_Name');
String Email = ApexPages.currentPage().getParameters().get('Email');
Account=New Account();
Contact=New Contact();
List<Contact> contactTemp = [Select email, Account.Name from contact where Email=:Email];
if(contactTemp.size()>0){
Cas.Account.Name=contactTemp.get(0).Account.Name;
Cas.Contact.Email=contactTemp.get(0).Email;

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

}

 

vishal@forcevishal@force

Hi,

 

In your code below

 

List<Contact> contactTemp = [Select email, Account.Name from contact where Email=:Email];

if(contactTemp.size()>0){
Cas.Account.Name=contactTemp.get(0).Account.Name;
Cas.Contact.Email=contactTemp.get(0).Email;

insert Cas;
}

 

You are inserting a Case, but you're referring to fields on Account and Contact, it doesn't work like this. It should be some field on Case where you'll assign this values. Ideally, you should be doing this:

 

List<Contact> contactTemp = [Select Id, AccountId from contact where Email=:Email];

if(contactTemp.size()>0){
Cas.AccountId = contactTemp.get(0).AccountId;
Cas.ContactId = contactTemp.get(0).Id;

insert Cas;
}

 

And yes, incase of multiple Contacts returned for the same email, this will consider only the first one.

This was selected as the best answer
ManjunathManjunath

Hi,

 

Try the below code. (I have added one more line marked in orange and make sure you fill all the required field of the case object before inserting).

 

public PageReference submitCase() {
    Map<string,string> postParameters = ApexPages.currentPage().getParameters();
    String LastName = ApexPages.currentPage().getParameters().get('Last_

Name');
    String Email = ApexPages.currentPage().getParameters().get('Email');
    Account=New Account();
    Contact=New Contact();
    
    List<Contact> contactTemp= new List<contactTemp>();

    contactTemp = [Select email, Account.Name from contact where Email=:Email];
    if(contactTemp.size()>0){
        Cas=New Case();
        Cas.fieldName=contactTemp.get(0).Account.Name;
        Cas.fieldName=contactTemp.get(0).Email;
        
        insert Cas;
    }
    return null;
}

AniltAnilt

Hi Manjunath,

 

Thanks for your extended help from the beginning to the end, you have stood up like a supporting wall for me in this regard.

 

Vishal: Thank you very much for your help (but unfortunately its not ended up..see my 2 comments added at the last of this reply).

 

I've marked your solution as the best.

 

1. Add Cas=New Case(); in the 1st line of if condition (mentioned in code of your reply) which makes sense.

 

2. I've a doubt, when email is null => there might have chances of adding the case to other contact (ex: If Account Name='Anil' and Contact.Email=null......then as per the code it will retrieve the 1st contact which has contact.email with null value (suppose contact name=''xyz'), then case will be added to xyz rather than anil)..

 

Can we do something to add the case to perfect contact?

vishal@forcevishal@force

Hi,

 

As per the query, if there's no Contact with that email then it won't return any results. And in that scenario, the case won't be inserted.

 

So what is the behaviour expected here for all possible scenarios?

 

1. When only one contact matches email => we take that.

2. When more than one contact has the same email => ??

3. When no Contact has that email => ?? 

AniltAnilt

Hi Vishal,

 

When only one contact matches email => we take that [PERFECT].

When more than one contact has the same email => Dont have prob in creating the case for first contact.

When no Contact has that email => Dont have prob...as there is no contact so no case will be created.

 

My scenario is different... Whenever VFpage is refreshed...then the case will raise for the contact who doesnot have an email address.