+ Start a Discussion
gowtham murugesangowtham 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();
    }
}
Best Answer chosen by gowtham murugesan
Ajay K DubediAjay K Dubedi
Hi Gowtham,
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

Ajay K DubediAjay K Dubedi
Hi Gowtham,
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
This was selected as the best answer
gowtham murugesangowtham murugesan
Thankyou ajay .The code is working Fine.