You need to sign in to do that
Don't have an account?
gowtham murugesan
Hi everyone,I need to write a test class for this attachment url link trigger.kindly help me out.
This is my code:
trigger attachmentopp on Attachment (After Insert) {
String parentId;
String TitleName;
Set<Id> oppId = new Set<Id>();
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
List<Opportunity> oppUpd = new List<Opportunity>();
String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
for(Attachment att:Trigger.new){
parentId = att.parentId;
TitleName = att.Name;
if(parentId.startsWith('006') && TitleName.startsWith('qrs') ){
oppId.add(att.parentId);
}
}
for(Attachment att: [SELECT Id,ParentId FROM Attachment WHERE ParentId IN:
oppId ORDER BY Createddate ASC]){
Opportunity opp = new Opportunity();
opp.Id = att.ParentId;
opp.Attachment_Link__c = baseUrl+'/'+att.Id;
oppMap.put(opp.Id, opp);
}
update oppMap.values();
}
}
trigger attachmentopp on Attachment (After Insert) {
String parentId;
String TitleName;
Set<Id> oppId = new Set<Id>();
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
List<Opportunity> oppUpd = new List<Opportunity>();
String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)){
for(Attachment att:Trigger.new){
parentId = att.parentId;
TitleName = att.Name;
if(parentId.startsWith('006') && TitleName.startsWith('qrs') ){
oppId.add(att.parentId);
}
}
for(Attachment att: [SELECT Id,ParentId FROM Attachment WHERE ParentId IN:
oppId ORDER BY Createddate ASC]){
Opportunity opp = new Opportunity();
opp.Id = att.ParentId;
opp.Attachment_Link__c = baseUrl+'/'+att.Id;
oppMap.put(opp.Id, opp);
}
update oppMap.values();
}
}
Try this code:
@isTest
public class attachmentopp_test {
@isTest static void testAttachmentopp() {
Opportunity testOpp = new Opportunity();
testOpp.Name='Test Opportunity' ;
testOpp.StageName='Order Placed';
testOpp.CloseDate= Date.newInstance(2019,03,28);
insert testOpp;
Blob b = Blob.valueOf('Test Data');
Attachment attachment = new Attachment();
attachment.ParentId = testOpp.Id;
attachment.Name = 'qrs Attachment for Parent';
attachment.Body = b;
insert(attachment);
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Try this code:
@isTest
public class attachmentopp_test {
@isTest static void testAttachmentopp() {
Opportunity testOpp = new Opportunity();
testOpp.Name='Test Opportunity' ;
testOpp.StageName='Order Placed';
testOpp.CloseDate= Date.newInstance(2019,03,28);
insert testOpp;
Blob b = Blob.valueOf('Test Data');
Attachment attachment = new Attachment();
attachment.ParentId = testOpp.Id;
attachment.Name = 'qrs Attachment for Parent';
attachment.Body = b;
insert(attachment);
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi