• Mani_SFDC
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

Can anybody help me in executing the below batch class using annonomous apex

 

global class batchUpdateAccounts implements Database.Batchable<sObject>,Database.Stateful{

global Database.QueryLocator start(Database.BatchableContext bc){

    return database.getQuerylocator('select id,Category__c,Family__c from Account');
}

global void execute(Database.BatchableContext bc, List<SObject> scope) {
    
        List<Account> accs = (List<Account>)scope;
        List<Account> accsToUpdate = new List<Account>();
        List<Global_families__c> gfslst = new List<Global_families__c>();
        gfslst = [select Is_Global__c, Name from Global_families__c];
 
        for(Account acunts : accs){
        for(Global_families__c lstglobal: gfslst ){
        if(acunts.Family__c == lstglobal.Name && lstglobal.Is_Global__c == TRUE){
        Acunts.Family__c = 'Global';
        accsToUpdate.add(acunts);
        }
        }
        }
        
        update accsToUpdate;
        
       
}

global void finish(Database.BatchableContext bc){
        

}

}

Hello ,

 

I am very new to Apex code and I am facing difficulties in my unit test code. The main functionality of the trigger is to assign a standard campaign to the leads based on the lead hub score. I wrote a unit test the code and it is as follows

 

@istest
public class LeadCampaignTestClass{
private static testMethod void testclass1 () {
//Create one campaign record
Campaign ca = new Campaign(Name='Test Campaign');
//required fields
insert ca;
// create a lead record
Lead le = new lead();
//required fields
le.hub_spot_score__c = 25;
insert le;
// adding a campaign member
List<CampaignMember> members = [SELECT Id FROM CampaignMember
WHERE LeadId
= :le.Id AND CampaignID = :ca.Id];
System.assertEquals(1, members.size());
}
// Negative test class
private static testMethod void testclass2(){
//Create one campaign record
Campaign ca = new Campaign(Name='Test Campaign');
insert ca;
//create lead record
Lead le = new lead();
//No hub score is added
insert le;
List<CampaignMember> members = [SELECT Id FROM CampaignMember WHERE LeadId
= :le.Id AND CampaignID = :ca.Id];
System.assertEquals(1, members.size());
}
}

 

 

When I tried to run the test , the foowing error was displayed :

 

Apex Test Result Detail  

Time Started 8/20/2013 10:44 AM Class LeadCampaignTestClass Method Name testclass2 Pass/Fail Fail Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName, Company]: [LastName, Company] Stack Trace

Class.LeadCampaignTestClass.testclass2: line 27, column 1

 

 

Can some one help me with this issue asap .

 

Regards,

Angela Joseph