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
shalushalu 

create one filed in any object like account or contact..create field "attachment obtained" checkbox.if file is attached checkbox should be check .otherwise file is deleted means checkbox should uncheck using triggers ...can anyone help this questions?

jigarshahjigarshah
Swathy,

You can create an after insert trigger on Attachment that counts the associated Attachments and accordingly updates the AttachmentObtained__c  checkbox. The trigger code is as below.
 
trigger AttachmentObtainedTrigger on Attachment (after insert) {

	if(Trigger.isAfter){
	
		if(Trigger.isInsert){
		
			Set<Id> accountRecordIdSet = new Set<Id>();
			Set<Id> contactRecordIdSet = new Set<Id>();

			for(Attachment attachmentRec :Trigger.new){

				//Check if the Parent is an Account
				if(attachmentRec.ParentId.startsWith('001'){
					accountRecordIdSet.add(attachmentRec.ParentId);
				} 
				
				//Check if the Parent is an Contact
				if(attachmentRec.ParentId.startsWith('003'){
					contactRecordIdSet.add(attachmentRec.ParentId);
				}
			}//for
			
			//Update AttachmentObtained__c for Account
			if(!accountRecordIdSet.isEmpty()){
			
				for(Account[] accountList :[Select Id, AttachmentObtained__c 
											From Account 
											Where Id IN :accountRecordIdSet]){
				
					for(Account accRec :accountList){
					
						if(accRec != null){
							if(accRec.AttachmentObtained__c == false){
								accRec.AttachmentObtained__c = true;
							}
						}
					}
					
					update(accountList);
				}
			}//if
			
			//Update AttachmentObtained__c for Contact
			if(!contactRecordIdSet.isEmpty()){
			
				for(Contact[] contactList :[Select Id, AttachmentObtained__c 
											From Contact 
											Where Id IN :accountRecordIdSet]){
				
					for(Contact contactRec :contactList){
					
						if(contactRec != null){
							if(contactRec.AttachmentObtained__c == false){
								contactRec.AttachmentObtained__c = true;
							}
						}
					}
					
					update(contactList);
				}
			}//if
			
		}
	}
	
}//AttachmentObtainedTrigger ends
shalushalu
i need trigger on account object ...that same requirement
jigarshahjigarshah
Here you go.
 
trigger AccountTrigger on Account (before insert, before update){

	if(Trigger.isBefore){
	
		if(Trigger.isInsert || Trigger.isUpdate){
		
			for(Account accountRec :[Select Id, AttachmentObtained__c, 
										(Select Id, ParentId From Attachment)
									 From Account 
									 Where Id IN :Trigger.newMap.keySet()]){
			
				if(accountRec.Attachments.size() > 0 && 
				   accountRec.AttachmentObtained__c == false){
				
					accountRec.AttachmentObtained__c = true;
				}
			}//for
		}
	}
}//AccountTrigger ends
jigarshahjigarshah
Please close this thread if your issue has been resolved.