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
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC) 

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:&nbsp;&nbsp;</b>'+Name+'</td>'; 
                  table = table + '</tr>';
                  table = table + '<tr>';
                  table = table + '<td width="25%" align="center"><b>ContactNo:&nbsp;&nbsp;&nbsp;</b>'+ContactNo+'</td>'; 
                  table = table + '</tr>';
                  table = table + '<tr>';
                 table = table + '<td width="25%" align="center"><b>Email Id:&nbsp;&nbsp;&nbsp;</b>'+Emailid+'</td>';
                 table = table + '</tr>';
                 table = table + '<tr>';
                  table = table + '<td width="25%" align="center"><b>Comment:&nbsp;&nbsp;&nbsp;&nbsp;</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;
   }
    }
Best Answer chosen by Gattam Rakesh (SFDC)
Chidambar ReddyChidambar Reddy
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;
        }
        else
        {
            
            // 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;
        }
        
        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:&nbsp;&nbsp;</b>'+Name+'</td>'; 
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>ContactNo:&nbsp;&nbsp;&nbsp;</b>'+ContactNo+'</td>'; 
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>Email Id:&nbsp;&nbsp;&nbsp;</b>'+Emailid+'</td>';
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>Comment:&nbsp;&nbsp;&nbsp;&nbsp;</b>'+Comment+'</td>'; 
        table = table + '</tr>';
        body=table;
        mail.setHTMLbody(body);      
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
        return null;
    }
}
This will work,

Thanks
Choose it as best answer if it has helped you

All Answers

Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
Iam Geeting a Error message as: CaseEmailClass Compile Error: Initial term of field expression must be a concrete SObject: LIST<Contact> at line 15 column 25
 
Chidambar ReddyChidambar Reddy
Hi Rakesh,

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.,
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
I have done But iam Getting same error 
{
   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;
 
Chidambar ReddyChidambar Reddy
Well, what is the cl here?

 
Chidambar ReddyChidambar Reddy
Replace 
Public list<Contact> cn{get;set;}
with 
 
public Contact cl {get; set; }


 
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
Sry its Cn 
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
Chidambar ReddyChidambar Reddy

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
Chidambar ReddyChidambar Reddy
Tell me the complete requirement
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
Thanks Chidambar it worked with out Trigger
Chidambar ReddyChidambar Reddy
Glad it helped :)
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
Chidambar its working now :-)
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:&nbsp;&nbsp;</b>'+Name+'</td>'; 
                  table = table + '</tr>';
                  table = table + '<tr>';
                  table = table + '<td width="25%" align="center"><b>ContactNo:&nbsp;&nbsp;&nbsp;</b>'+ContactNo+'</td>'; 
                  table = table + '</tr>';
                  table = table + '<tr>';
                 table = table + '<td width="25%" align="center"><b>Email Id:&nbsp;&nbsp;&nbsp;</b>'+Emailid+'</td>';
                 table = table + '</tr>';
                 table = table + '<tr>';
                  table = table + '<td width="25%" align="center"><b>Comment:&nbsp;&nbsp;&nbsp;&nbsp;</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;
   }
    }
    }
Chidambar ReddyChidambar Reddy
Could you please tell me the complete requirement?

Is it the visualforce page?
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
yes it is visualforce page
Chidambar ReddyChidambar Reddy
Hi Rakesh,

I am just trying to resolve your problem, as I have no idea on requirement, so the following code will work for you
 
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
        {
            
            // 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;
        }
        
        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:&nbsp;&nbsp;</b>'+Name+'</td>'; 
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>ContactNo:&nbsp;&nbsp;&nbsp;</b>'+ContactNo+'</td>'; 
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>Email Id:&nbsp;&nbsp;&nbsp;</b>'+Emailid+'</td>';
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>Comment:&nbsp;&nbsp;&nbsp;&nbsp;</b>'+Comment+'</td>'; 
        table = table + '</tr>';
        body=table;
        mail.setHTMLbody(body);      
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
    }
}

 
Chidambar ReddyChidambar Reddy
Sorry I fogot to remove 'return null;'

remove it from both if and else
Chidambar ReddyChidambar Reddy
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;
        }
        else
        {
            
            // 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;
        }
        
        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:&nbsp;&nbsp;</b>'+Name+'</td>'; 
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>ContactNo:&nbsp;&nbsp;&nbsp;</b>'+ContactNo+'</td>'; 
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>Email Id:&nbsp;&nbsp;&nbsp;</b>'+Emailid+'</td>';
        table = table + '</tr>';
        table = table + '<tr>';
        table = table + '<td width="25%" align="center"><b>Comment:&nbsp;&nbsp;&nbsp;&nbsp;</b>'+Comment+'</td>'; 
        table = table + '</tr>';
        body=table;
        mail.setHTMLbody(body);      
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
        return null;
    }
}
This will work,

Thanks
Choose it as best answer if it has helped you
This was selected as the best answer
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
Yes its working Thanks aLot :)
Chidambar ReddyChidambar Reddy
You are welcome!

It would be pretty good if you add 
  • Exception Handling
  • Apex Page Messages to display errors
  • Making the Email field Mandatory
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
Hai chidambar I have a small requirement
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
Chidambar ReddyChidambar Reddy
You can add this visualfroce page to force.com sites
 
Gattam Rakesh (SFDC)Gattam Rakesh (SFDC)
But case should be created from mail which we are getting for example if iam getting mail to support@xxxxxx.com from the mail a case should me created and a contact
Chidambar ReddyChidambar Reddy
Go to Setup > Build > Customize > Cases > Email-to-case

the other way in using InboundEmailHandler using Apex
rajesh reddy 3rajesh reddy 3
Chidambar Reddy can you please give me your Mail ID
Chidambar ReddyChidambar Reddy
I am confused, Are Rajesh and Rakesh same?

BTW, n.chiddu@gmail.com
rajesh reddy 3rajesh reddy 3
no this Rajesh thanks Bro i can send mail now