+ Start a Discussion
Shannon Andreas 1Shannon Andreas 1 

Moving an attachment via trigger

I have created a trigger that will create a contract when a DocuSign is completed. I also need the trigger (or another trigger which seems wrong to me) to move the attachment.

Process is when sending to DocuSign, a DocuSign Status record is created. When the Envelope Status changes to "Completed", the contract is created. IN addition to creating the contract, I need the trigger to "copy" the completed document attachment from the DocuSign Status record to the new contract.

Here is my trigger:

trigger CreateContractDocSignComp on dsfs__DocuSign_Status__c (after update)
{
    List<Contract> ctr = new List<Contract>();
    
      for(dsfs__DocuSign_Status__c dsfs : Trigger.new)
      {
        if(dsfs.dsfs__Envelope_Status__c == 'Completed')
        {
             Contract c = new Contract(Name = dsfs.Name,
             Status = 'Draft',
             Total_Contract_Value__c =dsfs.Total_Contract_Value__c,
             StartDate = dsfs.Contract_Start_Date__c,
             Payment_Status__c = 'Ready to be Invoiced',
             AccountId = dsfs.dsfs__Company__c,
             Opportunity_Name__c = dsfs.dsfs__Opportunity__c);
             ctr.add(c);
         }
      }
      if(ctr.size() > 0)
      {
            System.debug('-ctr------->'+ctr.size());
            insert ctr;
      }     
}

Thanks!

Shannon
JeffreyStevensJeffreyStevens
The documents are kept in the object - Attachment.  So, you'll want another trigger on the object Attachment.  The ParentId is the Id to the record that the Attachment (ie document) is attached to.  In this case you'll wan to just create a new Attachment record - and set the ParentId field to the ID of the New Contract. 
Shannon Andreas 1Shannon Andreas 1
Thanks Jeffrey! So there is no way to include this in the current trigger?
JeffreyStevensJeffreyStevens
Well - I'm not up-todate on my order of execution, but I think that if you're doing an after update on dsfs__DocuSign_Status__c  - then  you should be able to SOQL the Attachment object for the ParentId = the dsfs__DocuSign_Status__c .id.  That should get you the Attachment record. Then  you could duplicate it, or re-assign the ParentId.  Make sense?
Shannon Andreas 1Shannon Andreas 1
Yup!! Thanks again. I will mark best answer when I get the trigger working properly.

Might need help with a test class ;)

Shannon