+ Start a Discussion
Shannon Andreas 1Shannon Andreas 1 

Trigger help - not firing when I need it to!

We have a process where the rep creates a physical contract (not a Salesforce contract record) using DrawLoop. There is a button on the opportunity or quote the rep can push to initiate the process. When the document is created, there is an option to send using DocuSign. When the rep clicks the link, it sends to DocuSign, thereby creating a DocuSign Status record (DocuSign_Status custom object). 

I wrote a trigger that would create a contract when the DocuSign Status field "Envelope Status = Completed". It works...but only when I create the DocuSign Status record manually and change the trigger from after insert to after update. If it is created through the DocuSign workflow that is set up, it does not create the contract. Also, changing to after update messes up my test class as well, no coverage.

What am I missing?

Here is the original trigger that passes testing:

trigger CreateContractDocSignComp on dsfs__DocuSign_Status__c (after insert)
{
    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 for your help!

Shannon



 
Best Answer chosen by Shannon Andreas 1
Vishal_GuptaVishal_Gupta
Hi Shanon,

Please change your test class with below code :


@isTest
private class TestCreateContractDocSignCompTrigger 
{
    static testMethod void validateCreateContractDocuSignComp() 
    {   
       Account a = new Account(
       Name = 'Test Account');
    insert a;

       Opportunity o = new Opportunity(
       Name = 'Test Opp',
       Ready_for_Contract__c = true,
       CloseDate = System.Today(),
       AccountId = a.Id,
       StageName = 'Signed / Closed Sale',
       Amount = decimal.valueof('6995'));
        
    Test.StartTest();
    
    insert o;
    
       dsfs__DocuSign_Status__c  dsfs = new dsfs__DocuSign_Status__c();
       dsfs.dsfs__Company__c = o.AccountId;
       dsfs.dsfs__Opportunity__c = o.Id;
       dsfs.dsfs__Envelope_Status__c = 'Draft';
       dsfs.dsfs__DocuSign_Envelope_ID__c = '1001A123-1234-5678-1D84-F8D44652A382';
       dsfs.dsfs__Subject__c = 'Document for eSignature';
       insert dsfs;
   
      dsfs.dsfs__Envelope_Status__c = 'Completed';
      update dsfs;

       
                   
            List<Contract> lstContr = [select id from contract where Opportunity_Name__c =:o.id];
                 //System.assertNotEquals(1stContr,null);
                 
        Test.StopTest();
       
      }
}

Please let me know if  I can help you more.

Thanks,
Vishal
 

All Answers

Amit K AAmit K A
Please check whether the trigger is active or not.
Shannon Andreas 1Shannon Andreas 1
it is amit...like i said, it works when I change to after update and when I create the DocuSign record manually in SF.
Vishal_GuptaVishal_Gupta
Hi Shannon,

In dsfs__DocuSign_Status__c object, field dsfs__Envelope_Status__c always mark completed in a update call beacuse as a process docusign first send an envelope to the signer and once signer reviewed contarct and signed it, dsfs__DocuSign_Status__c change the envelop status equal to complete, so I suggest to keep your code in  After update.

Please let me know if I am missing something.

Thanks,
Vishal
Shannon Andreas 1Shannon Andreas 1
Thanks Vishal!

Please, I need help with my test class now. I am getting "0" coverage even though it is passing Run Test. Does it have something to do with how we are inserting the dsfs record? Maybe I have to start the test after we insert dsfs? Please advise and thank you very much!

@isTest
private class TestCreateContractDocSignCompTrigger 
{
    static testMethod void validateCreateContractDocuSignComp() 
    {   
       Account a = new Account(
       Name = 'Test Account');
    insert a;

       Opportunity o = new Opportunity(
       Name = 'Test Opp',
       Ready_for_Contract__c = true,
       CloseDate = System.Today(),
       AccountId = a.Id,
       StageName = 'Signed / Closed Sale',
       Amount = decimal.valueof('6995'));
        
    Test.StartTest();
    
    insert o;
    
       dsfs__DocuSign_Status__c  dsfs = new dsfs__DocuSign_Status__c();
       dsfs.dsfs__Company__c = o.AccountId;
       dsfs.dsfs__Opportunity__c = o.Id;
       dsfs.dsfs__Envelope_Status__c = 'Completed';
       dsfs.dsfs__DocuSign_Envelope_ID__c = '1001A123-1234-5678-1D84-F8D44652A382';
       dsfs.dsfs__Subject__c = 'Document for eSignature';
    insert dsfs;
       
                   
            List<Contract> lstContr = [select id from contract where Opportunity_Name__c =:o.id];
                 //System.assertNotEquals(1stContr,null);
                 
        Test.StopTest();
       
      }
}
 
Vishal_GuptaVishal_Gupta
Hi Shanon,

Please change your test class with below code :


@isTest
private class TestCreateContractDocSignCompTrigger 
{
    static testMethod void validateCreateContractDocuSignComp() 
    {   
       Account a = new Account(
       Name = 'Test Account');
    insert a;

       Opportunity o = new Opportunity(
       Name = 'Test Opp',
       Ready_for_Contract__c = true,
       CloseDate = System.Today(),
       AccountId = a.Id,
       StageName = 'Signed / Closed Sale',
       Amount = decimal.valueof('6995'));
        
    Test.StartTest();
    
    insert o;
    
       dsfs__DocuSign_Status__c  dsfs = new dsfs__DocuSign_Status__c();
       dsfs.dsfs__Company__c = o.AccountId;
       dsfs.dsfs__Opportunity__c = o.Id;
       dsfs.dsfs__Envelope_Status__c = 'Draft';
       dsfs.dsfs__DocuSign_Envelope_ID__c = '1001A123-1234-5678-1D84-F8D44652A382';
       dsfs.dsfs__Subject__c = 'Document for eSignature';
       insert dsfs;
   
      dsfs.dsfs__Envelope_Status__c = 'Completed';
      update dsfs;

       
                   
            List<Contract> lstContr = [select id from contract where Opportunity_Name__c =:o.id];
                 //System.assertNotEquals(1stContr,null);
                 
        Test.StopTest();
       
      }
}

Please let me know if  I can help you more.

Thanks,
Vishal
 
This was selected as the best answer
Shannon Andreas 1Shannon Andreas 1
YOU ARE AWESOME!! Thanks so much Vishal. I wish I knew how you guys knew this stuff! I am thinking that the more I do it, the more it will become second nature! Now I know how to write a test class on after insert and after update! 
Vishal_GuptaVishal_Gupta
You are welcome Shannon, you can contact me anytime for any help, my contact details are on my prfile.

Thanks,
Vishal
Shannon Andreas 1Shannon Andreas 1
I appreciate that Vishal! Have a great day.