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
Saurabh SmartySaurabh Smarty 

Hi All, I have wrote a trigger that updates a field if an attachment is uploaded in the object, but I want to get the field update only when the attachment's name has certain words in it, how can I accomplish that ?

Below is the trigger :
trigger attach on Attachment (after insert) {
public List<opportunity> oppoList = new List<opportunity>();
   public Set<Id> oppIds = new Set<Id>();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Opportunity.SobjectType && att.name ??? ){
              oppIds.add(att.ParentId);
         }
        system.debug('for loop completed');
    }
    oppoList = [select id from Opportunity where id in :oppIds];
    if(oppoList!=null && oppoList.size()>0){
        system.debug('in if condition');
        for(Opportunity op : oppoList){
            op.attach_upload__c = true;
        }
        system.debug('2nd for loop completed');
        
        update oppoList;
        system.debug('update completed');
    }
}
Best Answer chosen by Saurabh Smarty
v varaprasadv varaprasad
Hi Saurabh,

You can use below string method it is a case not sensitive.
 
string myString1 = 'abcd';
String myString2 = 'CD';
Boolean result = myString1.containsIgnoreCase(myString2);
system.debug('==result=='+result);
Instead of attachname.contains('SFDC'); you can use attachname.containsIgnoreCase('SFDC');


Hope it helps you.
Please let me know in case of other assistance.

Thanks
Varaprasad

All Answers

v varaprasadv varaprasad
Hi Saurabh,


By default attachment name == document name. 
Please check once below code :
trigger attach on Attachment (after insert) {
public List<opportunity> oppoList = new List<opportunity>();
   public Set<Id> oppIds = new Set<Id>();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
		 string attachname = att.name;
		 system.debug('==attachname=='+attachname);
         if(att.ParentId.getSobjectType() == Opportunity.SobjectType && attachname.contains('SFDC');){
              oppIds.add(att.ParentId);
         }
        system.debug('for loop completed');
    }
    oppoList = [select id from Opportunity where id in :oppIds];
    if(oppoList!=null && oppoList.size()>0){
        system.debug('in if condition');
        for(Opportunity op : oppoList){
            op.attach_upload__c = true;
        }
        system.debug('2nd for loop completed');
        
        update oppoList;
        system.debug('update completed');
    }
}

=======================
If you have any specific words then add all words to string nd check like blow : 

Set<String> validCountries = new Set<String>{'Germany', 'India', 'Japan', 'United States'};
    if (validCountries.contains('India')) {
        system.debug('do your logic');
    }

hope it helps you.
Please let me know in case of any other help.


Thanks
Varaprasad



 
Saurabh SmartySaurabh Smarty
Thanks Prasad for the response, contains is working fine but the only problem is it is case sensitive, is there any workaround to this?
v varaprasadv varaprasad
Hi Saurabh,

You can use below string method it is a case not sensitive.
 
string myString1 = 'abcd';
String myString2 = 'CD';
Boolean result = myString1.containsIgnoreCase(myString2);
system.debug('==result=='+result);
Instead of attachname.contains('SFDC'); you can use attachname.containsIgnoreCase('SFDC');


Hope it helps you.
Please let me know in case of other assistance.

Thanks
Varaprasad
This was selected as the best answer
Saurabh SmartySaurabh Smarty
Thanks Prasad , this worked!!