You need to sign in to do that
Don't have an account?
sabeera
Please help me to write test classes for the following trigger
Hi,
I tried some code, but it is not working.
/Trigger to Count the Number of Attachments on an Object
trigger countattachment on Attachment (after insert, after update, after delete, after undelete) {
Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
set<id> attids = new set<id>();
if(Trigger.new<>null){
for(Attachment c:Trigger.new){
MDF__c l;
if(c.ParentId != null)
attids.add(c.parentid);
}
}else if(Trigger.old != null){
for(Attachment c:Trigger.old){
if(c.ParentId<>null)
attids.add(Trigger.oldMap.get(c.id).parentid);
}
}
if(attids.size()>0){
try{
List<Attachment> a = new List<Attachment>();
Map<id,MDF__c> testmap = new Map<id,MDF__c>([select id,CountAttachment__c from MDF__c where id IN: attids]);
a = [select id,parentid from Attachment where parentid IN:attids];
for(Attachment at: a){
List<Attachment> llist = new List<Attachment>();
if(parent.get(at.parentid) == null){
llist = new List<Attachment>();
llist.add(at);
parent.put(at.parentid,llist);
}else if(parent.get(at.parentid) != null){
llist = new List<Attachment>();
llist = parent.get(at.parentid);
llist.add(at);
parent.put(at.parentid,llist);
}
}
for(Id i: attids){
if(testmap.get(i) != null && parent.get(i) != null){
testmap.get(i).CountAttachment__c = parent.get(i).size();
}else if(testmap.get(i) != null && parent.get(i) == null){
testmap.get(i).CountAttachment__c = 0;
}
}
update testmap.values();
System.Debug(testmap.values());
}catch(Exception e){}
}
}
I tried some code, but it is not working.
/Trigger to Count the Number of Attachments on an Object
trigger countattachment on Attachment (after insert, after update, after delete, after undelete) {
Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
set<id> attids = new set<id>();
if(Trigger.new<>null){
for(Attachment c:Trigger.new){
MDF__c l;
if(c.ParentId != null)
attids.add(c.parentid);
}
}else if(Trigger.old != null){
for(Attachment c:Trigger.old){
if(c.ParentId<>null)
attids.add(Trigger.oldMap.get(c.id).parentid);
}
}
if(attids.size()>0){
try{
List<Attachment> a = new List<Attachment>();
Map<id,MDF__c> testmap = new Map<id,MDF__c>([select id,CountAttachment__c from MDF__c where id IN: attids]);
a = [select id,parentid from Attachment where parentid IN:attids];
for(Attachment at: a){
List<Attachment> llist = new List<Attachment>();
if(parent.get(at.parentid) == null){
llist = new List<Attachment>();
llist.add(at);
parent.put(at.parentid,llist);
}else if(parent.get(at.parentid) != null){
llist = new List<Attachment>();
llist = parent.get(at.parentid);
llist.add(at);
parent.put(at.parentid,llist);
}
}
for(Id i: attids){
if(testmap.get(i) != null && parent.get(i) != null){
testmap.get(i).CountAttachment__c = parent.get(i).size();
}else if(testmap.get(i) != null && parent.get(i) == null){
testmap.get(i).CountAttachment__c = 0;
}
}
update testmap.values();
System.Debug(testmap.values());
}catch(Exception e){}
}
}
Replace this code and check .Any issue ask me directly .
Regards,
Harish.R.
All Answers
I am writen in org it's getting 100% check it any issue let me know .
01@isTest
02Public class harishraoTeat{
03
04
05 Public static testMethod void harish1() {
06
07
08 string emailid = 'harish.rao.salesforce@gmail.com';
09
10 string posswd = '9676930011';
11
12
13apexpages.currentpage().getparameters().put('Email' , emailid );
14
15apexpages.currentpage().getparameters().put('pwd' , posswd );
16
17
18apexpages.currentpage().getparameters().put('Email' , emailid );
19
20
21
22
23 CustomLogin login = new CustomLogin();
24
25
26 login.getEmail();
27
28 login.getpwd();
29
30 login.login();
31
32
33
34
35
36
37 }
38}
If it is usefull make it best answer .
@isTest
public class CustomLoginTest
{
public static testMethod void loginPass()
{
CustomLogin c = new CustomLogin();
String Email='test@yopmail.com';
String pwd='12345';
c.getEmail();
c.getpwd();
c.login();
}
}
It is also shows 100% is it fine? or can i use ur code?
The below code is cover only 64% only.
@isTest
public class countattachmenttest{
Private Static testmethod void countattachmenttest(){
MDF__C m = new MDF__C();
m.Name='Test';
m.Amount__c=500;
insert m;
Attachment attach=new Attachment();
attach.Name='Unit Test Attachment';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=m.id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:m.id];
System.assertEquals(1, attachments.size());
}
}
Replace this code and check .Any issue ask me directly .
Regards,
Harish.R.
Let Me know it's working or not .
Regards,
Harish.R.
Its working good and Code coverage 96%.
Your code is not covered all lines except below line
catch(Exception e){}
catch(Exception e){}
Write any exeption in test class like .if any error get exeption also cover .
Any query ask dirctly Harish.rao.salesforce@gmail.com
Regards,
Harish.R.