0

Why does this code give me an error?

Variable does not exist: incomingCase global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){

 List<EmailMessage> lstEmailMessage = new List<EmailMessage>();
 Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
 List<String> toAddresses = email.toAddresses;

 try{
     System.debug('Not able to get Inside');

     if(toAddresses.size()>0){
         System.debug('Inside the to Addresses.size');
         Case incomingCase = new Case();
         incomingCase.Description = email.plainTextBody;
         incomingCase.Subject = email.subject;
         incomingCase.Status= 'New';    
         incomingCase.RecordTypeId = caseComplaintRecordType;
         incomingCase.Origin ='Support Email';       
         incomingCase.ContactId = email.fromName;  

         if(email.subject.contains('Nothing')){

         }


  }

  Database.SaveResult[] srList = Database.insert(incomingCase, True);
2
  • Removing all your code from every question is inappropriate behavior and will not be tolerated.
    – Adrian Larson
    Commented Sep 21, 2017 at 21:25
  • Please stop modifying your questions in a way that makes the posted answers nonsense.
    – Adrian Larson
    Commented Sep 21, 2017 at 22:21

1 Answer 1

3

This is because you declare this inside if block. SO its scope is limited to if block and you are trying to access it outside if so declare it outside of if block

Case incomingCase = new Case();
  if(toAddresses.size()>0){

or you can move the insert inside if statement

if(toAddresses.size()>0){
         System.debug('Inside the to Addresses.size');
         Case incomingCase = new Case();
         incomingCase.Description = email.plainTextBody;
         incomingCase.Subject = email.subject;
         incomingCase.Status= 'New';    
         incomingCase.RecordTypeId = caseComplaintRecordType;
         incomingCase.Origin ='Support Email';       
         incomingCase.ContactId = email.fromName;  

         if(email.subject.contains('Nothing')){

         }
     Database.SaveResult srList = Database.insert(incomingCase, True);

  }

In first approach you need to make sure don't try to insert empty case record so it will be better if you move insert inside if block.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .