• Sheldon Callahan 16
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
I have this test code to test if an account is enabled.
@isTest
private class PIContractsCronTest {
    @isTest static void PIContractsCronTest() {

        //Scenario: 
        ///Given a nightly build at 11:59
        //Arrange

        Account acct = new Account();
        acct.Name = 'Test Account - Sheldon 3';
        acct.recordTypeId = '01250000000DfwkAAC';
        acct.Primary_Sector__c = 'General Inclusion';
        acct.Name_Code__c = 'TSTSHEL';
        insert acct;

        Account parentAcct = [SELECT Id,Name FROM Account WHERE Name = 'Test Account - Sheldon 3'];
        PI_Contract__c pic = new PI_Contract__c(Name ='Test Contract', Account__c = parentAcct.Id, Local_Currency_Peg_Enabled__c = 'No', Contract_Start_Date__c = DATE.TODAY());

        
        insert pic;

        // When an Account object has a PI CONTRACT with the “Status = current” AND current date is greater than or equal to “Contract Start Date” 
        //Act
        Test.StartTest();
        PIContractsCron cron = new PIContractsCron();
        Account checkPI = [SELECT Id,Name, PI_Enabled__c FROM Account WHERE Id = :parentAcct.Id];
      
        Test.StopTest();
        // Then set the field PI Contracts Enabled to true on the Account Object
        //Assert
          System.debug('Check PI:::'+checkPI);
   
        System.assertEquals(true,checkPI.PI_Enabled__c,'PI is not Enabled on this Account');

    }


}

This code runs this function

public class PIContractsCron {
    public PIContractsCron() {
       List<PI_Contract__c> contractList = [SELECT Id, Status__c, Contract_Start_Date__c, Account__c FROM PI_Contract__c WHERE Status__c = 'Current' AND Contract_Start_Date__c >= TODAY];
        Map <Id, Account> acctMap = new Map<Id, Account>();
        for(PI_Contract__c contract: contractList){
            Id acctNo = contract.Account__c;
            Account acct = [SELECT Id, PI_Enabled__c FROM Account WHERE Id = :acctNo];
            acctMap.put(acctNo,acct);
        }

		List <Account> enabledAcct = new List<Account>();
		for(Account a: acctMap.values()){
			a.PI_Enabled__c = true;
			enabledAcct.add(a);
		}

        System.debug('Enabled Accounts:::'+ enabledAcct);

        update enabledAcct;
    }
}
 

I can tell that the dml from the test is not showing up in this class because the  System.debug('Enabled Accounts:::'+ enabledAcct); comes back empty. What am I doing wrong?

I have an Id for a record I am getting from a trigger and I want to compare it to an Id in my SOQL Where clause. What I am doing wrong

trigger UpdateTest on Test__c (before insert) {
    
    List<Test__c> testList = new List<Test__c>();
   
	System.debug(Trigger.New);
    ID accNo;
    for(Test__c a : Trigger.New) {
        accNo = a.Account__c;
    }  
    
     testList = [SELECT Id, Status__c, Account__c FROM Test__c WHERE Account__c = accNo];
    
    List<Test__c> updateList = new List<Test__c>();
    
    for(Test__c contract : testList){

           contract.Status__c = 'Past';
        updateList.add(contract);

        
    }
    
    update updateList;


    
}

I have an Id for a record I am getting from a trigger and I want to compare it to an Id in my SOQL Where clause. What I am doing wrong

trigger UpdateTest on Test__c (before insert) {
    
    List<Test__c> testList = new List<Test__c>();
   
	System.debug(Trigger.New);
    ID accNo;
    for(Test__c a : Trigger.New) {
        accNo = a.Account__c;
    }  
    
     testList = [SELECT Id, Status__c, Account__c FROM Test__c WHERE Account__c = accNo];
    
    List<Test__c> updateList = new List<Test__c>();
    
    for(Test__c contract : testList){

           contract.Status__c = 'Past';
        updateList.add(contract);

        
    }
    
    update updateList;


    
}
I have this test code to test if an account is enabled.
@isTest
private class PIContractsCronTest {
    @isTest static void PIContractsCronTest() {

        //Scenario: 
        ///Given a nightly build at 11:59
        //Arrange

        Account acct = new Account();
        acct.Name = 'Test Account - Sheldon 3';
        acct.recordTypeId = '01250000000DfwkAAC';
        acct.Primary_Sector__c = 'General Inclusion';
        acct.Name_Code__c = 'TSTSHEL';
        insert acct;

        Account parentAcct = [SELECT Id,Name FROM Account WHERE Name = 'Test Account - Sheldon 3'];
        PI_Contract__c pic = new PI_Contract__c(Name ='Test Contract', Account__c = parentAcct.Id, Local_Currency_Peg_Enabled__c = 'No', Contract_Start_Date__c = DATE.TODAY());

        
        insert pic;

        // When an Account object has a PI CONTRACT with the “Status = current” AND current date is greater than or equal to “Contract Start Date” 
        //Act
        Test.StartTest();
        PIContractsCron cron = new PIContractsCron();
        Account checkPI = [SELECT Id,Name, PI_Enabled__c FROM Account WHERE Id = :parentAcct.Id];
      
        Test.StopTest();
        // Then set the field PI Contracts Enabled to true on the Account Object
        //Assert
          System.debug('Check PI:::'+checkPI);
   
        System.assertEquals(true,checkPI.PI_Enabled__c,'PI is not Enabled on this Account');

    }


}

This code runs this function

public class PIContractsCron {
    public PIContractsCron() {
       List<PI_Contract__c> contractList = [SELECT Id, Status__c, Contract_Start_Date__c, Account__c FROM PI_Contract__c WHERE Status__c = 'Current' AND Contract_Start_Date__c >= TODAY];
        Map <Id, Account> acctMap = new Map<Id, Account>();
        for(PI_Contract__c contract: contractList){
            Id acctNo = contract.Account__c;
            Account acct = [SELECT Id, PI_Enabled__c FROM Account WHERE Id = :acctNo];
            acctMap.put(acctNo,acct);
        }

		List <Account> enabledAcct = new List<Account>();
		for(Account a: acctMap.values()){
			a.PI_Enabled__c = true;
			enabledAcct.add(a);
		}

        System.debug('Enabled Accounts:::'+ enabledAcct);

        update enabledAcct;
    }
}
 

I can tell that the dml from the test is not showing up in this class because the  System.debug('Enabled Accounts:::'+ enabledAcct); comes back empty. What am I doing wrong?