You need to sign in to do that
Don't have an account?

how to write test class for the attachments ???
Hi Team,
I need to make mandatory attachment Probability is 100 %, also Check box field(Invoiced__c) is true then attchment is mandatory.
I write the below trigger and it working fine. I don't how can i write the test class for that trigger.
@isTest
private class ClosedOpportuntyTriggerTest{
static testmethod void methodOne(){
/*contact objc = new contact();
objc.Lastname = 'test';
objc.Email ='umadevi.s@bookmyshow.com';
insert objc;*/
Opportunity objOpp = new Opportunity();
objOpp.Name = 'test opp';
objOpp.StageName = 'Payment Receiveed';
objOpp.Probability = 80;
// objOpp.Contact__c = objc.id;
objOpp.CloseDate = system.today();
objOpp.Booking_Ids__c ='12345';
insert objOpp;
blob attBlob;
attBlob = EncodingUtil.base64Decode('AA=='); // zero-length, null-terminated string
Attachment objAttach = new Attachment();
objAttach.OwnerId = UserInfo.getUserId();
objAttach.Name = 'test.pdf';
objAttach.ParentId = objOpp.Id; // the record the file is attached to
objAttach.IsPrivate = true;
objAttach.Body = attBlob;
objAttach.ContentType = 'application/pdf';
insert objAttach;
objOpp.StageName = 'Payment Confirmation';
objOpp.Probability = 100;
objOpp.Invoiced__c = true;
objOpp.Probability = 100;
update objOpp;
}
}
And i got Error Add one Product
I need to make mandatory attachment Probability is 100 %, also Check box field(Invoiced__c) is true then attchment is mandatory.
I write the below trigger and it working fine. I don't how can i write the test class for that trigger.
trigger ClosedOpportunityTrigger on Opportunity (before update) { for(Opportunity o:Trigger.New) { if(o.Probability==100 && o.Invoiced__c == true) { 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 close the Opportunity'); } } }This my Test Class
@isTest
private class ClosedOpportuntyTriggerTest{
static testmethod void methodOne(){
/*contact objc = new contact();
objc.Lastname = 'test';
objc.Email ='umadevi.s@bookmyshow.com';
insert objc;*/
Opportunity objOpp = new Opportunity();
objOpp.Name = 'test opp';
objOpp.StageName = 'Payment Receiveed';
objOpp.Probability = 80;
// objOpp.Contact__c = objc.id;
objOpp.CloseDate = system.today();
objOpp.Booking_Ids__c ='12345';
insert objOpp;
blob attBlob;
attBlob = EncodingUtil.base64Decode('AA=='); // zero-length, null-terminated string
Attachment objAttach = new Attachment();
objAttach.OwnerId = UserInfo.getUserId();
objAttach.Name = 'test.pdf';
objAttach.ParentId = objOpp.Id; // the record the file is attached to
objAttach.IsPrivate = true;
objAttach.Body = attBlob;
objAttach.ContentType = 'application/pdf';
insert objAttach;
objOpp.StageName = 'Payment Confirmation';
objOpp.Probability = 100;
objOpp.Invoiced__c = true;
objOpp.Probability = 100;
update objOpp;
}
}
And i got Error Add one Product
- To write a test class for the attachments.May I suggest you please refer the below link to reference.
- https://developer.salesforce.com/forums/?id=906F00000008yzKIAQ
- https://developer.salesforce.com/forums/?id=906F0000000g2BtIAI
hope it helps.Please mark it as best answer if the information is informative.
Thanks
Rahul Kumar
this is fairly simple , you don't need to pass the Ids or insert attachment (trigger will do that for you) as you were doing in your test class.
just insert the opp and update it.( as trigger is fired on update).
Please mark it as best answer if this answer helps you.
Thanks
Aman Pathak
"just add a try catch block when you update Opp in test class , that will do.
this FIELD_CUSTOM_VALIDATION_EXCEPTION is due to " o.addError('Add an attachment before you close the Opportunity');"
below is the working code
with 100% coverage and all methods passed.
Please mark it as best answer if this answer helps you.
Thanks
Aman Pathak
I got the error msg like need add the atleast one Product for the Opportunity.
for product , you have to use opportunitylineItem or may be Product2 intest class ,I am not sure of that(depend on your org) Please check.
also I quite didn't understant your code ,
you are setting "a = null "in Catch block , why?
you can add the error in catch block itself