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);
  • Removing all your code from every question is inappropriate behavior and will not be tolerated. – Adrian Larson Sep 21 '17 at 21:25
  • Please stop modifying your questions in a way that makes the posted answers nonsense. – Adrian Larson Sep 21 '17 at 22:21
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.