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
Ashley ShealeyAshley Shealey 

Checkbox Field when an attachment is added with specified keywords

I currently have a trigger that checks a box when an attachment is added to a custom object called "Contracts__c". I would like to expand this Trigger to only check the box if the actual attatched document includes certain keywords. Is this possible? Here is my current code:

trigger CountAttachment on Attachment (before insert, before delete)
{
if(trigger.isinsert){
List<Contracts__c> co = [select id from Contracts__c where id =: Trigger.New[0].ParentId];
If(co.size()>0)        
{            
co[0].Contract_Attached__c = True;            
update co;        
}
}


if(trigger.isdelete){

List<Contracts__c> co = [select id from Contracts__c where id =: Trigger.old[0].ParentId];        
If(co.size()>0)        
{            
co[0].Contract_Attached__c = false;            
update co;        
}
}
}
Best Answer chosen by Ashley Shealey
hitesh90hitesh90
Hello Ashley,

I have checked your trigger code. it is not bulify. it will work only for single attachment. and also in this trigger you can't put filter for Keywords which you want. Please see below trigger code which will fulfill your requirement. don't forget to replace 'Your Key word' in this trigger.

Apex Trigger:
trigger CountAttachment on Attachment (after insert, after delete){
    set<Id> sContractId = new set<Id>();
    String objectName = '';
    if(trigger.isinsert){
        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 == 'Contracts__c'){
                if(atc.Name.containsIgnoreCase('Your Key word')){
                    sContractId.add(atc.ParentId);
                }
            }
        }
    }
    if(!sContractId.isEmpty()){
        List<Contracts__c> lstContracts = [select id from Contracts__c where id in: sContractId];
        for(Contracts__c con: lstContracts){
            con.Contract_Attached__c = true;
        }
        if(!lstContracts.isEmpty()){
            update lstContracts;
        }
    }
    
    set<Id> sContractAtcDel = new set<Id>();
    if(trigger.isdelete){
        for(Attachment atc: trigger.old){
            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 == 'Contracts__c'){
                sContractAtcDel.add(atc.ParentId);
            }
        }
    }
    if(!sContractAtcDel.isEmpty()){
        List<Contracts__c> lstContracts = [select id from Contracts__c where id in: sContractAtcDel];
        for(Contracts__c con: lstContracts){
            con.Contract_Attached__c = false;
        }
        if(!lstContracts.isEmpty()){
            update lstContracts;
        }
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90
Hello Ashley,

I have checked your trigger code. it is not bulify. it will work only for single attachment. and also in this trigger you can't put filter for Keywords which you want. Please see below trigger code which will fulfill your requirement. don't forget to replace 'Your Key word' in this trigger.

Apex Trigger:
trigger CountAttachment on Attachment (after insert, after delete){
    set<Id> sContractId = new set<Id>();
    String objectName = '';
    if(trigger.isinsert){
        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 == 'Contracts__c'){
                if(atc.Name.containsIgnoreCase('Your Key word')){
                    sContractId.add(atc.ParentId);
                }
            }
        }
    }
    if(!sContractId.isEmpty()){
        List<Contracts__c> lstContracts = [select id from Contracts__c where id in: sContractId];
        for(Contracts__c con: lstContracts){
            con.Contract_Attached__c = true;
        }
        if(!lstContracts.isEmpty()){
            update lstContracts;
        }
    }
    
    set<Id> sContractAtcDel = new set<Id>();
    if(trigger.isdelete){
        for(Attachment atc: trigger.old){
            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 == 'Contracts__c'){
                sContractAtcDel.add(atc.ParentId);
            }
        }
    }
    if(!sContractAtcDel.isEmpty()){
        List<Contracts__c> lstContracts = [select id from Contracts__c where id in: sContractAtcDel];
        for(Contracts__c con: lstContracts){
            con.Contract_Attached__c = false;
        }
        if(!lstContracts.isEmpty()){
            update lstContracts;
        }
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
This was selected as the best answer
Ashley ShealeyAshley Shealey
This works great. Thank you!
Mike Morrison 8Mike Morrison 8
Do you have the Test Class for this trigger.  I modified the object for my use but could use some help with the test class for depolyment.  Thank you.
Christophe Lereverend 4Christophe Lereverend 4
Does it work on a Lightning page? 

Functionality does works on Classic but not on the lightning page?  any advice welcome.