function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve LoudermilkSteve Loudermilk 

Test class for attachment help

I am basically writing that attachment ID to a custome field so I can display it on a page.  I have the trigger written which I will add below, but I am having trouble with the test class. I went down the pat of "loading" in a record an attachment, but haven't managed to get coverage yet.  If someoen could help me out I would be very appreciative.

Thanks in advance,

Milk
 
trigger MoveAttachmentIDforFormula on Attachment (after insert) {
    Map<Id, Id> mapCSId = new map<Id, Id>();
    List<InspectTHIS__Applied_Questionnaire__c> dct = new List<InspectTHIS__Applied_Questionnaire__c>();
    String objectName = '';
    for(Attachment atc: trigger.new){
        String myIdPrefix = string.valueOf(atc.ParentId).substring(0,3);
        Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 
        for(Schema.SObjectType stype : gd.values()){
            Schema.DescribeSObjectResult r = stype.getDescribe();
            String prefix = r.getKeyPrefix();
            if(prefix!=null && prefix.equals(myIdPrefix)){
                objectName = r.getName();
            }
        }
        if(objectName == 'InspectTHIS__Applied_Questionnaire__c'){
            mapCSId.put(atc.ParentId, atc.id);
        }
    }
    if(!mapCSId.isEmpty()){
        List<InspectTHIS__Applied_Questionnaire__c> lstdct = [select id from InspectTHIS__Applied_Questionnaire__c where id in: mapCSId.keyset()];
        for(InspectTHIS__Applied_Questionnaire__c cs: lstdct){
            cs.AttachmentId__c = mapCSId.get(cs.id);
        }
        if(!lstdct.isEmpty()){
            update lstdct;
        }
    }
    
}

 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Steve,

May I request you please refer the below link for Test class for attachment. I hope it will be helpful.

Best Regards
Rahul Kumar
Steve LoudermilkSteve Loudermilk
Hi Rahul,

        Thanks for the suggestions, but I have looked through those examples previously.  I am more struggling wiht the getkeyprefix() and the corresponding keyset.

Steve