+ Start a Discussion

Help with Trigger


I need to write a trigger for the following requirement:

My Opportunity is the master of Quotes. An Opp can have many Quotes.
Quotes is parent to the Docusign Status. A quote can have many docusign status. There is a field on Docusign status named "Envelope Status".
There is a field on Opp say "X", the value of this field will be either Completed or Incomplete. This value depends on whether all the Quotes attached to the Opp has their Envelope Status as "Completed" or "Incomplete".
I want to write the trigger such that if all the Quotes on an Opp has Envelope Status as "Completed", the field value of "X" should be completed else even if one of the Quotes is INcomplete then X shold be eqal to Incomplete.
Hi Wik,

The following post contains two triggers.  The first trigger fires whenever a Docusign_Status__c record is inserted, updated, deleted or undeleted.  It figures out which Opportunity records are affected and updates those Opportunities.

The second trigger fires whenever an Opportunity is updated.  If gets all of the related Docusign_Status__c records and figures out if the Opportunity is 'Completed' or 'Imcomplete'.

You may have to change the name of some of the fields.

Let me know if you have any questions.  I apologize for any typos.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy.blogspot.com
Twitter: @GlynAtClosedWon
trigger DocusignStatusAfterTrigger ( after insert, after update, after delete, after undelete )
    Set<Id> set_QuoteIDs = Set<Id>();
    for ( Docusign_Status__c status : trigger.isDelete ? trigger.old : trigger.new )
        if  (   trigger.isUpdate
            &&  status.Envelope_Status__c == trigger.oldMap.get( status.Id ).Envelope_Status__c
            ) continue;
        set_QuoteIDs.add( status.Quote__c );

    Set<Id> set_OpportunityIDs = Set<Id>();
    for ( Quote theQuote :
        [   SELECT  Id, OpportunityId
            FROM    Opportunity
            WHERE   Id IN :set_QuoteIDs
        set_OpportunityIDs.add( theQuote.OpportunityId );
    update [SELECT Id FROM Opportunity WHERE Id IN :set_OpportunityIDs];

trigger OpportunityBeforeTrigger on Opportunity ( before update )
    Map<Id,Boolean> map_OppID_Completed = Map<Id,Boolean>();
    for ( Docusign_Status__c status :
        [   SELECT  Id, Envelope_Status__c, Quote__r.OpportunityId
            FROM    Docusign_Status__c
            WHERE   Quote__r.OpportunityId IN :trigger.new
        if ( !map_OppID_Completed.contains( status.Quote__r.OpportunityId ) )
            map_OppID_Completed.put( status.Quote__r.OpportunityId, true );
        (   theOpp.Id,
            (   map_OppID_Completed.get( status.Quote__r.OpportunityId )
            &&  status.Envelope_Status__c == 'Completed'

    for ( Opportunity theOpp : trigger.new )
        theOpp.X__c =
            map_OppID_Completed.get( theOpp.Id )    ?   'Completed'
                                                    :   'Incomplete';