+ Start a Discussion
Edward VazquezEdward Vazquez 

Apex Class Question

I will explain what the script below achieves. We have a custom "Properties" object. On a properties record we have buttons that users select to generate documents. For example, "generate document". Once they select this button the document is generated through conga and automatically a new record is created in a new custom object called Properties Documents with the attached document. The script below was created by a team before my time. I am trying to replicate this process for two new custom objects. Those custom objects being "Resales" and "Resales Documents". Same idea, when users are on a resales record and they select a button to generate a document I want the document to be generated and automatically a new resales documents record is created with the attachment. I'm not familiar with Apex classes so if you could please help it would be greatly appreciated. The api name for Resales is Resales__c and for Resales Documents it is Resales_Documents__c.



public with sharing class AttachmentTriggerHandler {

  //public void OnBeforeInsert(Attachment[] newObjects){
    
  //}
  
  public void OnAfterInsert(Attachment[] newObjects){
    //creates new properties documents and copies the attachment to it.
      list<Attachment> aList = new list<Attachment>();
      for(Attachment a : newObjects){
        if(a.ParentId.getSobjectType() == Schema.Properties__c.SObjectType){
          aList.add(a);
        }
      }
      if(aList.size() > 0){
        PropertiesAttachment pa = new PropertiesAttachment();
        pa.CreateNewPropertiesAttatchment(aList); 
      }
  }
  
/*  public void OnBeforeUpdate(Attachment[] oldObjects, Attachment[] updatedObjects, map<id,Attachment> MapNewMap, map<id,Attachment> MapOldMap){

  }
  
  public void OnAfterUpdate(Attachment[] oldObjects, Attachment[] updatedObjects, map<id,Attachment> MapNewMap, map<id,Attachment> MapOldMap){
  
  }
  
  public void OnBeforeDelete(Attachment[] ObjectsToDelete, map<id,Attachment> MapNewMap, map<id,Attachment> MapOldMap){
  
  }
  
  public void OnAfterDelete(Attachment[] deletedObjects, map<id,Attachment> MapNewMap, map<id,Attachment> MapOldMap){
  
  }
  
  public void OnUndelete(Attachment[] restoredObjects){
  
  }
*/
}
Atul GuptaAtul Gupta
Edward, I don't this is being handled by a trigger for Attachment object. The record child creationwill be done via Resales__c trigger only.
Plus, the document attachment is being done via Conga only.

See this (http://salesforce.stackexchange.com/questions/83675/how-to-merge-and-attach-pdf-to-record-using-conga-composer-with-out-user-interfe)
Shashikant SharmaShashikant Sharma
Hi Edward,

First I would like to explain what is this class doing.
As the name AttachmentTriggerHandler suggest it is handler class for the Trigger on Attachment

It has all the code commented except this method. I have put some additional comment on this method to make you understand the flow

public void OnAfterInsert(Attachment[] newObjects){
      //creates new properties documents and copies the attachment to it.
      list<Attachment> aList = new list<Attachment>();
     // loop over the newly created attachment 
     for(Attachment a : newObjects){
       // when the newly created attachment is associated with Property record
       if(a.ParentId.getSobjectType() == Schema.Properties__c.SObjectType){
          aList.add(a);
        }
      }
      if(aList.size() > 0){
       // calls method from PropertiesAttachment class
        PropertiesAttachment pa = new PropertiesAttachment();
        // passed the list of attachments as parameter
        pa.CreateNewPropertiesAttatchment(aList); 
      }
  }

Now what I understand that conga creates a Document in attachment and then this trigger adds it to property attachment

You should add

1. Create  a similar class to PropertiesAttachment for ResaleAttachment and create method CreateNewResaleAttatchment
2.  Update this method
 
public void OnAfterInsert(Attachment[] newObjects){
      //creates new properties documents and copies the attachment to it.
      list<Attachment> aList = new list<Attachment>();
      
      list<Attachment> aListForResale = new list<Attachment>();
      
     // loop over the newly created attachment 
     for(Attachment a : newObjects){
       // when the newly created attachment is associated with Property record
       if(a.ParentId.getSobjectType() == Schema.Properties__c.SObjectType){
          aList.add(a);
        }
        else if (a.ParentId.getSobjectType() == Schema.Resales__c.SObjectType){
          aListForResale .add(a);
        }
      }
      if(aList.size() > 0){
       // calls method from PropertiesAttachment class
        PropertiesAttachment pa = new PropertiesAttachment();
        // passed the list of attachments as parameter
        pa.CreateNewPropertiesAttatchment(aList); 
      }

      if(aListForResale.size() > 0){
       // calls method from ResaleAttachment class
        ResaleAttachment pa = new ResaleAttachment();
        // passed the list of attachments as parameter
        pa.CreateNewResaleAttatchment(aListForResale ); 
      }
  }




Let me know if you face issues.

Thanks
Shashikant Sharma