You need to sign in to do that
Don't have an account?
Chad Davis
Confused Test Class Newbie needs help.
Hey Salesforce brilliance,
I am trying to deploy my trigger but I need to compose a test class and to be quite honest I am clueless and aloof at my begining stages of Apex-ing' . Could someone here please provide me with a little guidence, code perhaps and maybe addition educational documentation to make sense of this all so I can continue my journey to be half as great as many of you here.
Happy Holidays And thank you very much
-Chad
Trigger is here:
trigger Job_Description_Transfer on Job_Contract__c (before update, before insert) {
//create a set of job descriptions
set<Id> Ids = new Set <Id>();
for (Job_Contract__c japp: Trigger.new) {
Ids.add(japp.Related_Job__c); // This is thelookup field to description__c in Job_Contract__c
}
Map<Id, Opportunity> mapDescription = New Map<Id, Opportunity>([SELECT Id, Description FROM Opportunity WHERE Id IN :Ids]);
if(trigger.isUpdate){
for(Job_Contract__c jaTrigger : trigger.new) {
jaTrigger.Job_Description__c = mapDescription.get(jaTrigger.Related_Job__c).Description;
}
}
I am trying to deploy my trigger but I need to compose a test class and to be quite honest I am clueless and aloof at my begining stages of Apex-ing' . Could someone here please provide me with a little guidence, code perhaps and maybe addition educational documentation to make sense of this all so I can continue my journey to be half as great as many of you here.
Happy Holidays And thank you very much
-Chad
Trigger is here:
trigger Job_Description_Transfer on Job_Contract__c (before update, before insert) {
//create a set of job descriptions
set<Id> Ids = new Set <Id>();
for (Job_Contract__c japp: Trigger.new) {
Ids.add(japp.Related_Job__c); // This is thelookup field to description__c in Job_Contract__c
}
Map<Id, Opportunity> mapDescription = New Map<Id, Opportunity>([SELECT Id, Description FROM Opportunity WHERE Id IN :Ids]);
if(trigger.isUpdate){
for(Job_Contract__c jaTrigger : trigger.new) {
jaTrigger.Job_Description__c = mapDescription.get(jaTrigger.Related_Job__c).Description;
}
}
Please write TestClass as below for your Trigger.
goto Apex Class and click on New button from Setup.
@isTest
private class TestClassForJobContractTrigger {
static testMethod void validateTrigger() {
test.startTest();
Account acc = new Account(name='T1 Account');
// Insert account
insert acc;
// Retrieve account
acc = [SELECT Id,Name FROM account WHERE Id =:a.id];
// Test that name of account correctly added the value
System.assertEquals('T1 Account', acc.name);
opportunity opp=new Opportunity(Name='Test Oppty');
opp.CloseDate=system.today();
opp.StageName='Prospect';
opp.AccountId=acc.id;
opp.Description='this is my test opportunity';
insert opp;
//Create record of Job_Contract__c object type.
Job_Contract__c ObjJob=new Job_Contract__c();
ObjJob.Related_Job__c=opp.id;
ObjJob.Name='Test Contract';
// Insert Job_Contract__c
insert ObjJob;
// Insert Job_Contract__c
ObjJob.Name='My Name has been changed';
update ObjJob;
test.stopTest();
}
}
Hope above code will you writing Test class for your trigger.
All Answers
Please write TestClass as below for your Trigger.
goto Apex Class and click on New button from Setup.
@isTest
private class TestClassForJobContractTrigger {
static testMethod void validateTrigger() {
test.startTest();
Account acc = new Account(name='T1 Account');
// Insert account
insert acc;
// Retrieve account
acc = [SELECT Id,Name FROM account WHERE Id =:a.id];
// Test that name of account correctly added the value
System.assertEquals('T1 Account', acc.name);
opportunity opp=new Opportunity(Name='Test Oppty');
opp.CloseDate=system.today();
opp.StageName='Prospect';
opp.AccountId=acc.id;
opp.Description='this is my test opportunity';
insert opp;
//Create record of Job_Contract__c object type.
Job_Contract__c ObjJob=new Job_Contract__c();
ObjJob.Related_Job__c=opp.id;
ObjJob.Name='Test Contract';
// Insert Job_Contract__c
insert ObjJob;
// Insert Job_Contract__c
ObjJob.Name='My Name has been changed';
update ObjJob;
test.stopTest();
}
}
Hope above code will you writing Test class for your trigger.
ObjJob.Job_End_Date__c = XXXXXX ; Date.today(); - wont work beause its a formula.
Any ideas?