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
DevmenDevmen 

system.assertion failed

hi,

i have a test class.It is throwing error System.Assertexception
Lead newLead = new Lead();
        newLead.LastName='TestName';
        newLead.Company = 'TestCompany';
        newLead.Email='test@test.com';
        newLead.Status ='Open';
        newLead.Cc__c='test@test.com';
        newLead.Description='Subject:TextBody';
        insert newlead;
          
      Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

        email.plainTextBody = 'teset';
        email.fromAddress = 'test@test.com';
        email.subject = 'Test Lead';
       // email.ToAddress ='test@gmail.com';
        CreateLeadExample  edr = new CreateLeadExample();
        edr.handleInboundEmail(email,env); 

        Test.startTest();
        Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
         System.assert(result.success, 'Error processing email...');
        Test.stopTest();
               
        Lead [] leadDb = [SELECT Id FROM Lead where LastName=:email.FromAddress];
       // System.assertEquals (1, leadDb.size(),'Lead was not inserted');
       System.debug('Size'+leadDb.size());
       
        attachment.body = blob.valueOf('my attachment text');
        attachment.fileName = 'text';
        email.binaryAttachments =
        new Messaging.inboundEmail.BinaryAttachment[] { attachment };
        Attachment attach=new Attachment();     
        attach.Name='Test';
        Blob bodyBlob=Blob.valueOf('Testing Body of Attachment');
        attach.body=bodyBlob;
        insert attach;

the result.success is getting false.how can i resolve this error

thanks
Meenu MathewMeenu Mathew
Can you upload your class also
cvuyyurucvuyyuru
Hi Devmen,
What value you are getting in debug?
Result.success should give you true or false not a string.
debug it and change the assertion value in test class.
DevmenDevmen
@meenumathew this is my class .Here im displaying emailfield,cc address,subject +body in lead page.
global class CreateLeadExample implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

        Lead lead;

        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); 
     

        try {
            //Look for lead whos name is the email and create it if necessary

            List<Lead> leadList = new List<Lead>();


            String tempLead = email.FromAddress;
           
            String [] ccAddress=email.ccAddresses;
            
            System.debug('ToAddress'+email.toAddresses);
            System.debug('FROMADDRESS = '+email.FromAddress);
            System.debug('FROMADDRESS TEMPLEAD = '+tempLead);


            String leadQuery = 'select LastName from Lead where LastName = \'' + tempLead +'\'';

            leadList = Database.Query(leadQuery);
            Integer leadCount = leadList.size();
            System.debug('LEAD COUNT FROM THE QUERY -'+leadCount);
              
            if ([select count() from Lead where Name = :email.FromAddress] == 0) {
            
             for (String address : email.ccAddresses)
               {
                
                System.Debug('INSIDE IF for LEAD COUNT EQUAL TO 0');
                system.debug('EmailAddress'+ email.ccAddresses);
                lead = new Lead();
                lead.Status = 'New';
                lead.LastName = email.FromName;
                lead.Company = (email.FromAddress.split('@').get(1));
                lead.Email = email.FromAddress;
                system.debug(address);
                lead.Cc__c=address;
                lead.Description='Subject:'+ email.subject  + '/n' + email.plainTextBody;
                
                }
                 
                insert lead;
                
                
            //Save any text attachments, if any
            if (email.textAttachments != null)
            {
            for (integer i = 0 ; i < email.textAttachments.size() ; i++) {
            Attachment attachment = new Attachment();
            // attach to the newly created contact record
            attachment.ParentId = lead.Id;
            attachment.Name = email.textAttachments[i].filename;
            attachment.Body = Blob.valueof(email.textAttachments[i].body);
            insert attachment;
            }
            
            }

            //Save any Binary Attachment
            
             if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
          for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
            Attachment attachment = new Attachment();
            // attach to the newly created contact record
            attachment.ParentId = lead.Id;
            attachment.Name = email.binaryAttachments[i].filename;
            attachment.Body = email.binaryAttachments[i].body;
            insert attachment;
          }
       }
          
            }
            
            else { //Lead already exists
                lead = [select Id from Lead where Name = :email.FromAddress];
            }          
            result.success = true;
            
        } catch (Exception e) {
            result.success = false;
            result.message = 'Error processing email...';
        }
        return result;
    }
}

 
DevmenDevmen
yes im getting false value.