• Kareem Miller 6
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
I have create a trigger that creates a custom object record (Training__c ) whenever the formula field Training Id (on Tasks) equals 1, and the task is completed. The code seems to be working fine in my sandbox however when I go to implement the test class I run into two issues: 

Issue number 1: When I run my test class I don't get any code coverage on the trigger after running it.
Issue number 2: When I try to deploy the trigger and class I get the following error:
       
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] 
Stack Trace: Class.insertTraining.insertTraining: line 26, column 1.

I'm not quite sure what the Invalid_Cross_Refeference_Key  id is but I'm guessing it has to do with the task that I am creating. The code for both the class and trigger are below. Any assistance is greatly a ppreciated. 
 
Insert Training Trigger: 


trigger insertTraining on Task (after update) {
               
    for (Task task : Trigger.new) {
       
        if(task.Training_Id__c == 1 && task.Status == 'Completed' ){
            
            Training__c train = new Training__c();
 
 
 train.Name = task.Subject;
 train.Type__c = task.Activity_Type__c;
 train.Missed__c= task.Missed__c;
 train.Onsite__c= task.Onsite__c;
 train.Comments__c = task.Description;
 train.Related_To__c = Trigger.new[0].WhatId;
 train.Due_Date__c = task.ActivityDate;
 train.Start_Time__c = task.Start_Time__c;
 train.End_Time__c = task.End_Time__c;
 train.Concerns__c = task.Concerns__c;
 train.Next_Steps__c = task.Next_Steps__c;

 insert train;
             
        }
    }
}



insertTraining Test Class: 

@istest

public class insertTraining {
    static testMethod void insertTraining (){
    
    string parentId = '001p000000ObV3q';
      
    //Insert a new activity where Training ID is 1
    
    Task task = new Task();
    
    task.Subject= 'Task is 1';
        
    task.No_Connect__c =  True;
    task.Connect__c = False;
    task.Notes_and_Comments__c = 'This should be the record that is taken to create a training' ;
    task.ActivityDate = date.newInstance(2017, 10, 15);
    task.Start_Time__c = dateTime.newInstance(2017, 10, 15, 12, 30, 0); 
    task.End_Time__c = dateTime.newInstance(2017, 10, 15, 12, 45, 0) ;
    task.Activity_Type__c = 'Basic Training';
    task.WhatId = parentId;
    task.Status = 'Completed';
    
   
    
    insert task;
  
 
      
      
       // insert new Training record
    
    Training__c train = new Training__c();
    
    train.Name = task.Subject;
    train.Type__c  = task.Activity_Type__c;
    train.missed__c = task.missed__c;  
    train.onsite__c = task.onsite__c;
    train.Comments__c = task.Notes_and_Comments__c;
    train.Related_To__c = task.WhatId;
    train.Due_Date__c = task.ActivityDate;
    train.Start_Time__c = task.Start_Time__c;
    train.End_Time__c = task.End_Time__c;
    
    
    insert train;
        
     //find the number of all of the trainings that that have been created
     
    List<Training__c> train1 =[Select Id,Name, onsite__c, missed__c, Related_To__c, Start_Time__c, End_Time__c, Due_Date__c, Comments__c, Type__c FROM Training__c WHERE Id = :task.Whatid];
     
                    
     System.assertEquals(0,train1.size());
     
     
     
     
     
     
         
         
         
         }
     
     
  }
I'm still relatively knew to triggers and classes so any assistance or clarification is greatly appreciated. 

I am creating a custom button that when pressed will change the record owner to that of the user and update a picklist field 'Sales Status' to working. Whenever I click the button in the list view the owner changes just fine however the Picklist field continues to reuturn a value of 'fasle'. I have double checked the value to make suer there weren't any errors and it is a value within the picklist field. Can anyone assist. I have come up with the following code:

 

{!REQUIRESCRIPT('/soap/ajax/31.0/connection.js')} 

var accountIds = {!GETRECORDIDS($ObjectType.Account)}; 
var account = [], tempAccount, result; 
while(accountIds.length) { 
tempAccount = new sforce.SObject('Account'); 
tempAccount.Id = accountIds.shift(); 
tempAccount.OwnerId = '{!$User.Id}'; 
tempAccount.Sales_Status__c= {!ISPICKVAL(Account.Sales_Status__c, 'Working')}; 

account.push(tempAccount); 



result = sforce.connection.update(account); 

window.top.location.href = window.top.location.href; 

I have a trigger that will prevent me from deleting a contact within my org but can't push it into production without a test class. I am new to triggers and am not entirely sure how to write a test class for the following code:

trigger RestrictConDels on Contact (before delete) {
    if (Trigger.isDelete && Trigger.isBefore) {
    //In a before delete trigger, the trigger accesses the records that will be deleted with the Trigger.old list.
        for (Contact a : Trigger.old) {
            if (a.name != 'okToDelete') {
                system.debug('Record: '+a.name+' failed to delete');
                a.addError('You can\'t delete this record!');
                continue;
            }
            system.debug('Successfully Deleted Record: '+a.name);
        }
    }
}


Any help is appreciated. 
I have create a trigger that creates a custom object record (Training__c ) whenever the formula field Training Id (on Tasks) equals 1, and the task is completed. The code seems to be working fine in my sandbox however when I go to implement the test class I run into two issues: 

Issue number 1: When I run my test class I don't get any code coverage on the trigger after running it.
Issue number 2: When I try to deploy the trigger and class I get the following error:
       
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] 
Stack Trace: Class.insertTraining.insertTraining: line 26, column 1.

I'm not quite sure what the Invalid_Cross_Refeference_Key  id is but I'm guessing it has to do with the task that I am creating. The code for both the class and trigger are below. Any assistance is greatly a ppreciated. 
 
Insert Training Trigger: 


trigger insertTraining on Task (after update) {
               
    for (Task task : Trigger.new) {
       
        if(task.Training_Id__c == 1 && task.Status == 'Completed' ){
            
            Training__c train = new Training__c();
 
 
 train.Name = task.Subject;
 train.Type__c = task.Activity_Type__c;
 train.Missed__c= task.Missed__c;
 train.Onsite__c= task.Onsite__c;
 train.Comments__c = task.Description;
 train.Related_To__c = Trigger.new[0].WhatId;
 train.Due_Date__c = task.ActivityDate;
 train.Start_Time__c = task.Start_Time__c;
 train.End_Time__c = task.End_Time__c;
 train.Concerns__c = task.Concerns__c;
 train.Next_Steps__c = task.Next_Steps__c;

 insert train;
             
        }
    }
}



insertTraining Test Class: 

@istest

public class insertTraining {
    static testMethod void insertTraining (){
    
    string parentId = '001p000000ObV3q';
      
    //Insert a new activity where Training ID is 1
    
    Task task = new Task();
    
    task.Subject= 'Task is 1';
        
    task.No_Connect__c =  True;
    task.Connect__c = False;
    task.Notes_and_Comments__c = 'This should be the record that is taken to create a training' ;
    task.ActivityDate = date.newInstance(2017, 10, 15);
    task.Start_Time__c = dateTime.newInstance(2017, 10, 15, 12, 30, 0); 
    task.End_Time__c = dateTime.newInstance(2017, 10, 15, 12, 45, 0) ;
    task.Activity_Type__c = 'Basic Training';
    task.WhatId = parentId;
    task.Status = 'Completed';
    
   
    
    insert task;
  
 
      
      
       // insert new Training record
    
    Training__c train = new Training__c();
    
    train.Name = task.Subject;
    train.Type__c  = task.Activity_Type__c;
    train.missed__c = task.missed__c;  
    train.onsite__c = task.onsite__c;
    train.Comments__c = task.Notes_and_Comments__c;
    train.Related_To__c = task.WhatId;
    train.Due_Date__c = task.ActivityDate;
    train.Start_Time__c = task.Start_Time__c;
    train.End_Time__c = task.End_Time__c;
    
    
    insert train;
        
     //find the number of all of the trainings that that have been created
     
    List<Training__c> train1 =[Select Id,Name, onsite__c, missed__c, Related_To__c, Start_Time__c, End_Time__c, Due_Date__c, Comments__c, Type__c FROM Training__c WHERE Id = :task.Whatid];
     
                    
     System.assertEquals(0,train1.size());
     
     
     
     
     
     
         
         
         
         }
     
     
  }
I'm still relatively knew to triggers and classes so any assistance or clarification is greatly appreciated. 

I am creating a custom button that when pressed will change the record owner to that of the user and update a picklist field 'Sales Status' to working. Whenever I click the button in the list view the owner changes just fine however the Picklist field continues to reuturn a value of 'fasle'. I have double checked the value to make suer there weren't any errors and it is a value within the picklist field. Can anyone assist. I have come up with the following code:

 

{!REQUIRESCRIPT('/soap/ajax/31.0/connection.js')} 

var accountIds = {!GETRECORDIDS($ObjectType.Account)}; 
var account = [], tempAccount, result; 
while(accountIds.length) { 
tempAccount = new sforce.SObject('Account'); 
tempAccount.Id = accountIds.shift(); 
tempAccount.OwnerId = '{!$User.Id}'; 
tempAccount.Sales_Status__c= {!ISPICKVAL(Account.Sales_Status__c, 'Working')}; 

account.push(tempAccount); 



result = sforce.connection.update(account); 

window.top.location.href = window.top.location.href; 

I have a trigger that will prevent me from deleting a contact within my org but can't push it into production without a test class. I am new to triggers and am not entirely sure how to write a test class for the following code:

trigger RestrictConDels on Contact (before delete) {
    if (Trigger.isDelete && Trigger.isBefore) {
    //In a before delete trigger, the trigger accesses the records that will be deleted with the Trigger.old list.
        for (Contact a : Trigger.old) {
            if (a.name != 'okToDelete') {
                system.debug('Record: '+a.name+' failed to delete');
                a.addError('You can\'t delete this record!');
                continue;
            }
            system.debug('Successfully Deleted Record: '+a.name);
        }
    }
}


Any help is appreciated.