+ Start a Discussion
Douglas TrasterDouglas Traster 

Help with Trigger on Opp/Contact

I am having a problem with a trigger.  I am trying to update the Active Opportunity(checkbox) account in Contact when an Opportunity is active, then the field should be True. When the StageName in Opportunity is either Closed Won or Closed Lost then False.

trigger updateActiveOpportunity on opportunity (after insert, after update){
    list<Id> ConIds = new list<Id>();
    list<Contact> Contacts = new list<contact>();
    for(opportunity o:trigger.new){
        conIds.add(o.contactId);
    }
    for(contact c:[select Id, Active_Opportunity__c from contact where Id IN :contIds]){
        for(opportunity opp:trigger.new){
            if(opp.stage=='closed won','closed lost'){
                c.Active_Opportunity__c='False';
                contacts.add(c);
            }
        }
    }
    update Contacts;
}
Hargobind_SinghHargobind_Singh
Hi Douglas, I don't see the code to turn the flag to "True", is that a separate trigger ? You can it in your loop itself as a if-else ? 

Also, False doesn't require a Quote sign, you can just write false, as long as your field is a checkbox. 
 
SaranSaran
HI Douglas,

Here is the small modification with code, Please look for the below updated code that solves you issue.
 
trigger updateActiveOpportunity on opportunity (after insert, after update){
    set<Id> ConIds = new set<Id>();
set<id> allConId = new set<id>();
    list<Contact> Contacts = new list<contact>();

    for(opportunity o:trigger.new){
		allConId.add(o.ContactId)
		if(opp.isClosed)
       		conIds.add(o.contactId);
    }

    for(contact c:[select Id, Active_Opportunity__c from contact where Id IN :allConId]){
    {
        if(conIds.contains(c.id))
		{
            c.Active_Opportunity__c = false;
		}
		else
		{
			c.Active_Opportunity__c= true;
		}
            contacts.add(c);
    }
    update Contacts;
}

Hope this solves your problem.

Thanks,
Douglas TrasterDouglas Traster


I broke into 2 triggers, to get it to save .
They are supposed to look at the Opportunity Stage field and then update a custom field on Contacts -- Active_Opportunity__C (a checkbox).  

I am getting 0% code coverage on both and when I enter contact and new opportunity, the contact field is not populating?   What am I doing wrong?

Active_Opportunity__c (Contacts)
False 

Stage (Opportunities)
Closed Won
Closed Lost

Active_Opportunity__c (Contacts)
True

Stage (Opportunities)
Prospecting
Qualified Interest
Needs Analysis
Value Proposition
Proposal/Price Quote
Negotiation/Review
Verbal Commit
Closed Pending

Can I put these 2 into 1 and then would I just mock up an opporunity for the Unit Test?

trigger activeOpportunity on Opportunity (after insert, after update) {
    
    List<contact> conList = new List <contact>();
    
    for (opportunity optyObj: Trigger.new){
        if (optyObj.StageName == 'Closed Won' 
        && optyObj.StageName == 'Closed Lost'
        && optyObj.Contact__c <> null){
            
            conList = [select Id, Active_Opportunity__c from contact where Id = :optyObj.Contact__c ];
            for(contact conObj: conList){
                conObj.Active_Opportunity__c = False;
            }
            update(conList);
        }   
    }
}

trigger activeOpportunity2 on Opportunity (after insert, after update) {
    
    List<contact> conList = new List <contact>();
    
    for (opportunity optyObj: Trigger.new){
        if (optyObj.StageName == 'Prospecting' 
        && optyObj.StageName == 'Qualified Interest'
        && optyObj.StageName == 'Needs Analysis'
        && optyObj.StageName == 'Value Proposition'
        && optyObj.StageName == 'Proposal/Price Quote'
        && optyObj.StageName == 'Negotiation/Review'
        && optyObj.StageName == 'Verbal Commit'
        && optyObj.StageName == 'Closed Pending'
        && optyObj.Contact__c <> null){
            
            conList = [select Id, Active_Opportunity__c from contact where Id = :optyObj.Contact__c ];
            for(contact conObj: conList){
                conObj.Active_Opportunity__c = True;
            }
            update(conList);
        }   
    }
}