You need to sign in to do that
Don't have an account?
ankitsrivastav7771.3911494159052937E12
How to cover test class for attachment trigger its getting covered if statement from line 13 to 21
TRIGGER-----
trigger Rfleet_DisableAttachements on Attachment(before insert,before delete,after update) {
Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RFLEET-OPP-DCVF-RT').getRecordTypeId();
Id devRecordTypeId1 = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DC-VF Read only RT').getRecordTypeId();
if(trigger.isinsert && trigger.isbefore){
for (Attachment att:Trigger.new){
String parentObjId = att.ParentId; //It will get profitability obj id.
list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
for(Rfleet_Profitability__c pro:ids){
for(opportunity opp:pid){
if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId )//a3r is the starting sting in ID for all Profitability
{
att.addError('Since this opportunity was submitted for approval, you are not allowed to add a new attachment.');
}
else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
{
att.addError('Since this opportunity was closed, you are not allowed to add a new attachment.');
}
}
}
}
}
if(trigger.isdelete && trigger.isbefore){
for (Attachment att:Trigger.old){
String parentObjId = att.ParentId;
list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
for(Rfleet_Profitability__c pro:ids){
for(opportunity opp:pid){
if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId)//a3r is the starting sting in ID for all Profitability
{
att.addError('Since this opportunity was submitted for approval, you are not allowed to delete the attachment.');
}
else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
{
att.addError('Since this opportunity was closed, you are not allowed to delete the attachment.');
}
}
}
}
}
if(trigger.isupdate && trigger.isafter){
for (Attachment att:Trigger.new){
String parentObjId = att.ParentId;
list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
for(Rfleet_Profitability__c pro:ids){
for(opportunity opp:pid){
if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId)//a3r is the starting sting in ID for all Profitability
{
att.addError('Since this opportunity was submitted for approval, you are not allowed to update the attachment.');
}
else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
{
att.addError('Since this opportunity was closed, you are not allowed to update the attachment.');
}
}
}
}
}
}
@testclass
@isTest(SeeAllData=true)
public class Rfleet_DisableAttachements_Test {
static testMethod void Rfleet_DisableAttachements(){
Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RFLEET-OPP-DCVF-RT').getRecordTypeId();
Id devRecordTypeId1 = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DC-VF Read only RT').getRecordTypeId();
//RecordType rt = [select Id from RecordType where Name = 'RFLEET-OPP-DCVF-RT' and SobjectType = 'opportunity' LIMIT 1];
Opportunity opp=new Opportunity();
//opp.RecordTypeId = rt.id;
opp.RecordTypeId=devRecordTypeId ;
opp.Rfleet_locked__c = True;
opp.name='fdf';
//opp.id='a3rm0000000D6Z5';
//opp.id==pro.Rfleet_opportunity__c
opp.stagename='Internal Approbation';
opp.closedate=System.Today();
insert opp;
opp.stagename='Proposal to Customer';
update opp;
Attachment attach=new Attachment();
//attach.id=opp.Id;
attach.Name='TestAtt';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=opp.Id;
insert attach;
Rfleet_Profitability__c Pro=new Rfleet_Profitability__c();
pro.Rfleet_opportunity__c=opp.Id;
//pro.id=attach.id;
// pro.id='a3rm0000000D6Z5';
try{
insert pro;
}catch (DMLException e) {}
List<Attachment> attachments=[select id, name from Attachment where parent.id=:opp.id ];
System.assertEquals(1, attachments.size());
attach = [SELECT Id, name from Attachment where parent.id=:opp.id];
//System.assertEquals(pro.Id, attach.ParentId);
update attach;
delete attach;
}
}
trigger Rfleet_DisableAttachements on Attachment(before insert,before delete,after update) {
Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RFLEET-OPP-DCVF-RT').getRecordTypeId();
Id devRecordTypeId1 = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DC-VF Read only RT').getRecordTypeId();
if(trigger.isinsert && trigger.isbefore){
for (Attachment att:Trigger.new){
String parentObjId = att.ParentId; //It will get profitability obj id.
list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
for(Rfleet_Profitability__c pro:ids){
for(opportunity opp:pid){
if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId )//a3r is the starting sting in ID for all Profitability
{
att.addError('Since this opportunity was submitted for approval, you are not allowed to add a new attachment.');
}
else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
{
att.addError('Since this opportunity was closed, you are not allowed to add a new attachment.');
}
}
}
}
}
if(trigger.isdelete && trigger.isbefore){
for (Attachment att:Trigger.old){
String parentObjId = att.ParentId;
list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
for(Rfleet_Profitability__c pro:ids){
for(opportunity opp:pid){
if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId)//a3r is the starting sting in ID for all Profitability
{
att.addError('Since this opportunity was submitted for approval, you are not allowed to delete the attachment.');
}
else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
{
att.addError('Since this opportunity was closed, you are not allowed to delete the attachment.');
}
}
}
}
}
if(trigger.isupdate && trigger.isafter){
for (Attachment att:Trigger.new){
String parentObjId = att.ParentId;
list<opportunity> pid= [select Rfleet_locked__c,recordtypeid from Opportunity where Rfleet_locked__c=true ];// it get list of opp locked records.
list<Rfleet_Profitability__c> ids=[Select Rfleet_opportunity__c from Rfleet_Profitability__c where id=:att.ParentId];
for(Rfleet_Profitability__c pro:ids){
for(opportunity opp:pid){
if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId)//a3r is the starting sting in ID for all Profitability
{
att.addError('Since this opportunity was submitted for approval, you are not allowed to update the attachment.');
}
else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
{
att.addError('Since this opportunity was closed, you are not allowed to update the attachment.');
}
}
}
}
}
}
@testclass
@isTest(SeeAllData=true)
public class Rfleet_DisableAttachements_Test {
static testMethod void Rfleet_DisableAttachements(){
Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RFLEET-OPP-DCVF-RT').getRecordTypeId();
Id devRecordTypeId1 = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DC-VF Read only RT').getRecordTypeId();
//RecordType rt = [select Id from RecordType where Name = 'RFLEET-OPP-DCVF-RT' and SobjectType = 'opportunity' LIMIT 1];
Opportunity opp=new Opportunity();
//opp.RecordTypeId = rt.id;
opp.RecordTypeId=devRecordTypeId ;
opp.Rfleet_locked__c = True;
opp.name='fdf';
//opp.id='a3rm0000000D6Z5';
//opp.id==pro.Rfleet_opportunity__c
opp.stagename='Internal Approbation';
opp.closedate=System.Today();
insert opp;
opp.stagename='Proposal to Customer';
update opp;
Attachment attach=new Attachment();
//attach.id=opp.Id;
attach.Name='TestAtt';
Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
attach.body=bodyBlob;
attach.parentId=opp.Id;
insert attach;
Rfleet_Profitability__c Pro=new Rfleet_Profitability__c();
pro.Rfleet_opportunity__c=opp.Id;
//pro.id=attach.id;
// pro.id='a3rm0000000D6Z5';
try{
insert pro;
}catch (DMLException e) {}
List<Attachment> attachments=[select id, name from Attachment where parent.id=:opp.id ];
System.assertEquals(1, attachments.size());
attach = [SELECT Id, name from Attachment where parent.id=:opp.id];
//System.assertEquals(pro.Id, attach.ParentId);
update attach;
delete attach;
}
}
for(opportunity opp:pid){
if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId )//a3r is the starting sting in ID for all Profitability
{
att.addError('Since this opportunity was submitted for approval, you are not allowed to add a new attachment.');
}
else if(parentObjId.startsWith('a3r') && opp.Rfleet_locked__c==true && opp.id==pro.Rfleet_opportunity__c && opp.recordtypeid==devRecordTypeId1)
{
att.addError('Since this opportunity was closed, you are not allowed to add a new attachment.')