0

Follwoing this post What is the best workaround for the 20 field history tracking cap?

I have tried to adapt it for my custom Object MPG__c where I want to track all fields. Tracking via field sets is a great idea without having to keep amending the apex. I have amended what I can based on my current custom object, my new MPGHistory__c customer object for field history and field set 'History Tracking' which is on MPG__c. I have built this in our Developer Pro sandbox but it is showing 0% coverage and when i tried to deploy to live I get: "Code Coverage Failure The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage. MPGHistoryTracker"

Plus where is the relationship to MPG__c on the MPGHistory__c object so it is a related list on MPG__c object? :

trigger MPGHistoryTracker on MPG__c (after update) {
List<Schema.FieldSetMember> trackedFields =     SObjectType.MPG__c.FieldSets.HistoryTracking.getFields();
if (trackedFields.isEmpty()) return;

 List<MPGHistory__c> fieldChanges = new List<MPGHistory__c>();

 List<string> apiNameList = new List<string>();        

if(Trigger.isUpdate){
for (MPG__c aNew : trigger.new) {

MPG__c aOld = trigger.oldmap.get(aNew.Id);

for (Schema.FieldSetMember fsm : trackedFields) {

 String fieldName  = fsm.getFieldPath();
String fieldLabel = fsm.getLabel();

if (aNew.get(fieldName) != aOld.get(fieldName)) {

String oldValue = String.valueOf(aOld.get(fieldName));
String newValue = String.valueOf(aNew.get(fieldName));
if (oldValue != null && oldValue.length()>255) oldValue = oldValue.substring(0,255);
 if (newValue != null && newValue.length()>255) newValue = newValue.substring(0,255); 

 MPGHistory__c aht = new MPGHistory__c();
aht.name__c         = fieldLabel;
aht.apiName__c   = fieldName;
aht.User__c      = aNew.Id;
aht.ChangedBy__c = UserInfo.getUserId();
aht.OldValue__c  = oldValue;
 aht.NewValue__c  = newValue;

 apiNameList.add(aht.apiName__c);
 fieldChanges.add(aht);
}        
}
}
}
if (!fieldChanges.isEmpty()) {
 insert fieldChanges;
 }
 }
  • did you write a testmethod that exercises this trigger and run that testmethod? – cropredy Sep 5 '14 at 17:37
  • No I deployed it from sand box to live. – Chris Manton Sep 6 '14 at 19:14
1

Before you deploy any code to production, you should

  • Create test classes/testmethods that exercise the new code. SFDC won;t allow any code to be deployed to PROD unless at least 75% of the lines have been executed (covered). Of course, this 75% is really a 'lower bound' on best practices. You really want to have test methods that do system.assert statements to verify that your code is doing what you expect - positive and negative use cases
  • There is an extensive set of guidance on writing testmethods in the developer.salesforce.com site as well as excellent examples in the Apex Developer's Guide. You'll also find excellent advice elsewhere on SFSE

When you deploy the new trigger, you also need to deploy the test class.

All PROD deployments cause SFDC to rerun all testmethods, even for code you didn't deploy, to verify that the new deployment hasn't broken existing code. This is true for ant, eclipse, mavensmate, and changeset deployment paths

Since the error you received:

Code Coverage Failure The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage. MPGHistoryTracker

is indicative of not deploying any testmethod and your comment confirms this, the above steps will solve your issue

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.