+ Start a Discussion
SFDC n12SFDC n12 

new trigger help

Hi,

Iam having a trigger on a custom object which will update the checkbox to true if there is any attachment attached to the "Notes and Attachments" related list

But its not updating to true after attaching the record 


My Trigger

trigger Validate_attachment on Special_Programs_ADR_Exception__c (after insert) {
    List<id> AttachmentIds=New List<id>();
    Set<String> adrIdSet = new Set<String>();
    List< Special_Programs_ADR_Exception__c> updAdrList = new List< Special_Programs_ADR_Exception__c>();

     for(Special_Programs_ADR_Exception__c adr: Trigger.new){
                adrIdSet.add(adr.Id);
    }

    List<Attachment> notesList  =  [Select ParentId from Attachment where ParentId IN : adrIdSet];
   if(notesList!= null && notesList.size()>0){
                for(Special_Programs_ADR_Exception__c adr: Trigger.new){
                         adr.Attachments__c =true;
                          updAdrList.add(adr);
                               system.debug('list'+updAdrList);
               }
                                               
     try{
                Update updAdrList;
      }
       Catch(Exception e){
                 
   system.debug('Update failed'+ + e.getMessage());
    }
     system.debug('test'+updAdrList);

}
}


Let me know whats the issue on it


Thanks in Advance
Best Answer chosen by SFDC n12
Vatsal KothariVatsal Kothari
+1 Adrian.

Here is the updated code:

trigger validate_attachment on Attachment (after insert,after update) {
	
	Map<Id,Id> attachmentMap = new Map<Id,Id>();
	List<Special_Programs_ADR_Exception__c> adrList = new List<Special_Programs_ADR_Exception__c>();
	
	for(Attachment at  : trigger.new){
		attachmentMap.put(at.ParentId,at.Id);
	}

	for(Special_Programs_ADR_Exception__c adr:[select Id,Attachments__c from Special_Programs_ADR_Exception__c]){
		if(attachmentMap.containsKey(adr.Id) && attachmentMap.get(adr.Id) != null){
			adr.Attachments__c = true;
			adrList.add(adr);
		}
	}
	
	if(adrList.size() > 0){
		update adrList;
	}
	
}
trigger will be on attachment object.

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

AdrianCCAdrianCC
Hi,

You are firing the trigger on the wrong object. The way it is now it will fire on the parent of the Attachment when it is inserted. It will not have any attachments at that time. 
Try to change it to fire on Attachment sobject(for Note as well) and check if the parent is a Special_Programs_ADR_Exception__c record(use the first 3 chars of the Id). If it is then update the parent record with that checkbox checked

Ty,
Adrian
Vatsal KothariVatsal Kothari
Hi,

Below is the updated code:

trigger Validate_attachment on Special_Programs_ADR_Exception__c (before insert) {
	Set<Id> adrIdSet = new Set<Id>();
	Map<Id,Id> attachmentMap = new Map<Id,Id>();
	
	for(Special_Programs_ADR_Exception__c adr: Trigger.new){
		adrIdSet.add(adr.Id);
	}

	for(Attachment at  :  [Select Id,ParentId from Attachment where ParentId IN : adrIdSet]){
		attachmentMap.put(at.ParentId,at.Id);
	}

	for(Special_Programs_ADR_Exception__c adr: Trigger.new){
		if(attachmentMap.containsKey(adr.Id) && attachmentMap.get(adr.Id) != null){
			adr.Attachments__c = true;
		}
	}
}

make it before insert so you dont required to do any DML operation.

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
SFDC n12SFDC n12
Hi Vatsal,

The above trigger didnt work , its still not making the checkbox to true
Vatsal KothariVatsal Kothari
+1 Adrian.

Here is the updated code:

trigger validate_attachment on Attachment (after insert,after update) {
	
	Map<Id,Id> attachmentMap = new Map<Id,Id>();
	List<Special_Programs_ADR_Exception__c> adrList = new List<Special_Programs_ADR_Exception__c>();
	
	for(Attachment at  : trigger.new){
		attachmentMap.put(at.ParentId,at.Id);
	}

	for(Special_Programs_ADR_Exception__c adr:[select Id,Attachments__c from Special_Programs_ADR_Exception__c]){
		if(attachmentMap.containsKey(adr.Id) && attachmentMap.get(adr.Id) != null){
			adr.Attachments__c = true;
			adrList.add(adr);
		}
	}
	
	if(adrList.size() > 0){
		update adrList;
	}
	
}
trigger will be on attachment object.

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

This was selected as the best answer
CJagannathCJagannath
The apex trigger should be written in attachment object since the trigger event (i.e. attaching/ uploading an attachment) is in that object. Using developer console you should write an apex trigger on attachment and update the parent sobjects attributes as desired.