You need to sign in to do that
Don't have an account?
Douglas 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;
}
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;
}
Also, False doesn't require a Quote sign, you can just write false, as long as your field is a checkbox.
Here is the small modification with code, Please look for the below updated code that solves you issue.
Hope this solves your problem.
Thanks,
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);
}
}
}