• Sandy Sandy12
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 1
    Replies

global class createChildForExistingRecords implements Database.Batchable<sObject>,Database.Stateful
{
    global final string startDate;
    global final string endDate;
    global final String query;
    static Set<Id> conIds = new Set<Id>();
    
    global createChildForExistingRecords(string sd, string ed)
    {
        startDate=sd; 
        endDate=ed;
        query='Select Id, Category_Group_Uploaded__c,EKS_Item_Number__c,ELWIS_Item_Number_Manual__c,Item_Short_Description_Manual__c,Listing_Type__c,'+
				'MPG_Valid_from__c,MPG_Valid_Until__c,Pack_Size__c,Prelisting_Item_Number__c,Quantity__c,Tier__c,Comments__c from Contract_Log__c where'+
            	' CreatedDate >='+sd+' and CreatedDate <='+ed;
        System.debug('query--------------------'+query);
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> scope)
    {
        List<Child_Contract_Log__c> childContractList = new List<Child_Contract_Log__c>();
        for(sObject s : scope)
        {
            Contract_Log__c c = (Contract_Log__c)s; 
            Child_Contract_Log__c child = new Child_Contract_Log__c();
            child.Contract_Log__c = c.Id;
            child.EKS_Item_Number__c = c.EKS_Item_Number__c;
            child.Category_Group__c = c.Category_Group_Uploaded__c;
            child.eLWIS_Item_Number__c = c.ELWIS_Item_Number_Manual__c;
            child.Item_Short_Description__c = c.Item_Short_Description_Manual__c;
            child.Internal_Comments__c = c.Comments__c;
            child.Listing_Type__c = c.Listing_Type__c;
            child.MPG_Valid_from__c = c.MPG_Valid_from__c;
            child.MPG_Valid_Until__c = c.MPG_Valid_Until__c;
            child.Pack_Size__c= c.Pack_Size__c;
            child.Prelisting_Item_Number__c = c.Prelisting_Item_Number__c;
            child.Quantity__c = c.Quantity__c;
            child.Tier__c = c.Tier__c;
            childContractList.Add(child);
        }
        insert childContractList;
        Map<Id,Id> childContractMap = new Map<Id,Id>();
        for(Child_Contract_Log__c child : childContractList)
            childContractMap.put(child.Contract_Log__c,child.Id);
        List<Master_Data__c> masterListToBeUpdated = new List<Master_Data__c>();
        for(contract_Log__c con : [Select Id,(Select Id from Master_Data_Logs__r) from Contract_Log__c where Id in:childContractMap.keyset()])
        {
            for(Master_Data__c m : con.Master_Data_Logs__r)
            {
                Master_Data__c mas = new Master_Data__c();
                mas.Id = m.Id;
                mas.Child_Contract_Log__c = childContractMap.get(con.Id);
                masterListToBeUpdated.Add(mas);
            }
        }
        update masterListToBeUpdated;
        CheckRecursive.runOnce = true;
    }
    
    global void finish(Database.BatchableContext BC)
    {
        
    }
}

my test class below :
 
@isTest
public class createChildForExistingRecords_Test {
    static testMethod void createChildcontractTest(){ 
    
    List<Contract_Log__c> sobjList =  new List<Contract_Log__c>();
        sobjList.add( new Contract_Log__c(
            
            Item_Group__c=12,
            Item_Family__c=14,
            EKS_Item_Number__c='12345',
            ELWIS_Item_Number_Manual__c='120',
            Item_Short_Description_Manual__c='Test Short Description',
            Listing_Type__c='In and Out',
            MPG_Valid_from__c=System.today(),
            MPG_Valid_Until__c=System.today()+356,
            Pack_Size__c='6',
            Prelisting_Item_Number__c='12',
            Quantity__c='13',
            Tier__c='Essentials',
            Comments__c='Test comments'
            
        ));
        sobjList.add( new Contract_Log__c(
           Item_Group__c=10,
            Item_Family__c=19,
            EKS_Item_Number__c='12309',
            ELWIS_Item_Number_Manual__c='128',
            Item_Short_Description_Manual__c='Test Short Description test',
            Listing_Type__c='In and Out',
            MPG_Valid_from__c=System.today()+10,
            MPG_Valid_Until__c=System.today()+350,
            Pack_Size__c='6',
            Prelisting_Item_Number__c='19',
            Quantity__c='11',
            Tier__c='Essentials',
            Comments__c='Test comments'
        ));
        insert sobjList;
        
        String startDate = '2019-03-01T00:00:00Z';
        String endDate ='2019-03-02T00:00:00Z';

    
           
        Test.startTest();
            database.executeBatch(New createChildForExistingRecords(startDate,endDate ));

        Test.stopTest();
    }
}

​​​​​​​Can anyone help me with test class 
Suppose we have group of checkboxs if
                  ( one check box is checked) {
                                             correction required
}
else 
{
None
}
Map<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
for(Opportunity opp : [Select id,accountId from opportunity]){
    List<Opportunity> opplst = AccountOppMap.get(opp.AccountId);
    if(opplst == null)
        opplst = new List<Opportunity>();
    opplst.add(opp);
    accountOppMap.add(opp.accountId,opplst);
}
 
Map<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
for(Opportunity opp : [Select id,accountId from opportunity]){
    List<Opportunity> opplst = AccountOppMap.get(opp.AccountId);
    if(opplst == null)
        opplst = new List<Opportunity>();
    opplst.add(opp);
    accountOppMap.add(opp.accountId,opplst);
}