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
SFDC n12SFDC n12 

notes and attachment validation

 Hi,

I am having the notes and attachments section as a related list on a custom object,


on save i have a trigger which is firing a approval process on my customobject

my scenario is when no record is attached under notes and attachment it should throw an error befre save "please enter value for notes and attachment" preventing the approval process not to fire


if attachment is present then no error should be thrown and record can be saved


can a validation rule help on this case

let me know how to do it

below is my trigger for approval process

trigger SubmitForApproval on Special_Programs_ADR_Exception__c (after update, after insert) {
    try{
        for (Integer i = 0; i < Trigger.new.size(); i++) {
            if (Trigger.new[i].RoutenotifytoSalesSpecialist__c !=null && Trigger.new[i].DOSUser__c !=null && Trigger.new[i].RVP_Approver__c !=null) {

                // create the new approval request to submit
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setComments('Submitted for approval. Please approve.');
                req.setNextApproverIds(new List<Id>{trigger.new[i].ownerId});
                req.setObjectId(Trigger.new[i].Id);
                
                // submit the approval request for processing
                Approval.ProcessResult result = Approval.process(req);
                
                // display if the reqeust was successful
                System.debug('Submitted for approval successfully: '+result.isSuccess());
            }
        }
    }catch(System.DMLException e) { }
}




Thnks in advance

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Unfortunatly this is not possible. Other approch I would suggest is to have One custom chackbox field on you object which will be updated using attachement trigger whenever attachemnt is added to your custom object record.
Then depending on checkbox is checked or unchecked you can have validation rule to add error message before approval.


Thanks,
N.J
SFDC n12SFDC n12
 ok fine,

can you help me with the trigger  based on notes and attachments



Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Have a look at this sample code :

trigger SetTitleToAttachment on Attachment (after insert) {
	String Title;
	Id pId;

	for(Attachment att: Trigger.new){
		Title=att.Name;
		pId=att.ParentId;
	}
	
	List<Case> c=[select Id , Title__c from Case where Id=:pId];
	//assuming one record is fetched.
	c[0].Title__c=Title;

	update c[0];
}

Thanks,
N.J