You need to sign in to do that
Don't have an account?
Douglas Traster
New Apex Trigger
I am fairly new to Apex.
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);
}
}
}
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);
}
}
}
trigger activeOpportunity on Opportunity (after insert, after update) {
List<contact> conUpdList = new List <contact>();
List<Id> oppConIDList = new List<Id>();
List<opportunity> oppConList = new List<opportunity>();
List<Contact> conList = new List<Contact>();
Map<ID, Contact> conMap = new Map<ID, Contact>();
for (opportunity opp: Trigger.new){
if(opp.Contact__c!= null){
oppConList.add(opp);
oppConIDList.add(opp.Contact__c);
}
}
if(!oppConIDList.isEmpty()){
conList = [select Id, Active_Opportunity__c from contact where Id = :oppConIDList];
}
if(!conList.isEmpty()){
for(Contact con : conList){
conMap.put(con.id,con);
}
}
if(!oppConList.isEmpty()){
for(Opportunity optyObj : oppConList){
Contact c = conMap.get(optyObj.Contact__c);
if (optyObj.StageName == 'Closed Won' || optyObj.StageName == 'Closed Lost'){
c.Active_Opportunity__c = False;
}
else 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'){
c.Active_Opportunity__c = True;
}
conUpdList.add(c);
}
}
if(!conUpdList.isEmpty()){
update conUpdList;
}
}
Let me know if you any queries...
All Answers
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);
(opportunity optyObj2: 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 = False;
conList2 = [select Id, Active_Opportunity__c from contact where Id = :optyObj.Contact__c ];
for(contact conObj: conList){
conObj.Active_Opportunity__c = True;
}
update(conList);
update(conList2);
}
}
}
trigger activeOpportunity on Opportunity (after insert, after update) {
List<contact> conUpdList = new List <contact>();
List<Id> oppConIDList = new List<Id>();
List<opportunity> oppConList = new List<opportunity>();
List<Contact> conList = new List<Contact>();
Map<ID, Contact> conMap = new Map<ID, Contact>();
for (opportunity opp: Trigger.new){
if(opp.Contact__c!= null){
oppConList.add(opp);
oppConIDList.add(opp.Contact__c);
}
}
if(!oppConIDList.isEmpty()){
conList = [select Id, Active_Opportunity__c from contact where Id = :oppConIDList];
}
if(!conList.isEmpty()){
for(Contact con : conList){
conMap.put(con.id,con);
}
}
if(!oppConList.isEmpty()){
for(Opportunity optyObj : oppConList){
Contact c = conMap.get(optyObj.Contact__c);
if (optyObj.StageName == 'Closed Won' || optyObj.StageName == 'Closed Lost'){
c.Active_Opportunity__c = False;
}
else 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'){
c.Active_Opportunity__c = True;
}
conUpdList.add(c);
}
}
if(!conUpdList.isEmpty()){
update conUpdList;
}
}
Let me know if you any queries...
Would this be related to the way that Contact__c was set up as First Name last name? It is not an ID.
I adjusted this piece to get it to run...
if(!oppConIDList.isEmpty()){
conList = [select Name__c, Active_Opportunity__c from contact where Name__c = :oppConIDList];
Do I need to change the first part when selecting the list of things?