trigger setGuid on dftly_app_contact__c (before insert) {
For (dftly_app_contact__c dac: Trigger.new){
Blob b = Crypto.GenerateAESKey(128);
String h = EncodingUtil.ConvertTohex(b);
String guid = h.SubString(0,8)+ '-' +
h.SubString(8,12) + '-' +
h.SubString(12,16) + '-' +
h.SubString(16,20) + '-' +
h.substring(20);
system.debug(guid);
dac.Guid_App_Contact__c = guid;
}
}
-
Normal test class is not working ah ?– MonishMar 27, 2015 at 7:54
-
Please also post what you have already tried. If you don't know where to start, my suggestion is to test whether your trigger sets the GUID field to a String in the correct format.– Koen WesselmanMar 27, 2015 at 7:56
-
@KoenWesselman, yes trigger is working fine and field is getting filled. But the thing is I'm not getting how to create a test class for the above trigger.– ChanduMar 27, 2015 at 8:05
-
@Chandu Have a look at my answer and let me know if you have any other questions.– Koen WesselmanMar 27, 2015 at 8:15
1 Answer
The following code is an example of a test class for your Trigger. You will still have to write the actual assertions you want to do, i.e. whether the GUID string is the correct format.
If there's any required relations for dftly_app_contact__c, you will have to insert these records as well. You can do this simply inside your method or using testSetup: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_testsetup.htm
@isTest
public class TestSetGuidTrigger {
@isTest
static void setGuid() {
dftly_app_contact__c dac = new dftly_app_contact__c();
// SET ATTRIBUTES OF dftly_app_contact__c, IE NAME
insert dac;
dftly_app_contact__c dacTest = [SELECT Guid_App_Contact__c FROM dftly_app_contact__c WHERE Id =: dac];
// USE System.assert() AND System.assertEquals() TO TEST WHETHER THE GUID FIELD IS SET AND WHETHER THE STRING IS IN THE RIGHT FORMAT
}
}
Does that help?