• FranchisingGuru
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies

I don't know what happened, but I had several triggers working previously, and now I can't even deploy previous triggers that used to be working in production.  The first trigger and class is below.. I think there is something wrong with my test class that is not making the trigger fire (test coverage is 100% in sandbox). Perhaps this causing the code coverage upon deployment to be 0%.... PLEASE HELP.

 

THIS IS MY TRIGGER THAT WORKS IN SANDBOX

 

trigger statusChangeAutoEmail on Lead (after update) {
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
List<Lead> leadsToUpdate=new List<Lead>();

List<Lead> leadList= [select Id, Email,X1st_Attempt_Email__c,X2nd_Attempt_Email__c, 
    Closed_Lost_Email__c, Status,Franchise_Concept__c, Owner.Name,Owner.Email from Lead 
    where International__c=false AND No_Auto_Email__c=false AND EmailBouncedReason=NULL AND
    (Status!='New Lead' or Status!='Qualified')]; 

for(Lead leadEmail : leadList) {
       
        if(leadEmail.Status == '1st Attempt' && leadEmail.X1st_Attempt_Email__c==false
        ) {       
            //send Email
            String template = ''; // initialize variable to hold template name filled in below           
            template = 'Standard Auto Sales Appt Reminder Email';
           
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTemplateId([select id from EmailTemplate where Name = :template].id);
            message.setTargetObjectId(leadEmail.id);
           
            // String to hold the email addresses to which the email is being sent. 
            message.setToAddresses (new String[] {leadEmail.Email});
            
            message.setsaveAsActivity(true);
            message.setsenderDisplayName(leadEmail.Owner.Name);
            message.setreplyTo(leadEmail.Owner.Email);

            // Add the e-mail to the list to be sent
            messages.add(message);
            
            //update to Lead so email only sent once
            leadEmail.X1st_Attempt_Email__c=true;
            leadsToUpdate.add(leadEmail);
            
        }            
                    
        
        else if(leadEmail.Status == '2nd Attempt' && leadEmail.X2nd_Attempt_Email__c==false
        ) {       
            //send Email
            String template = ''; // initialize variable to hold template name filled in below           
            template = 'Standard Auto Sales Appt Reminder Email';
           
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTemplateId([select id from EmailTemplate where Name = :template].id);
            message.setTargetObjectId(leadEmail.id);
           
            // String to hold the email addresses to which the email is being sent. 
            message.setToAddresses (new String[] {leadEmail.Email});
            
            message.setsaveAsActivity(true);
            message.setsenderDisplayName(leadEmail.Owner.Name);
            message.setreplyTo(leadEmail.Owner.Email);

            // Add the e-mail to the list to be sent
            messages.add(message);
            
            //update to Lead so email only sent once
            leadEmail.X2nd_Attempt_Email__c=true;
            leadsToUpdate.add(leadEmail);
                      
        }

        else if(leadEmail.Status == 'Closed - Lost' && leadEmail.Closed_Lost_Email__c==false
        ) {       
            //send Email
            String template = ''; // initialize variable to hold template name filled in below           
            template = 'Standard Auto Sales Appt Reminder Email';
           
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTemplateId([select id from EmailTemplate where Name = :template].id);
            message.setTargetObjectId(leadEmail.id);
           
            // String to hold the email addresses to which the email is being sent. 
            message.setToAddresses (new String[] {leadEmail.Email});
            
            message.setsaveAsActivity(true);
            message.setsenderDisplayName(leadEmail.Owner.Name);
            message.setreplyTo(leadEmail.Owner.Email);

            // Add the e-mail to the list to be sent
            messages.add(message);
            
            //update to Lead so email only sent once
            leadEmail.Closed_Lost_Email__c=true;
            leadsToUpdate.add(leadEmail);
                        
        }                     
        
        else{
        }
    }  //end for statement      
        Messaging.sendEmail(messages);
        update leadsToUpdate;    
} //end trigger

 

THIS IS MY TEST CLASS

 

@isTest
private class statusChangeAutoEmail {

    static testMethod void statusChangeAutoEmail() {

    List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
    List<Lead> leadsToUpdate=new List<Lead>();
    
    
    Lead[] leadsToCreate=new Lead[]{};
    for(Integer x=0; x<20; x++){
        Lead ld = new Lead(lastname='test', Status='1st Attempt', email='test@testing.com',company='test');
        leadsToCreate.add(ld);
    }    

    for(Integer x=0; x<20; x++){
        Lead ld = new Lead(lastname='test', Status='2nd Attempt', email='test@testing.com',company='test');
        leadsToCreate.add(ld);
    }

    for(Integer x=0; x<20; x++){
        Lead ld = new Lead(lastname='test', Status='Closed - Lost', email='test@testing.com',company='test',
        Closed_Lost_Dispositions__c='Other');
        leadsToCreate.add(ld);
    }    
        

    //Now for the test...
    Test.startTest();
    insert leadsToCreate;
    Messaging.sendEmail(messages);
    Test.stopTest();
        
        
        
    }
}

 

I'm pretty frantic at this point, since our sales team relies on several of these triggers that used to work... Help would be MOST APPRECIATED

 

-Amanda

I am a complete newbie to Apex triggers, but have semi-successfully completed a trigger that will initiate from an opportunity.  The trigger is supposed to retrieve the associated contact Ids by retrieving them from the OpportunityContactRole.  Once the contact(s) have been identified, I want to send an email to the contact and update a field within the contact. 

 

I have three main concerns/problems:

  1. If there is more than one contact associated to an opportunity, only one contact is updated and sent an email
  2. Is there a way to limit emails to send only once, rather than every time an opportunity is edited and meets the criteria? Or will I have to upsert an opportunity field (i.e. - a checkbox indicating email sent) and make sure that if that field is checked, the trigger does not fire?
  3. Is this trigger "bulkified"?

I have copied the applicable code for my trigger in progress below.  Again, my main issue is when there is more than one contact associated to an opportunity:

 

trigger portalEmailSend on Opportunity (after update) {
for(Opportunity oppEmail : Trigger.new) {
        if(oppEmail.StageName == 'Qualified' && oppEmail.Account_Name_Copy__c == 'Something'  && oppEmail.Send_Auto_Discovery_Portal_Emails__c==True) {       
            List<OpportunityContactRole> theOppConRole = [SELECT Id, OpportunityId,
ContactId FROM OpportunityContactRole WHERE OpportunityId=:oppEmail.ID];
//get OpportunityContactRole ID from Opportunity that initiated trigger
            for (OpportunityContactRole contactRole:theOppConRole) {
// add OpportunityContactRole to the main OpportunityContactRole list
theOppConRole.add(contactRole);
                     
                 if(contactRole.ContactId==NULL){

                 }    
                 else{       
                 //Sending email to matching contact(s)
                 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                 mail.settargetObjectId(contactRole.ContactId);
                 mail.settemplateId('IdofTemplate');
                 mail.setsaveAsActivity(true);
                 mail.setsenderDisplayName(oppEmail.Owner.Name);
                 mail.setreplyTo(oppEmail.Owner.Email);
                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                
            
                //updating opportunity stage field in matching contact record(s)
                List<Contact> inQuestion = [SELECT Id, FirstName, LastName,
Opportunity_Stage__c FROM Contact WHERE Id=:contactRole.ContactId];
                    for (Contact contactMatch:inQuestion) {
                    
                    contactMatch.Opportunity_Stage__c = 'Qualified';
                    inQuestion.add(contactMatch);
                    upsert contactMatch;
                    break; //exit after upsert to avoid continuous looping
                    }
                    break; //break initial for loop that processes contact data
              }
            }
        }

I don't know what happened, but I had several triggers working previously, and now I can't even deploy previous triggers that used to be working in production.  The first trigger and class is below.. I think there is something wrong with my test class that is not making the trigger fire (test coverage is 100% in sandbox). Perhaps this causing the code coverage upon deployment to be 0%.... PLEASE HELP.

 

THIS IS MY TRIGGER THAT WORKS IN SANDBOX

 

trigger statusChangeAutoEmail on Lead (after update) {
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
List<Lead> leadsToUpdate=new List<Lead>();

List<Lead> leadList= [select Id, Email,X1st_Attempt_Email__c,X2nd_Attempt_Email__c, 
    Closed_Lost_Email__c, Status,Franchise_Concept__c, Owner.Name,Owner.Email from Lead 
    where International__c=false AND No_Auto_Email__c=false AND EmailBouncedReason=NULL AND
    (Status!='New Lead' or Status!='Qualified')]; 

for(Lead leadEmail : leadList) {
       
        if(leadEmail.Status == '1st Attempt' && leadEmail.X1st_Attempt_Email__c==false
        ) {       
            //send Email
            String template = ''; // initialize variable to hold template name filled in below           
            template = 'Standard Auto Sales Appt Reminder Email';
           
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTemplateId([select id from EmailTemplate where Name = :template].id);
            message.setTargetObjectId(leadEmail.id);
           
            // String to hold the email addresses to which the email is being sent. 
            message.setToAddresses (new String[] {leadEmail.Email});
            
            message.setsaveAsActivity(true);
            message.setsenderDisplayName(leadEmail.Owner.Name);
            message.setreplyTo(leadEmail.Owner.Email);

            // Add the e-mail to the list to be sent
            messages.add(message);
            
            //update to Lead so email only sent once
            leadEmail.X1st_Attempt_Email__c=true;
            leadsToUpdate.add(leadEmail);
            
        }            
                    
        
        else if(leadEmail.Status == '2nd Attempt' && leadEmail.X2nd_Attempt_Email__c==false
        ) {       
            //send Email
            String template = ''; // initialize variable to hold template name filled in below           
            template = 'Standard Auto Sales Appt Reminder Email';
           
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTemplateId([select id from EmailTemplate where Name = :template].id);
            message.setTargetObjectId(leadEmail.id);
           
            // String to hold the email addresses to which the email is being sent. 
            message.setToAddresses (new String[] {leadEmail.Email});
            
            message.setsaveAsActivity(true);
            message.setsenderDisplayName(leadEmail.Owner.Name);
            message.setreplyTo(leadEmail.Owner.Email);

            // Add the e-mail to the list to be sent
            messages.add(message);
            
            //update to Lead so email only sent once
            leadEmail.X2nd_Attempt_Email__c=true;
            leadsToUpdate.add(leadEmail);
                      
        }

        else if(leadEmail.Status == 'Closed - Lost' && leadEmail.Closed_Lost_Email__c==false
        ) {       
            //send Email
            String template = ''; // initialize variable to hold template name filled in below           
            template = 'Standard Auto Sales Appt Reminder Email';
           
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setTemplateId([select id from EmailTemplate where Name = :template].id);
            message.setTargetObjectId(leadEmail.id);
           
            // String to hold the email addresses to which the email is being sent. 
            message.setToAddresses (new String[] {leadEmail.Email});
            
            message.setsaveAsActivity(true);
            message.setsenderDisplayName(leadEmail.Owner.Name);
            message.setreplyTo(leadEmail.Owner.Email);

            // Add the e-mail to the list to be sent
            messages.add(message);
            
            //update to Lead so email only sent once
            leadEmail.Closed_Lost_Email__c=true;
            leadsToUpdate.add(leadEmail);
                        
        }                     
        
        else{
        }
    }  //end for statement      
        Messaging.sendEmail(messages);
        update leadsToUpdate;    
} //end trigger

 

THIS IS MY TEST CLASS

 

@isTest
private class statusChangeAutoEmail {

    static testMethod void statusChangeAutoEmail() {

    List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
    List<Lead> leadsToUpdate=new List<Lead>();
    
    
    Lead[] leadsToCreate=new Lead[]{};
    for(Integer x=0; x<20; x++){
        Lead ld = new Lead(lastname='test', Status='1st Attempt', email='test@testing.com',company='test');
        leadsToCreate.add(ld);
    }    

    for(Integer x=0; x<20; x++){
        Lead ld = new Lead(lastname='test', Status='2nd Attempt', email='test@testing.com',company='test');
        leadsToCreate.add(ld);
    }

    for(Integer x=0; x<20; x++){
        Lead ld = new Lead(lastname='test', Status='Closed - Lost', email='test@testing.com',company='test',
        Closed_Lost_Dispositions__c='Other');
        leadsToCreate.add(ld);
    }    
        

    //Now for the test...
    Test.startTest();
    insert leadsToCreate;
    Messaging.sendEmail(messages);
    Test.stopTest();
        
        
        
    }
}

 

I'm pretty frantic at this point, since our sales team relies on several of these triggers that used to work... Help would be MOST APPRECIATED

 

-Amanda