+ Start a Discussion
TheLearnerTheLearner 

Urgent--Test class

Hi Experts,

Could anyone write test clas for this class please.Thansk in advance

global class S2S_Notes_Inbound_Email_Handler implements Messaging.InboundEmailHandler {
    /**
    handleInboundEmail() -  Processes the email & extracts body, subject & Attachments.
    **/    
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
      Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
      
      //result.success = true;
      //result.message = 'Your request has been processed.';
          //return result; 
          Map<String, String> allObjectsMap = new Map<String,String>{'Accreditation'=>'Accreditation__c','SCCAccreditations'=>'SCC_Accreditations__c','SCCClient'=>'SCC_Clients__c'
            ,'SCCS14'=>'SCC_C34__c','SCCSentEmailHistory'=>'SCC_Sent_Email_History__c','SubcontractorClient'=>'Subcontractor_Client__c','SubcontractorPerformanceReview'=>'','SubContractRegister'=>'SCC__c','SubcontractorAudits'=>'Subcontractor_Audits__c'};
          try{
            All_ConnectionHelpler.batchRunning = true;
      String body = email.plainTextBody;
      String subject = email.subject;
            
          List<String> lstSub = subject.split('#@#',-5);
           
          String recordname = lstSub[1];
          String objectName = lstSub[2];
          String title = lstSub[3];

          if(allObjectsMap.keySet().contains(objectName))
            objectName = allObjectsMap.get(objectName);
            
          String query = 'select id from '+objectName+' where name =\''+recordname+'\'';
            
          sObject s = Database.query(query);
            
          Note note = new Note(parentid=s.id, Title = title,body = body);
            
          Note[] noteExistings = [select id from Note where parentid=:s.id and Title = :Title];
          if(noteExistings.size()>0){
            note = noteExistings[0];
            note.body=body;
            //note.id = noteExistings[0].id;
          }
            
      upsert note;
          }catch(Exception e){
      //result.success = false;
      //result.message = 'Error'+e.getMessage();
      return result;            
          }
         //result.success = false;
      //result.message = 'YES'+note.id;   
          return result;
      }
  }

 
Best Answer chosen by TheLearner
ManojjenaManojjena
Hi ,

You need to type cast like below .As sObject is generic you can not assign any query result to that .
One more thing your query will return a list not asingle record .

You ca try below code in  console and it will help you  for sure .
 
String query='SELECT id ,Name FROM Account';
 List<sObject> slist= (Account[])Database.query(query);
 System.debug('***********'+slist.size());
Let me know if it helps .

 

All Answers

ManojjenaManojjena
  Hi ,

Check below link it will help ,Let me know if it helps .

https://developer.salesforce.com/forums/?id=906F000000095d2IAA
TheLearnerTheLearner
Hi Manoj, 
Im little confused could you write sample test class for my code please, so that with the help of that i can write next test class when ever i got such kind of requirment.its humble request please
 
David Holland 6David Holland 6
Hi TheLearner

The link provided by Manoj gives you the knowledge you will need to write the test class yourself.

As all classes are different and have different methods etc, there is no point someone writing you a test class so you can then "write the next one".

Please follow the link and try and write yourself.
TheLearnerTheLearner
Hi Manoj,


actually query is not coming into the sobject s. could you tell me the soluation for this please

 sObject s = Database.query(query);
              
            Note note = new Note(parentid=s.id, Title = title,body = body);
              
            Note[] noteExistings = [select id from Note where parentid=:s.id and Title = :Title];
            if(noteExistings.size()>0)
            {
                note = noteExistings[0];
                note.body=body;
                //note.id = noteExistings[0].id;
            }
              
            upsert note;
          }catch(Exception e){
            //result.success = false;
            //result.message = 'Error'+e.getMessage();
            return result;              
          }
           //result.success = false;
            //result.message = 'YES'+note.id;   
          return result;
      }
  }
 
 
ManojjenaManojjena
Hi ,

You need to type cast like below .As sObject is generic you can not assign any query result to that .
One more thing your query will return a list not asingle record .

You ca try below code in  console and it will help you  for sure .
 
String query='SELECT id ,Name FROM Account';
 List<sObject> slist= (Account[])Database.query(query);
 System.debug('***********'+slist.size());
Let me know if it helps .

 
This was selected as the best answer
TheLearnerTheLearner
HI Manoj,

getting this error to me : "[Error] Error: Compile Error: Initial term of field expression must be a concrete SObject: List<SObject> at line 45 column 43"

could you help me please
TheLearnerTheLearner
i assigned like this "  list <sObject> s =(HS_Evaluation__c[])Database.query(query);"
 i got this error"
[Error] Error: Compile Error: Initial term of field expression must be a concrete SObject: List<SObject> at line 45 column 43"