You need to sign in to do that
Don't have an account?
SFDC 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
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
Here is the updated code:
trigger will be on attachment object.
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
All Answers
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
Below is the updated code:
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
The above trigger didnt work , its still not making the checkbox to true
Here is the updated code:
trigger will be on attachment object.
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal