• karthik R 80
  • NEWBIE
  • 25 Points
  • Member since 2016

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 7
    Replies
Hi All,
I am new to Salesforce.
I have written a batch apex to delete records and send a csv file to some email id.
I want to write test class for the same. Her is my code.
Batch Apex:
global class Del_leads implements Database.Batchable<sobject>
{  
       
   Public String s;
   public integer j; 
   Public string k='\n'+'IDName'+','+'CreatedDate';  
   Custom_Setting1__c mc=Custom_Setting1__c.getInstance('days');
   String dy=mc.day__c;
   global Database.QueryLocator start(Database.BatchableContext BC){
      
      return Database.getQueryLocator([SELECT Id,Subject FROM lead where createdData=:Today.addDays-(dy)]);     
   }
   
global void execute(Database.BatchableContext BC,List<Task> Lds){     
  
    s +=k+'\n'+ lds[j].Id+','+lds[j].CreatedDate;
          
  Blob b=Blob.valueOf(s);
  Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  efa.setFileName(m+'attachment.csv');
  efa.setBody(b);
        
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
  mail.setToAddresses(new String[] {abc@test.com'});        
  mail.setSenderDisplayName('Batch Processing');
  mail.setSubject('Batch Process Completed');
  mail.setPlainTextBody('Please find the attachment of deleted records');
  mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});     
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });       
  delete Lds;           

}  
   
global void finish(Database.BatchableContext BC){     
  System.debug(LoggingLevel.WARN,'Deleting Leads Finished');
}

}
Test Class:
@isTest 
public class Del_leadsTest 
{
    static testMethod void testMethod1() 
	{
	
		Lead newLead = new Lead(firstName = 'Cole', lastName = 'Swain', company = 'BlueWave', status = 'contacted' ) ;
		insert 	newLead;
		
		Test.startTest();

			Del_leads obj = new Del_leads();
			obj.j=0;
			DataBase.executeBatch(obj);

		Test.stopTest();
		
	}
}

This test class is failing while running. Error is getting at custom setting line.
Please help me in writing test class for this.
Thanks in Advance
Hi All ,

I would like to know the answers of the following Interview questions.
1) Why salesforce Introduced Code Coverage?
2) What is Multitenant Architecture?
3) Is Rest API transfers only less amount of Data ?if it is Y?
Thanks,
karthik
global class UpdatesObjectFields implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        String query= 'SELECT Id,FirstName, LastName, OtherStreet,OtherCity,OtherState,OtherPostalCode,OtherCountry,OtherStateCode,OtherCountryCode,OtherLatitude,OtherLongitude,OtherGeocodeAccuracy,OtherAddress,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,MailingStateCode,MailingCountryCode,MailingLatitude,MailingLongitude,MailingGeocodeAccuracy,MailingAddress,Phone,Fax,MobilePhone,HomePhone,OtherPhone,AssistantPhone,Email,Birthdate,Description,PhotoUrl,Middle_Name__c,Email_2nd__c,Gender__c,Last_Contact_Number__c  from Contact';


 return Database.getQueryLocator(query); 
    }
    
    global void execute(Database.BatchableContext bc, List<Contact> cnct)
    {
        
        Contact[] con = new List<Contact>();
        List<Contact> my_list = new List<Contact>();  
        List<Contact> my_list1 = new List<Contact>();
        my_list = cnct; 
     my_list1 =  my_list;
        Contact ct = new Contact(); 
        for (integer i=0; i<cnct.size();i++ )
        {
            ct = my_list[i];
            integer j=0;
            if(i+1 >=my_list.size())    
            {
                ct.FirstName = my_list1[j].FirstName;j++;         
            }
           else
            {
                ct.FirstName = my_list[i+1].FirstName;
            }
            if(i+2 >=my_list.size())    
            {
                ct.LastName = my_list1[j].LastName;j++;
            }
            else
            {
                ct.LastName = my_list[i+2].LastName;
            }
            if(i+3 >=my_list.size())    
            {
                ct.OtherStreet = my_list1[j].OtherStreet;j++;
            }
            else
            {
                ct.OtherStreet = my_list[i+3].OtherStreet;
            }  
            
            
            con.add(ct);
        }    
        update con;
        
        
    }
    
    global void finish(Database.BatchableContext bc)
    { 
    }
}
            
  • November 28, 2016
  • Like
  • 0
Hi All ,

I would like to know the answers of the following Interview questions.
1) Why salesforce Introduced Code Coverage?
2) What is Multitenant Architecture?
3) Is Rest API transfers only less amount of Data ?if it is Y?
Thanks,
karthik
<apex:page standardController="Account"> <script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script> <script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script> <script type="text/javascript"> function validateText(){ var numb="{!Account.AccountNumber}"; alert("Test:"+numb); if(isNaN(parseInt(numb))) { alert("Please only enter numeric characters only ! (Allowed input:0-9)"); return false ; } else return true }; </script> } <apex:form > <apex:pageBlock title="edit accounts for{!$User.LastName}"> <apex:pageBlock title="edit accounts for{!$User.Title}"> <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton value="Next" action="{!save}" onclick="return validateText();"/> </apex:pageBlockButtons>` <apex:pageBlockSection > <apex:inputField value="{!Account.Site}"/> <apex:inputField value="{!Account.AccountNumber}" /> <apex:inputField value="{!Account.Name}"/> <apex:inputField value="{!Account.NumberOfEmployees}"/> <apex:inputField value="{!Account.Industry}"/> <apex:inputField value="{!Account.Type}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:pageBlock> </apex:form> </apex:page>
  • November 28, 2016
  • Like
  • 0
Hi All,
I am new to Salesforce.
I have written a batch apex to delete records and send a csv file to some email id.
I want to write test class for the same. Her is my code.
Batch Apex:
global class Del_leads implements Database.Batchable<sobject>
{  
       
   Public String s;
   public integer j; 
   Public string k='\n'+'IDName'+','+'CreatedDate';  
   Custom_Setting1__c mc=Custom_Setting1__c.getInstance('days');
   String dy=mc.day__c;
   global Database.QueryLocator start(Database.BatchableContext BC){
      
      return Database.getQueryLocator([SELECT Id,Subject FROM lead where createdData=:Today.addDays-(dy)]);     
   }
   
global void execute(Database.BatchableContext BC,List<Task> Lds){     
  
    s +=k+'\n'+ lds[j].Id+','+lds[j].CreatedDate;
          
  Blob b=Blob.valueOf(s);
  Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  efa.setFileName(m+'attachment.csv');
  efa.setBody(b);
        
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
  mail.setToAddresses(new String[] {abc@test.com'});        
  mail.setSenderDisplayName('Batch Processing');
  mail.setSubject('Batch Process Completed');
  mail.setPlainTextBody('Please find the attachment of deleted records');
  mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});     
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });       
  delete Lds;           

}  
   
global void finish(Database.BatchableContext BC){     
  System.debug(LoggingLevel.WARN,'Deleting Leads Finished');
}

}
Test Class:
@isTest 
public class Del_leadsTest 
{
    static testMethod void testMethod1() 
	{
	
		Lead newLead = new Lead(firstName = 'Cole', lastName = 'Swain', company = 'BlueWave', status = 'contacted' ) ;
		insert 	newLead;
		
		Test.startTest();

			Del_leads obj = new Del_leads();
			obj.j=0;
			DataBase.executeBatch(obj);

		Test.stopTest();
		
	}
}

This test class is failing while running. Error is getting at custom setting line.
Please help me in writing test class for this.
Thanks in Advance
try{
            for(Case c : caseList){
                if(c.Timer__c >= 10){
                    c.IsEscalated = true;
                    c.Status = 'Escalated';
                    caseUpdateList .add(c);
                    system.debug(c);
                }
            }
            if(caseUpdateList .size()>0){
                update caseUpdatelist ;
                system.debug(' case is Escalated in 10 minutes');
            }
        }
        catch(exception ex){
            system.debug(' case is not updated, it is now working according the criteria');
        }

In the Above code it is not covering a if statements. Here timer__c is formula field .

Test Class:
 
@isTest(SeeAllData = true)
private class CaseEscalationTest {

	private static testMethod void caseEscalationTestMethod() {
	    
	    case c = new case();
	    c.status = 'New';
	    c.origin = 'Phone';
	    c.Type = 'Status of specimen';
	    c.IsEscalated = false;
	    
	    insert c;
	    c = [SELECT Status,Origin,Type,Timer__c FROM case WHERE Id = :c.Id];
	    //Datetime yesterday = Datetime.now().addDays(-1);
        //Test.setCreatedDate(c.Id, yesterday);
	    
	    case c1 = new case();
	    c1.status = 'New';
	    c1.origin = 'Phone';
	    c1.Type = 'Status of specimen';
	    c1.IsEscalated = true;
	    insert c1;
	    
	    Test.startTest();
	    scheduleOneMinute som = new scheduleOneMinute();
	    system.Schedule('Specimen Cases', '0 1 11 ? * *', som);
        Test.stopTest();
	}
		

}