You need to sign in to do that
Don't have an account?
Jeyabharathi Rajendran 2
Apex Test Class for opportunity
Hi Guys
I am new to coding and i need your help. My task is to create a trigger which will allow to move the opportunity stage from qualification to estimation only if the record has an attachment. I found a trigger for this but i need a test class.
Apex Trigger
trigger AttachmentTrigger on Opportunity (before update) {
for(Opportunity o:Trigger.New) {
if(o.stagename == 'Estimating') {
Attachment a = new Attachment();
try {
a = [Select Id, Name from Attachment where ParentId =:o.Id];
}
catch(Exception e) {
a = null;
}
if (a == null)
o.addError('Add an attachment before you change the Opportunity stage to Estimating');
}
}
}
Apex Class:
@isTest
public class AttachmentTriggerTest {
static testMethod void testMethod1(){
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.CloseDate = system.today();
opp.StageName = 'Qualification';
opp.Type = 'New Customer';
insert opp;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
attach.body = bodyBlob;
attach.parentId = Opp.id;
insert attach;
}
}
i don't know why the apex class is not working. Iried deploy it in my production but it says 0% code coverage.
I am new to coding and i need your help. My task is to create a trigger which will allow to move the opportunity stage from qualification to estimation only if the record has an attachment. I found a trigger for this but i need a test class.
Apex Trigger
trigger AttachmentTrigger on Opportunity (before update) {
for(Opportunity o:Trigger.New) {
if(o.stagename == 'Estimating') {
Attachment a = new Attachment();
try {
a = [Select Id, Name from Attachment where ParentId =:o.Id];
}
catch(Exception e) {
a = null;
}
if (a == null)
o.addError('Add an attachment before you change the Opportunity stage to Estimating');
}
}
}
Apex Class:
@isTest
public class AttachmentTriggerTest {
static testMethod void testMethod1(){
Opportunity opp = new Opportunity();
opp.Name = 'Test Opportunity';
opp.CloseDate = system.today();
opp.StageName = 'Qualification';
opp.Type = 'New Customer';
insert opp;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
attach.body = bodyBlob;
attach.parentId = Opp.id;
insert attach;
}
}
i don't know why the apex class is not working. Iried deploy it in my production but it says 0% code coverage.
Please try below code:
As your trigger will execute on "before update", you need to update the record once in Test Class. Please try and let me know in case of any isuee.
All Answers
Please try below code:
As your trigger will execute on "before update", you need to update the record once in Test Class. Please try and let me know in case of any isuee.