• SFDC 13
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Here is the code but it won't work properly if I attached document in the notes and Attachment section then also it will throw an error. if any one know then please help me.
Code::  

trigger closeWonMustAttach on Opportunity (before insert, before update) {
  // Find all opportunities and their list of attachments...
  Map<Id,Opportunity> opportunityAttachments = new Map<Id,Opportunity>(
    [SELECT Id,(SELECT Id FROM Attachments) FROM Opportunity WHERE Id IN :Trigger.new]
  );
  // For each opportunity being created or modified...
  for(Opportunity opp:Trigger.new) {
    if(opp.StageName=='Closed Won' && // If it is changing to closed/won...
      (!opportunityAttachments.containsKey(opp.id) || // And the opportunity was not found (Insert)...
       opportunityAttachments.get(opp.id).Attachments == null || // Or the opportunity attachment list was null...
       opportunityAttachments.get(opp.id).Attachments.size()==0)) { // Or the attachment list has no entries...
      opp.StageName.addError('You must first attach a file to this opportunity before changing to Closed/Won.'); // So we prevent saving here.
    }
  }
}
  • September 20, 2021
  • Like
  • 0