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

A case and a contact should be created when user clicks on submit for a existing user directly a case should be crerates under his contact can any one please help me this
public with sharing class CaseEmailClass
{ Public Contact cc{get;set;}
Public list<Contact> cn{get;set;}
public String Comment { get; set; }
public String Emailid { get; set; }
public String ContactNo { get; set; }
public String Name { get; set; }
public PageReference submit()
{
cn=[SELECT Id,Email FROM Contact];
if(cn.Email==EmailId)
{
Case c=new Case();
c.Status='New';
c.ContactId=cn.id;
c.Priority='High';
c.Description=Comment;
c.Subject='A New Case Has Been Rasied';
insert c;
}
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'mail2gattam@gmail.com'});
mail.setSubject('A New Case Has Been Raised');
String table = '';
String body='';
/* table=table+'Name:'+Name+'<br/>';
table=table+'ContactNo:'+ContactNo+'<br/>';
table=table+'Email Id:'+Emailid+'<br/>';
table=table+'Comment:'+Comment+'<br/>'; */
table = table + '<b><table align="center" width="50%" border: 1px solid black;>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Name: </b>'+Name+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>ContactNo: </b>'+ContactNo+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Email Id: </b>'+Emailid+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Comment: </b>'+Comment+'</td>';
table = table + '</tr>';
body=table;
mail.setHTMLbody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
// return null;
Contact con= New Contact();
con.LastName=Name;
con.MobilePhone=ContactNo;
con.Email=EmailId;
insert con;
Case c=new Case();
c.Status='New';
c.ContactId=con.id;
c.Priority='High';
c.Description=Comment;
c.Subject='A New Case Has Been Rasied';
insert c;
return null;
}
}
{ Public Contact cc{get;set;}
Public list<Contact> cn{get;set;}
public String Comment { get; set; }
public String Emailid { get; set; }
public String ContactNo { get; set; }
public String Name { get; set; }
public PageReference submit()
{
cn=[SELECT Id,Email FROM Contact];
if(cn.Email==EmailId)
{
Case c=new Case();
c.Status='New';
c.ContactId=cn.id;
c.Priority='High';
c.Description=Comment;
c.Subject='A New Case Has Been Rasied';
insert c;
}
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'mail2gattam@gmail.com'});
mail.setSubject('A New Case Has Been Raised');
String table = '';
String body='';
/* table=table+'Name:'+Name+'<br/>';
table=table+'ContactNo:'+ContactNo+'<br/>';
table=table+'Email Id:'+Emailid+'<br/>';
table=table+'Comment:'+Comment+'<br/>'; */
table = table + '<b><table align="center" width="50%" border: 1px solid black;>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Name: </b>'+Name+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>ContactNo: </b>'+ContactNo+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Email Id: </b>'+Emailid+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Comment: </b>'+Comment+'</td>';
table = table + '</tr>';
body=table;
mail.setHTMLbody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
// return null;
Contact con= New Contact();
con.LastName=Name;
con.MobilePhone=ContactNo;
con.Email=EmailId;
insert con;
Case c=new Case();
c.Status='New';
c.ContactId=con.id;
c.Priority='High';
c.Description=Comment;
c.Subject='A New Case Has Been Rasied';
insert c;
return null;
}
}
Thanks
Choose it as best answer if it has helped you
All Answers
you are trying to access List<Contact> as a single contact, you either use a single contact and query using LIMIT 1
or use index of the list to access the single contact
for ex use cn[0].email, cn[0].Id etc.,
{
cl=[SELECT Id,Email FROM Contact LIMIT 1];
if(cl.Email==EmailId)
{
Case c=new Case();
c.Status='New';
c.ContactId=cl.id;
c.Priority='High';
c.Description=Comment;
c.Subject='A New Case Has Been Rasied';
insert c;
The main Requirment is A case should be created as well as contact
1st functionlity is working and second requiremnet is if the user has entering the case for second time based on his email adress a under his contact another case should be created
then you should query the contact based on user's email address
and you should handle this in the trigger, calling a different method for update
here is my Complete Code:
public with sharing class CaseEmailClass
{
Public Contact cc{get;set;}
Public list<Contact> cn{get;set;}
public String Comment { get; set; }
public String Emailid { get; set; }
public String ContactNo { get; set; }
public String Name { get; set; }
public PageReference submit()
{
cn=[SELECT Id,Email FROM Contact WHERE Email=:EmailId LIMIT 1];
if(cn.size()>0)
{
Case c=new Case();
c.Status='New';
c.ContactId=cn[0].id;
c.Priority='High';
c.Description=Comment;
c.Subject='A New Case Has Been Rasied';
insert c;
return null;
}
else
{
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'mail2gattam@gmail.com'});
mail.setSubject('A New Case Has Been Raised');
String table = '';
String body='';
table = table + '<b><table align="center" width="50%" border: 1px solid black;>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Name: </b>'+Name+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>ContactNo: </b>'+ContactNo+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Email Id: </b>'+Emailid+'</td>';
table = table + '</tr>';
table = table + '<tr>';
table = table + '<td width="25%" align="center"><b>Comment: </b>'+Comment+'</td>';
table = table + '</tr>';
body=table;
mail.setHTMLbody(body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
// return null;
Contact con= New Contact();
con.LastName=Name;
con.MobilePhone=ContactNo;
con.Email=EmailId;
insert con;
Case c=new Case();
c.Status='New';
c.ContactId=con.id;
c.Priority='High';
c.Description=Comment;
c.Subject='A New Case Has Been Rasied';
insert c;
return null;
}
}
}
Is it the visualforce page?
I am just trying to resolve your problem, as I have no idea on requirement, so the following code will work for you
remove it from both if and else
Thanks
Choose it as best answer if it has helped you
It would be pretty good if you add
If a get a Mail on submiting form in online automatically a case and a contact should be created.
I need to use custom Functionality
the other way in using InboundEmailHandler using Apex
BTW, n.chiddu@gmail.com