• Alex Dorn
  • NEWBIE
  • 30 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 12
    Replies
I have edited a trigger and test class to apply to my needs. I just need the custom object on conversion to update the custom object on an opportunity where previously it updated contact, account, and the opportunity. I recieve this error when testing
Failure Message: "System.Exception: SFSSDupeCatcher:Too many SOQL queries: 101", Failure Stack Trace: "(SFSSDupeCatcher)"
Is this an error with DupeCatcher or with the Trigger/test class.

Trigger:
trigger UpdateCustomeObject_Trigger on Lead (before update) {
//This trigger will associate a Custom Object record with the contact and opportunity associated to the
//lead after it has been converted.
//The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
            Set<Id> leadIds = new Set<Id>();
            for (Lead lead : Trigger.new)
                leadIds.add(lead.Id);
           
            Map<Id, Reached_Out__c> entries = new Map<Id, Reached_Out__c>([select test__c, Lead__c from Reached_Out__c where lead__c in :leadIds]);       
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.new)  {
                    for (Reached_Out__c CustomObject : entries.values()) {
                        if (CustomObject.Lead__c == lead.Id) {
                            CustomObject.test__c = lead.ConvertedOpportunityId;
                            update CustomObject;
                        }
                    }
                }
            }
        }
    }

}

Test Class:
@isTest
//This is a test case for a situation where a lead will be converted.  The developer must explicitly call the convert lead
//method to simulate the user action.

private class TestTriggerCustomObjectUpdate {
        static testMethod void TestReferralUpdate() {
        // Insert the Lead
        List<Lead> leads = new List<Lead>();
        Lead leadt = new Lead (FirstName ='fname', LastName ='test', Company ='myCompany',  Website = 'TestWebsite.com', Origin_Date__C = Date.newInstance(2014,12,31), Origin__c = 'Other', Status = 'Exception', Exception_Notes__c='asdf');
        insert leadt;
        // Insert the custom object Record
        Reached_Out__C customobject = new Reached_Out__C (Lead__c = leadt.Id);
        insert customobject;       
       
        //Convert the Lead
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(leadt.Id);
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);   
       
        //Requery for the referral record to see if it is updated
        Reached_Out__C ref_upd = [select Test__c from Reached_Out__C where Lead__c = :leadt.Id];
       
        //Check that the test passed
        System.assertEquals(ref_upd.Test__c,[Select ConvertedOpportunityId From Lead Where Id = :customobject.Lead__c].ConvertedOpportunityId);      
   
        //Test if no opty is created
        string NoOpty = 'Y';       
        if (NoOpty =='Y'){
            Lead leadto = new Lead (FirstName ='fnameo', LastName ='testo', Company ='myCompanyo');
            insert leadto;
            // Insert the custom object record
            Reached_Out__C customobjecto = new Reached_Out__C (Lead__c = leadto.Id);
            insert customobjecto;
           
            Database.LeadConvert lco = new database.LeadConvert();
            lco.setLeadId(leadto.Id);
            lco.isDoNotCreateOpportunity();
            lco.setDoNotCreateOpportunity(true);
            LeadStatus convertStatuso = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            lco.setConvertedStatus(convertStatuso.MasterLabel);
            Database.LeadConvertResult lcro = Database.convertLead(lco);
       
            Reached_Out__C ref_updo = [select Test__c from Reached_Out__C where Lead__c = :leadto.Id];
           
            //Check that the test passed
            System.assert(ref_updo.Test__c == null);
        }  
    }

    static testMethod void testBulkUpdate() {
        List<Lead> leads = new List<Lead>();      
        for (Integer i=0;i<5;i++) {
            Lead l = new Lead (FirstName ='bulk', LastName ='Test', Company ='myCompanyo');
            insert l;
            // Insert the Custom Record
            Reached_Out__C r = new Reached_Out__C (Lead__c = l.Id);
            insert r;
           
            //Convert the Lead
            Database.LeadConvert lcb = new database.LeadConvert();
            lcb.setLeadId(l.Id);
            LeadStatus convertStatusb = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            lcb.setConvertedStatus(convertStatusb.MasterLabel);
            Database.LeadConvertResult lcrb = Database.convertLead(lcb);
           
            Reached_Out__C bulkup = [select  Test__c from Reached_Out__C where Lead__c =:l.Id];
           
            //Check that the test has passed
            System.assertEquals(bulkup.Test__c,[Select ConvertedOpportunityId From Lead Where Id = :r.Lead__c].ConvertedOpportunityId);
            }  
        }
}
I spent all of yesterday trying to write test classes for my two new triggers, but with zero experience with coding it has been a very difficult process.  All the apex triggers do is update the Last_activity_type__c field when a new activity is logged. Is there anyone who can help me write the test class code? Here are my triggers:
List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();

for(Task t :trigger.new)
    {
    if(t.whatid!=null)
    {
        Schema.SObjectType tType= t.whatid.getSObjectType();
        if(tType == Opportunity.Schema.SObjectType)
        {
            OpportunityIds.add(t.Whatid);
        }
    }
    }
    {
    //Querying the related Opportunity based on whatid on Task
    Map<Id,Opportunity> OpportunityMap =  new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
 
    for(Task t :Trigger.new)

        for(Opportunity l : OpportunityMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            OpportunityList.add(l);
        }
    }
    // updating the Opportunity
    if(OpportunityList.size()>0)
    {
        update OpportunityList;
    }
}

trigger updateRelatedLead on Task (after insert,after update) {

List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();

for(Task t :trigger.new)
    {
    if(t.whoId!=null)
    {
        Schema.SObjectType tType= t.whoId.getSObjectType();
        if(tType == Lead.Schema.SObjectType)
        {
            LeadIds.add(t.WhoId);
        }
    }
    }
    {
    //Querying the related Lead based on whoId on Task
    Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Subject__C from Lead where id in:LeadIds]);
 
    for(Task t :Trigger.new)

        for(Lead l : LeadMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            LeadList.add(l);
        }
    }
    // updating the Lead
    if(LeadList.size()>0)
    {
        update LeadList;
    }
}
How would I write a test class for each of these two codes I am beyond confused with Apex. The triggers just update the Last_activity_type__c field when a new activity is logged.

List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();

for(Task t :trigger.new)
    {
    if(t.whatid!=null)
    {
        Schema.SObjectType tType= t.whatid.getSObjectType();
        if(tType == Opportunity.Schema.SObjectType)
        {
            OpportunityIds.add(t.Whatid);
        }
    }
    }
    {
    //Querying the related Opportunity based on whatid on Task
    Map<Id,Opportunity> OpportunityMap =  new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
  
    for(Task t :Trigger.new)

        for(Opportunity l : OpportunityMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            OpportunityList.add(l);
        }
    }
    // updating the Opportunity
    if(OpportunityList.size()>0)
    {
        update OpportunityList;
    }
}

trigger updateRelatedLead on Task (after insert,after update) {

List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();

for(Task t :trigger.new)
    {
    if(t.whoId!=null)
    {
        Schema.SObjectType tType= t.whoId.getSObjectType();
        if(tType == Lead.Schema.SObjectType)
        {
            LeadIds.add(t.WhoId);
        }
    }
    }
    {
    //Querying the related Lead based on whoId on Task
    Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Subject__C from Lead where id in:LeadIds]);
  
    for(Task t :Trigger.new)

        for(Lead l : LeadMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            LeadList.add(l);
        }
    }
    // updating the Lead
    if(LeadList.size()>0)
    {
        update LeadList;
    }
}
I am attempting to deploy two fields and two triggers that place the Last Activity Subject into a field I made for leads and opportunities the triggers work perfectly in sandbox, but won't deploy in the production version. I am moving the two triggers along with the two fields the triggers reference. The code coverage listed in sandbox is 0% (0/14) for both I don't know if this is the problem, but they work perfectly and I haven't run into any problems when testing them in sandbox.

Here are the errors:
updateRelatedLead               Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
updateRelatedOpportunity    Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Deploy Error                             Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required.

And the two codes:
trigger updateRelatedOpportunity on Task (after insert,after update) {

List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();

for(Task t :trigger.new)
    {
    if(t.whatid!=null)
    {
        Schema.SObjectType tType= t.whatid.getSObjectType();
        if(tType == Opportunity.Schema.SObjectType)
        {
            OpportunityIds.add(t.Whatid);
        }
    }
    }
    {
    //Querying the related Opportunity based on whatid on Task
    Map<Id,Opportunity> OpportunityMap =  new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
   
    for(Task t :Trigger.new)

        for(Opportunity l : OpportunityMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            OpportunityList.add(l);
        }
    }
    // updating the Opportunity
    if(OpportunityList.size()>0)
    {
        update OpportunityList;
    }
}

trigger updateRelatedLead on Task (after insert,after update) {

List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();

for(Task t :trigger.new)
    {
    if(t.whoId!=null)
    {
        Schema.SObjectType tType= t.whoId.getSObjectType();
        if(tType == Lead.Schema.SObjectType)
        {
            LeadIds.add(t.WhoId);
        }
    }
    }
    {
    //Querying the related Lead based on whoId on Task
    Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Subject__C from Lead where id in:LeadIds]);
   
    for(Task t :Trigger.new)

        for(Lead l : LeadMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            LeadList.add(l);
        }
    }
    // updating the Lead
    if(LeadList.size()>0)
    {
        update LeadList;
    }
}
After working with some people on here I was able to get a code that made it so the last activity's subject was placed in a Last_Activity_Subject__C field. I would like to make this same code for our opportunities. All I did to alter the code was replace any instance of Lead with Opportunity, and there was no errors saving, but the code does not work. Can anyone provide insight on what else I have to change. Here is the code:

trigger updateRelatedOpportunity on Task (after insert,after update) {

List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();

for(Task t :trigger.new)
    {
    if(t.whoId!=null)
    {
        Schema.SObjectType tType= t.whoId.getSObjectType();
        if(tType == Opportunity.Schema.SObjectType)
        {
            OpportunityIds.add(t.WhoId);
        }
    }
    }
    {
    //Querying the related Opportunity based on whoId on Task
    Map<Id,Opportunity> OpportunityMap =  new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
    
    for(Task t :Trigger.new)

        for(Opportunity l : OpportunityMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            OpportunityList.add(l);
        }
    }
    // updating the Opportunity
    if(OpportunityList.size()>0)
    {
        update OpportunityList;
    }
}
This is the first time I have used an Apex trigger and I am struggling to get it to work. I took the code used from here https://developer.salesforce.com/forums?id=906F00000008yXUIAY. And then adjusted it so it fit for leads and the names of fields I currently have in my for my leads. I am not getting any errors now, but the code does not seem to be doing anything and is active. The items I changed were changing any opportunity or opp to lead, whatid to id to get rid of errors, and the name of fields. What i want to have happen is the subject of activities to become its own field, and only display the last activity subject. The Last_Activity_Date_c field is a field I created that uses a simple formula that pulls the last activity date through. If anyone has any insight it would be greatly appreciated. Here is my code for reference:

trigger NextTastInfo on Lead (after insert, after update) {
          

  if(Trigger.new.size() == 1 ) {

    Lead tk = Trigger.New[0];
    String str = tk.id;
    if(str != null && str.substring(0,3)== '006')
    {

         Lead lead = [select OwnerId,Last_Activity_Subject__c,Last_Activity_Date__c from Lead where Id = :tk.id ];

        List<Task> tskMin = [Select ActivityDate,Subject From Task where id=:tk.id and  what.type = 'Lead' and isClosed = false order By ActivityDate limit 1];

        if (tskMin.size()>0) {
                lead.Next_Step_Date__c=tskMin[0].ActivityDate;
                lead.Last_Activity_Subject__c=tskMin[0].Subject;
        }
        else {
                lead.Next_Step_Date__c=null;      
                lead.Last_Activity_Subject__c='';
        }
        update lead;
    }
}
}
This is the first time I have used an Apex trigger and I am struggling to get it to work. I took the code used from here https://developer.salesforce.com/forums?id=906F00000008yXUIAY. And then adjusted it so it fit for leads and the names of fields I currently have in my for my leads. I am not getting any errors now, but the code does not seem to be doing anything and is active. The items I changed were changing any opportunity or opp to lead, whatid to id to get rid of errors, and the name of fields. What i want to have happen is the subject of activities to become its own field, and only display the last activity subject. The Last_Activity_Date_c field is a field I created that uses a simple formula that pulls the last activity date through. If anyone has any insight it would be greatly appreciated. Here is my code for reference:

trigger NextTastInfo on Lead (after insert, after update) {
          

  if(Trigger.new.size() == 1 ) {

    Lead tk = Trigger.New[0];
    String str = tk.id;
    if(str != null && str.substring(0,3)== '006')
    {

         Lead lead = [select OwnerId,Last_Activity_Subject__c,Last_Activity_Date__c from Lead where Id = :tk.id ];

        List<Task> tskMin = [Select ActivityDate,Subject From Task where id=:tk.id and  what.type = 'Lead' and isClosed = false order By ActivityDate limit 1];

        if (tskMin.size()>0) {
                lead.Next_Step_Date__c=tskMin[0].ActivityDate;
                lead.Last_Activity_Subject__c=tskMin[0].Subject;
        }
        else {
                lead.Next_Step_Date__c=null;      
                lead.Last_Activity_Subject__c='';
        }
        update lead;
    }
}
}
I spent all of yesterday trying to write test classes for my two new triggers, but with zero experience with coding it has been a very difficult process.  All the apex triggers do is update the Last_activity_type__c field when a new activity is logged. Is there anyone who can help me write the test class code? Here are my triggers:
List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();

for(Task t :trigger.new)
    {
    if(t.whatid!=null)
    {
        Schema.SObjectType tType= t.whatid.getSObjectType();
        if(tType == Opportunity.Schema.SObjectType)
        {
            OpportunityIds.add(t.Whatid);
        }
    }
    }
    {
    //Querying the related Opportunity based on whatid on Task
    Map<Id,Opportunity> OpportunityMap =  new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
 
    for(Task t :Trigger.new)

        for(Opportunity l : OpportunityMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            OpportunityList.add(l);
        }
    }
    // updating the Opportunity
    if(OpportunityList.size()>0)
    {
        update OpportunityList;
    }
}

trigger updateRelatedLead on Task (after insert,after update) {

List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();

for(Task t :trigger.new)
    {
    if(t.whoId!=null)
    {
        Schema.SObjectType tType= t.whoId.getSObjectType();
        if(tType == Lead.Schema.SObjectType)
        {
            LeadIds.add(t.WhoId);
        }
    }
    }
    {
    //Querying the related Lead based on whoId on Task
    Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Subject__C from Lead where id in:LeadIds]);
 
    for(Task t :Trigger.new)

        for(Lead l : LeadMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            LeadList.add(l);
        }
    }
    // updating the Lead
    if(LeadList.size()>0)
    {
        update LeadList;
    }
}
After working with some people on here I was able to get a code that made it so the last activity's subject was placed in a Last_Activity_Subject__C field. I would like to make this same code for our opportunities. All I did to alter the code was replace any instance of Lead with Opportunity, and there was no errors saving, but the code does not work. Can anyone provide insight on what else I have to change. Here is the code:

trigger updateRelatedOpportunity on Task (after insert,after update) {

List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();

for(Task t :trigger.new)
    {
    if(t.whoId!=null)
    {
        Schema.SObjectType tType= t.whoId.getSObjectType();
        if(tType == Opportunity.Schema.SObjectType)
        {
            OpportunityIds.add(t.WhoId);
        }
    }
    }
    {
    //Querying the related Opportunity based on whoId on Task
    Map<Id,Opportunity> OpportunityMap =  new Map<Id,Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
    
    for(Task t :Trigger.new)

        for(Opportunity l : OpportunityMap.Values())
        {
            l.Last_Activity_Subject__C = t.subject;
            OpportunityList.add(l);
        }
    }
    // updating the Opportunity
    if(OpportunityList.size()>0)
    {
        update OpportunityList;
    }
}
This is the first time I have used an Apex trigger and I am struggling to get it to work. I took the code used from here https://developer.salesforce.com/forums?id=906F00000008yXUIAY. And then adjusted it so it fit for leads and the names of fields I currently have in my for my leads. I am not getting any errors now, but the code does not seem to be doing anything and is active. The items I changed were changing any opportunity or opp to lead, whatid to id to get rid of errors, and the name of fields. What i want to have happen is the subject of activities to become its own field, and only display the last activity subject. The Last_Activity_Date_c field is a field I created that uses a simple formula that pulls the last activity date through. If anyone has any insight it would be greatly appreciated. Here is my code for reference:

trigger NextTastInfo on Lead (after insert, after update) {
          

  if(Trigger.new.size() == 1 ) {

    Lead tk = Trigger.New[0];
    String str = tk.id;
    if(str != null && str.substring(0,3)== '006')
    {

         Lead lead = [select OwnerId,Last_Activity_Subject__c,Last_Activity_Date__c from Lead where Id = :tk.id ];

        List<Task> tskMin = [Select ActivityDate,Subject From Task where id=:tk.id and  what.type = 'Lead' and isClosed = false order By ActivityDate limit 1];

        if (tskMin.size()>0) {
                lead.Next_Step_Date__c=tskMin[0].ActivityDate;
                lead.Last_Activity_Subject__c=tskMin[0].Subject;
        }
        else {
                lead.Next_Step_Date__c=null;      
                lead.Last_Activity_Subject__c='';
        }
        update lead;
    }
}
}