• Hjams Haks
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
Hello! I'm just now digging into Apex Triggers, and I'm having a little trouble with creating tests. Here's my Apex class:
trigger CanceledProgram on Program__c (before update) {
	Program__c pNew = Trigger.new[0];
    Program__c pOld = Trigger.oldMap.get(pNew.id);
    
    if(pNew.Canceled__c != pOld.Canceled__c && pNew.Canceled__c == true){
        System.debug('Updated to True');
        System.debug('New value = ' + pNew.Canceled__c);
        System.debug('Old value = ' + pOld.Canceled__c);
        
        if(pOld.Name.length() <= 70){
            pNew.Name = 'CANCELED: ' + pNew.Name;
            System.debug('New name length: '+pNew.Name.length());
            System.debug('Old name length: '+pOld.Name.length());
            System.debug(pNew.Name);
        } else if (pOld.Name.length() > 70){
            String pNameShort = pNew.Name.substring(0, 65) + '...';
            pNew.Name = 'CANCELED: ' + pNameShort;
            System.debug(pNew.Name);
            System.debug(pNew.Name.length());
        }
        
        
    } else if (pNew.Canceled__c != pOld.Canceled__c && pNew.Canceled__c == false) {
        System.debug('Updated to False');
        System.debug('New value = ' + pNew.Canceled__c);
        System.debug('Old value = ' + pOld.Canceled__c);
        if(pNew.Name.startsWith('CANCELED: ')){
            pNew.Name = pNew.Name.substring(10);
        }
        
        
    } else {
        System.debug('The Canceled Field was not changed');
    }
    
    if (pNew.Name != pOld.Name && pOld.Name.length() >70){
        
        System.debug(pOld.Name.length());
        System.debug(pNew.Name.length());
    }
        
}

And see below for the test class. It says I have 100% coverage, but that the 3/4 tests failed. I assume they need to succeed in order to push this class to production. I don't understand why they didn't pass though? In the first test method, I did a little testing, trial, and error with using System.debug to double check the values, and the test should be passing. Can anyone tell me what I'm doing wrong? Thanks for any help you can provide!
 
@isTest
private class CanceledProgramTest {     

    @isTest static void TestCanceledProgramWithShortName() {
        Program__c p1 = new Program__c(Name='Will not happen');
        insert p1;
        
        p1 = [SELECT Id, Name, Canceled__c FROM Program__c WHERE Id = :p1.Id];
        System.debug(p1);
        Test.startTest();
        p1.Canceled__c = true;
        update p1;
        System.assertEquals('CANCELED: Will not happen', p1.Name);
        Test.stopTest();
    }
    
    @isTest static void TestCanceledProgramWithLongName() {
        Program__c p = new Program__c(Name='This program will not happen. I am sorry, but this program just will not happen.');
        insert p;
        p.Canceled__c = true;
        update p;
        System.assertEquals('CANCELED: This program will not happen. I am sorry, but this program just w...', p.Name);
    }
    
    @isTest static void TestUnCanceledProgramChangeName() {
        Program__c p = new Program__c(Name='CANCELED: We will have it after all. Name changes.', Canceled__c = true);
        insert p;
        p.Canceled__c = false;
        update p;
        System.assertEquals('We will have it after all. Name changes.', p.Name);
    }
    
    @isTest static void TestUnCanceledProgramSameName() {
        Program__c p = new Program__c(Name='We will have it after all. Name is same.' , Canceled__c = true);
        insert p;
        p.Canceled__c = false;
        update p;
        System.assertEquals('We will have it after all. Name is same.', p.Name);
    }
}

 
I am looking to map the Lead Record Type to the Opportunity Record Type when I convert the lead. Right now the Opportunity Record Type populates with a default value. I have only written a few apex triggers as of now and am still new to salesforce, I appreciate any help! 

I have an issue with a trigger that grabs a date/time value from one field (Field 1)and populates it in another (Field 2). The issue I run into is that if Field 1 is empty I get an error which I am guessing is cause you can insert a null value in a time/date field. Is there a to leave the field blank?

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Previous_Notes caused an unexpected exception, contact your administrator: Previous_Notes: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Previous_Notes: line 27, column 1

 

trigger Previous_Notes on Event (before insert) {
    

Set<ID> trEvent = New Set<ID>();
Map<ID, Event> mAccEvent = New Map<ID, Event>();

//Get list of WhatIDs in trigger
for(Event e : trigger.new)
    trEvent.add(e.whatID);

List<Event> lEvent = [SELECT id, Event.WhatID,Event.Previous_Completed_Date_Time__c,Event.Completed_Date_Time_-c,Event.Previous_Notes__c,Event.Relevant_Notes__c FROM Event WHERE (whatID IN :trEvent) and (Subject = 'Outside Sales Call') and  (Sales_Call_Completed__c ='Yes' ) AND (Relevant_Notes__c != ' ') ORDER BY Completed_Date_Time__c DESC];

//Populate MAP with WhatIDs to the list of events
For (Event e : lEvent){
      If(mAccEvent.get(e.whatID) == Null ) 
         mAccEvent.put(e.WhatID, e);
}

System.Debug('mAccEvent ' + mAccEvent);

for (Event updatedEvent : trigger.new) {   

System.Debug('updatedEvent.whatID = ' + updatedEvent.whatID);


If(mAccEvent.containsKey(updatedEvent.whatID) == TRUE) updatedEvent.Previous_Notes__c = mAccEvent.get(updatedEvent.whatID).Relevant_Notes__c  ; 
updatedEvent.Previous_Completed_Date_Time__c = mAccEvent.get(updatedEvent.whatID).Completed_Date_Time__c ;
}

//This should get most recent events with whatIDs that are the sames as all the whatIDs in the trigger. Then it will populate a map using those whatIDs. In the last for loop it will update each record in the trigger with the Previous_Notes__c value from the 1 event with a matching whatID. 

}

 

  • November 07, 2011
  • Like
  • 0