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
WebNXWebNX 

Apex Trigger Email

Can some please help me. I am trying to code an apex trigger to email, whenever there is an attachment to a contract. Also, inside the body of the trigger email have a link to the contract that has the insert attachment, as well as having the name and contract id information in the body and subject line of the email. I get an error message on line 24, stating that; "Save error: Initial term of field expression must be a concrete SObject: MAP<Id,Contract>." Here is the my code below:

 

trigger NoteAttachmentTrigger on Attachment (after insert) {
 
 List<Id> contIds = new List<Id>();
 
 for (Attachment att : trigger.new){
  contIds.add(att.parentId);
 }
 
 Map<Id, Contract> contracts = new Map<Id, Contract>();
 contracts.putAll([Select c.Id, c.Name, c.Contract_ID__c, c.ContractNumber From Contract c Where Id IN:contIds]);
 
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
 String[] toAddresses = new String []{'email@example.com'};
 
 mail.setToAddresses(toAddresses);
 mail.setReplyTo('email@example.com');
 mail.setSenderDisplayName('Email Example');
 mail.setSubject(contract.Name + ' has A New Note & Attachments: ' + contract.Contract_ID__c);
 mail.setUseSignature(false);
 mail.setPlainTextBody('A new Note & Attachments was posted to a contract name ' + contracts.Name
      + ', the contract id is ' + contracts.Contract_ID__c + '.');
 mail.setHtmlBody('Here is the link to the contract:<b> ' + contracts.ContractNumber +' </b>has been created<p>'+
     ' View case <a href=https://cs4.salesforce.com/' + contracts.Id + '>click here</a>');
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

 

Thanks in advance,