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
Will Sun 14Will Sun 14 

Note and Attachment as mandatory field

Hi All,

I need some helps to make Note and Attachment as mandatory before submit for approval.
currently I have created a checkbox field name: Attachment_c but don't know how to write a Apex code for Trigger. 
what I would like to do is:
if there is a file in attachment, then checkbox for attachment will be ticked, then you could click "submit for approval"
otherwise, you will receive a error msg: " Please attach claim form for approval"

current coding i have :

trigger TiggerName on attachment (after insert)
{
    Set<Id> attId = new Set<Id>();
    List<sObject> objList = new List<sObject>();
    for(Attachment at :Trigger.New){
        attId.add(at.ParentId);
    }
    if(attId.size() > 0){
        List<Retailer_Return_Request__c> recList = [select Id, Attachments__c from Retailer_Return_Request__c where id IN :attId];
        
        if(recList.size() > 0){
            for(Retailer_Return_Request__c a:recList){
                Retailer_Return_Request__c obj = new Retailer_Return_Request__c();
                obj.Attachments__c = true;
                objList.add(obj);
            }
        }
        
        if(objList.size()>0)
        {
            update objList;
        }                                                                   
    }
}
 with error msg:
Compile Error: Incorrect SObject type: Attachment should be Retailer_Return_Request__c at line -1 column -1

Thank you 

Will
 
Best Answer chosen by Will Sun 14
Ajay K DubediAjay K Dubedi
Hi Will,

Here is your apex code for the trigger. Hope this helps you.

And never create a custom field with standard object name given by salesforce it will led to confusion.

trigger TiggerName on attachment (after insert)
{
    Set<Id> attId = new Set<Id>();
    List<sObject> objList = new List<sObject>();
    for(Attachment at :Trigger.New){
        if(at.ParentId.getSObjectType().getDescribe().getName()=='Event__c')
        {
            if(at.ParentId!=NULL)
            {
                attId.add(at.ParentId);
            }
        }
    }
    if(attId.size() > 0){
        List<Event__c> recList = [select Id from Event__c where id IN :attId];
        
        if(recList.size() > 0){
            for(Event__c a:recList){
                Event__c obj = new Event__c();
                obj.Id=a.Id;
                obj.IsAttached__c = true;
                objList.add(obj);
            }
        }
        
        if(objList.size()>0)
        {
            update objList;
        }                                                                   
    }
}


Hope this will help you.

Thank you 
Ajay Dubedi