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
Shannon Andreas 1Shannon Andreas 1 

Attachment trigger

Hello!

Can someone help me with a trigger? I have it together, but there is an issue with the attachment part. It is not attaching. Here is the trigger:

trigger CreateContractDocSignComp on dsfs__DocuSign_Status__c (after update)
{
    map<id,id> testmap = new map<id,id>();
    
     list <attachment> attach = new list<attachment>();
    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,
             ContractTerm = Integer.valueOf(dsfs.Contract_Term_months2__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,dsfsid__c = dsfs.id);
             ctr.add(c);
        }
      }
      if(ctr.size() > 0)
      {
             System.debug('-ctr------->'+ctr.size());
             insert ctr;
      }
      
        for(contract con : ctr){
         testmap.put(con.dsfsid__c, con.id);
      }
      system.debug('Testmap size is '+testmap.size());
      
      system.debug('query data'+[select id,parentid,body,name from attachment where parentid in :
      testmap.keyset()]);
        for(attachment att : [select id,parentid,body,name from attachment where parentid in : 
      testmap.keyset()]){
      
      if(testmap.containskey(att.parentid)){
      attachment a = new attachment(name=att.name,body=att.body,parentid=testmap.get(att.parentid));
      attach.add(a);
      }
      }
      system.debug('attach is '+attach);
      insert attach;
      
      }

Thanks so much!
Best Answer chosen by Shannon Andreas 1
Neetu_BansalNeetu_Bansal
Hi Shannon,

Try this code:
trigger CreateContractDocSignComp on dsfs__DocuSign_Status__c( after update )
{
    Map<Id, Contract> docuSignToContractMap = new Map<Id, Contract>();
    List<Attachment> attach = new List<Attachment>();
    
	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,
										ContractTerm = Integer.valueOf(dsfs.Contract_Term_months2__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, dsfsid__c = dsfs.id );
            docuSignToContractMap.put( dsfs.id, c );
        }
    }
    
	if( docuSignToContractMap.size() > 0 )
		insert docuSignToContractMap.values();
    
	for( Attachment att : [ Select Id, Parentid, Body, Name from attachment where parentid IN: trigger.new ])
	{
		if( docuSignToContractMap.containskey( att.Parentid ))
		{
			Attachment a = new Attachment( Name=att.name, body=att.body, Parentid = docuSignToContractMap.get( att.Parentid ).Id );
			attach.add( a );
		}
    }
    
	if( attach.size() > 0 )
		insert attach;
}
Let me know, if you need any other help.

Thanks,
Neetu

All Answers

john4sfdcjohn4sfdc
Can you post the debug log?
Shannon Andreas 1Shannon Andreas 1

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 13:37:01.122 (122934506)|ENTERING_MANAGED_PKG|dsfs 13:37:01.129 (129529494)|SOQL_EXECUTE_BEGIN|[14]|Aggregations:0|SELECT Id, OrganizationType FROM Organization LIMIT 1 13:37:01.133 (133370048)|SOQL_EXECUTE_END|[14]|Rows:1 13:37:01.225 (225763369)|SOQL_EXECUTE_BEGIN|[32]|Aggregations:0|SELECT id, dsfs__ChatterUpdatesEnabled__c, dsfs__DocuSign_Envelope_ID__c, dsfs__Source_Object__c, dsfs__ChatterEnvSent__c, dsfs__ChatterEnvCompleted__c, dsfs__ChatterEnvDeclined__c, dsfs__ChatterEnvDelivered__c, dsfs__ChatterEnvVoided__c, dsfs__ChatterEnvSentText__c, dsfs__ChatterEnvCompletedText__c, dsfs__ChatterEnvDeclinedText__c, dsfs__ChatterEnvDeliveredText__c, dsfs__ChatterEnvVoidedText__c FROM DocuSign_Envelope__c WHERE DocuSign_Envelope_ID__c IN :tmpVar1 13:37:01.231 (231509917)|SOQL_EXECUTE_END|[32]|Rows:0 13:37:01.234 (234584140)|SOQL_EXECUTE_BEGIN|[242]|Aggregations:0|SELECT dsfs__DocuSign_EnvelopeID__c, dsfs__DocuSign_Signer_Type__c, dsfs__DSER_ContactID__c, dsfs__DocuSign_Signature_Name__c, dsfs__DSER_LeadID__c, dsfs__DSER_UserID__c, dsfs__DSER_CustomId__c, dsfs__DSER_CustomFeatures__c, dsfs__DSER_CustomFeaturesEx__c, dsfs__DSER_CustomName__c, d.Id, dsfs__Routing_Order__c, dsfs__Salesforce_Recipient_Type__c, dsfs__DocuSign_Recipient_Role__c, dsfs__RoleName__c FROM DocuSign_Envelope_Recipient__c d WHERE DocuSign_EnvelopeID__c IN :tmpVar1 ORDER BY d.Routing_Order__c ASC NULLS FIRST 13:37:01.239 (239703018)|SOQL_EXECUTE_END|[242]|Rows:0 13:37:01.241 (241358523)|SOQL_EXECUTE_BEGIN|[189]|Aggregations:0|SELECT dsfs__Attachment_NameEx__c, dsfs__Document_Name__c, dsfs__DocuSign_EnvelopeID__c FROM DocuSign_Envelope_Document__c WHERE DocuSign_EnvelopeID__c IN :tmpVar1 13:37:01.244 (244969582)|SOQL_EXECUTE_END|[189]|Rows:0 13:37:01.252 (252486500)|EXECUTION_STARTED 13:37:01.252 (252494154)|CODE_UNIT_STARTED|[EXTERNAL]|01qS0000000DJdo|CreateContractDocSignComp on DocuSign_Status trigger event AfterUpdate for [a0OS0000004Jl8i] 13:37:01.252 (252803511)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>() 13:37:01.252 (252834083)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>() 13:37:01.252 (252880535)|SYSTEM_CONSTRUCTOR_ENTRY|[6]|<init>() 13:37:01.252 (252898305)|SYSTEM_CONSTRUCTOR_EXIT|[6]|<init>() 13:37:01.252 (252929466)|SYSTEM_METHOD_ENTRY|[8]|List<dsfs__DocuSign_Status__c>.iterator() 13:37:01.252 (252950289)|SYSTEM_METHOD_EXIT|[8]|List<dsfs__DocuSign_Status__c>.iterator() 13:37:01.252 (252962214)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext() 13:37:01.252 (252977456)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext() 13:37:01.253 (253016101)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext() 13:37:01.253 (253030332)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext() 13:37:01.253 (253051208)|SYSTEM_METHOD_ENTRY|[23]|List<Contract>.size() 13:37:01.253 (253069796)|SYSTEM_METHOD_EXIT|[23]|List<Contract>.size() 13:37:01.253 (253086054)|SYSTEM_METHOD_ENTRY|[29]|List<Contract>.iterator() 13:37:01.253 (253200763)|SYSTEM_METHOD_EXIT|[29]|List<Contract>.iterator() 13:37:01.253 (253225086)|SYSTEM_METHOD_ENTRY|[29]|system.ListIterator.hasNext() 13:37:01.253 (253238979)|SYSTEM_METHOD_EXIT|[29]|system.ListIterator.hasNext() 13:37:01.253 (253262081)|SYSTEM_METHOD_ENTRY|[32]|Map<Id,Id>.size() 13:37:01.253 (253284280)|SYSTEM_METHOD_EXIT|[32]|Map<Id,Id>.size() 13:37:01.253 (253321831)|SYSTEM_METHOD_ENTRY|[32]|String.valueOf(Object) 13:37:01.253 (253340818)|SYSTEM_METHOD_EXIT|[32]|String.valueOf(Object) 13:37:01.253 (253358894)|SYSTEM_METHOD_ENTRY|[32]|System.debug(ANY) 13:37:01.253 (253368222)|USER_DEBUG|[32]|DEBUG|Testmap size is 0 13:37:01.253 (253373813)|SYSTEM_METHOD_EXIT|[32]|System.debug(ANY) 13:37:01.253 (253403330)|SYSTEM_METHOD_ENTRY|[35]|Map<Id,Id>.keySet() 13:37:01.253 (253468691)|SYSTEM_METHOD_EXIT|[35]|Map<Id,Id>.keySet() 13:37:01.254 (254181236)|SOQL_EXECUTE_BEGIN|[34]|Aggregations:0|SELECT id, parentid, body, name FROM attachment WHERE parentid = :tmpVar1 13:37:01.256 (256556991)|SOQL_EXECUTE_END|[34]|Rows:0 13:37:01.256 (256628758)|SYSTEM_METHOD_ENTRY|[34]|String.valueOf(Object) 13:37:01.256 (256665918)|SYSTEM_METHOD_EXIT|[34]|String.valueOf(Object) 13:37:01.256 (256682263)|SYSTEM_METHOD_ENTRY|[34]|System.debug(ANY) 13:37:01.256 (256688942)|USER_DEBUG|[34]|DEBUG|query data() 13:37:01.256 (256694029)|SYSTEM_METHOD_EXIT|[34]|System.debug(ANY) 13:37:01.256 (256721147)|SYSTEM_METHOD_ENTRY|[37]|Map<Id,Id>.keySet() 13:37:01.256 (256757035)|SYSTEM_METHOD_EXIT|[37]|Map<Id,Id>.keySet() 13:37:01.257 (257008683)|SOQL_EXECUTE_BEGIN|[36]|Aggregations:0|SELECT id, parentid, body, name FROM attachment 13:37:01.258 (258043551)|SOQL_EXECUTE_END|[36]|Rows:0 13:37:01.258 (258108590)|SYSTEM_METHOD_ENTRY|[36]|Database.QueryLocator.iterator() 13:37:01.258 (258243019)|SYSTEM_METHOD_ENTRY|[7]|QueryLocatorIterator.QueryLocatorIterator() 13:37:01.258 (258256258)|SYSTEM_METHOD_EXIT|[7]|QueryLocatorIterator 13:37:01.258 (258308556)|SYSTEM_METHOD_EXIT|[36]|Database.QueryLocator.iterator() 13:37:01.258 (258326944)|SYSTEM_METHOD_ENTRY|[36]|Database.QueryLocatorIterator.hasNext() 13:37:01.258 (258341957)|SYSTEM_METHOD_EXIT|[36]|Database.QueryLocatorIterator.hasNext() 13:37:01.258 (258387454)|SYSTEM_METHOD_ENTRY|[44]|String.valueOf(Object) 13:37:01.258 (258420282)|SYSTEM_METHOD_EXIT|[44]|String.valueOf(Object) 13:37:01.258 (258435347)|SYSTEM_METHOD_ENTRY|[44]|System.debug(ANY) 13:37:01.258 (258441427)|USER_DEBUG|[44]|DEBUG|attach is () 13:37:01.258 (258446493)|SYSTEM_METHOD_EXIT|[44]|System.debug(ANY) 13:37:01.258 (258553226)|CUMULATIVE_LIMIT_USAGE 13:37:01.258 (258553226)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 2 out of 100 Number of query rows: 0 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 0 out of 10000 Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50 Number of Mobile Apex push calls: 0 out of 10 13:37:01.258 (258553226)|LIMIT_USAGE_FOR_NS|dsfs| Number of SOQL queries: 4 out of 100 Number of query rows: 1 out of 50000 Number of SOSL queries: 0 out of 20 Number of DML statements: 0 out of 150 Number of DML rows: 0 out of 10000 Maximum CPU time: 0 out of 10000 Maximum heap size: 0 out of 6000000 Number of callouts: 0 out of 100 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 0 out of 50 Number of Mobile Apex push calls: 0 out of 10 13:37:01.258 (258553226)|CUMULATIVE_LIMIT_USAGE_END 13:37:01.258 (258630988)|CODE_UNIT_FINISHED|CreateContractDocSignComp on DocuSign_Status trigger event AfterUpdate for [a0OS0000004Jl8i] 13:37:01.259 (259949426)|EXECUTION_FINISHED

 
Chandra Sekhar CH N VChandra Sekhar CH N V
I think its the below code which needs to be corrected:
 
if(testmap.containskey(att.parentid)){
      attachment a = new attachment(name=att.name,body=att.body,parentid=testmap.get(att.parentid));
      attach.add(a);
      }

try instead below;
if(testmap.containskey(att.dsfsid__c)){
      attachment a = new attachment(name=att.name,body=att.body,parentid=testmap.get(att.dsfsid__c));
      attach.add(a);
      }


 
Neetu_BansalNeetu_Bansal
Hi Shannon,

Try this code:
trigger CreateContractDocSignComp on dsfs__DocuSign_Status__c( after update )
{
    Map<Id, Contract> docuSignToContractMap = new Map<Id, Contract>();
    List<Attachment> attach = new List<Attachment>();
    
	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,
										ContractTerm = Integer.valueOf(dsfs.Contract_Term_months2__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, dsfsid__c = dsfs.id );
            docuSignToContractMap.put( dsfs.id, c );
        }
    }
    
	if( docuSignToContractMap.size() > 0 )
		insert docuSignToContractMap.values();
    
	for( Attachment att : [ Select Id, Parentid, Body, Name from attachment where parentid IN: trigger.new ])
	{
		if( docuSignToContractMap.containskey( att.Parentid ))
		{
			Attachment a = new Attachment( Name=att.name, body=att.body, Parentid = docuSignToContractMap.get( att.Parentid ).Id );
			attach.add( a );
		}
    }
    
	if( attach.size() > 0 )
		insert attach;
}
Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer
Shannon Andreas 1Shannon Andreas 1
Neetu,

I tried your code...no luck. 

Two FYIs...

1.) The document is attached using the DocuSign Connect for Salesforce integration.
2.) The dsfsid__c that is attached to the contract when it is created is 18 characters, but the DocuSign record ID is 15 characters. 

Do you think it is timing of the trigger? Do you think a separate trigger would work better? Just so you know, the connect feature also attaches the document to the Opportunity, Quote, and Account records.Maybe it makes sense to write the trigger against these objects?

Please let me know your thoughts.

Thanks,

Shannon
Neetu_BansalNeetu_Bansal
Hi Shannon,

I figure out the reason, actually we have trigger on after update, but attachment will be created after updating the dsfsid__c.

Yes, you need to write trigger on Attachment, means some checks and according to that create new Attachment. If you need any other help, you can contact me either on my gmail id: neetu.bansal.5@gmail.com or Skype id: neetu.bansal.5

Thanks,
Neetu
Shannon Andreas 1Shannon Andreas 1
I am definitely going to need your help with the attachment object trigger. I will send an email. Thanks.
Varun29Varun29
Need to update Case child object(Work) fields  based on Case attachments inserted:- wrote a below code:-

trigger BIIB_GenerateSOB_Autoclosure on Attachment (after insert) {      String wi_Item='Generate SOB'; String sr_type='Benefit Investigation'; Set<Id> attachIds = new Set<Id>();  List<Attachment> sr_attachment = new List<Attachment>(); List<BIIB_Work__c> updateSOB_WI = new List<BIIB_Work__c>();     for(Attachment atc: Trigger.new){         if(atc.ParentId.getSobjectType() == Case.SobjectType)             attachIds.add(atc.ParentId);     }      List<BIIB_Work__c> sobWI = [select id,Name,BIIB_Case__r.Id,BIIB_Case__r.BIIB_SR_Type__c from BIIB_Work__c where BIIB_Case__r.Id IN :attachIds and Name=:wi_Item and BIIB_Case__r.BIIB_SR_Type__c =:sr_type limit 49999];     if(sobWI.size()>0)     {         sr_attachment = [select id from Attachment where ParentId=: sobWI[0].BIIB_Case__r.Id];     }     if(!sr_attachment.isEmpty()){         for (BIIB_Work__c work: sobWI){              work.BIIB_Result__c = 'BI Complete' ;              work.BIIB_Status__c = 'Closed' ;              updateSOB_WI.add(work);         }       }     if(!updateSOB_WI.isEmpty())         Database.update(updateSOB_WI, false);        }