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
sudha76sudha76 

Trigger to check the field TRUE when the attachment is provided on the parent object.

Hi there,

 

Here is the APEX Class and the Trigger on the parent object - MDF_Funding.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

/* This class will return the number of attachments in the Notes and Attachment section for the parent object Partner Funded Activity. */

public class checkAttachments
{
    public static boolean isInsert = false;
    public static integer numberOf(MDF_Funding__c[] funds)
    {
        Attachment [] att = [SELECT ID from Attachment Where ParentID IN: funds];
        return att.size();
    }
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


trigger QuoteAttached on MDF_Funding__c (before insert)
{

checkAttachments.numberOf(Trigger.new);

for(MDF_Funding__c funds : Trigger.new)
{
    if (checkAttachments.isInsert == false)
        {
        funds.addError('Please provide the Quote as an Attachment before saving this record');
        }
}
   

       

}

 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 

I want to add one more logic here, which says that when the attachment is provided the field called "Quote_Attached" should be set to TRUE. It should be checked.

 

How can I do this? Please help?

 

AshlekhAshlekh

Hi,
      I think you want to check that if id is present in att the record will save other wise it show error if this is your requirment than this code will help you

 

       trigger QuoteAttached on MDF_Funding__c (before insert)
      {

                //getAttachments 
               Map<id,Attachment> att = new Map<id,Attachment>([SELECT ID from Attachment Where ParentID IN: Trigger.new]);

              for(MDF_Funding__c funds : Trigger.new)
             {
                     if (!att.containsKey(funds.id))
                     {
                          funds.addError('Please provide the Quote as an Attachment before saving this record');
                     }
              }

     }

 

 Did this post answers your problem If so please mark it solved.....

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

If you are writing a before insert trigger on MDF_Funding__c there would be no way to check if there is an attachemnt to it. Before mdf_funding__c record is created there is no way to make sure you have an attachment associated to it because the parentId on attachment will refer to the record Id of the object that you have trigger on.

In your trigger you are trying to make sure that attachment is associated even before record creation itself...

 

The only way to make sure you have an attachment associated to it is to use a visualforce page and ovveride the standard page so that you can make sure that there is an attachment associated to the parent record.

 

The above triggers would stop creating records itself...

sudha76sudha76

Thanks for the reply. Here is the breakdown of the steps of my trigger that it should look like:-

 

a) MDF Request Record is created.

b) The record is also saved.

c) When and only when the user provides the Attachment to that saved record, then there is a field on the Parent obect called - Quote_Attached which should be set to TRUE.

 

So, the record can be saved without the attachment. That is ok.

But, when the attachment is provided the checkbox field should be set to TRUE.

 

so, for this , I dont think I need a Visualforce page. I think I can do this with the Trigger on after insert event.

 

Thoughts?

 

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

If that is the case... then creating a trigger on MDF_Funding__c would not help I believe.. I am not 100% sure if the trigger fires on the object MDF if an attachment is inserted... Instead you can create a Trigger on Attachment (Through Eclipse).. and then update that flag as soon as the attachment is inserted...

 

Try this code and see if it helps..

 

trigger updateMDF on attachment (after insert)
{
//collect all the parentId's into a set
set<Id> pIds = new set<Id>();

//list of all MDF's to be updated with the flag
List<MDF_Funding__c> toUpdate = new List<MDF_Funding__c>();

//Collect all parentIds
for(Attachment a : Trigger.new){
pIds.add(a.parentId);
}

//Collect all MDF records with the above list of parentId's and it will return only MDF Records
List<MDF_Funding__c> MDFList = [select id, Has_Attachment__c from MDF_Funding__c where id IN :pIds];

if(MDF<>null)
{
for(MDF_Funding__c m : MDFList)
m.Has_Attachment__c = true;
toUpdate.add(m);
}
if(toUpdate.size()>0){
Database.update(toUpdate,false);
}
}