• schmichael
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies

Hello All,

 

I modified a trigger I found online to suit my company's needs, but I'm having issues writing a test for it. I'm able to test the first 4 lines no problem, but I'm having issues testing the last two (beginning with the for loop). Can anyone provide assistance?

 

trigger OppTaskContactRole on Task (before insert, before update)
{

    for (Task curr: Trigger.new)
    {
        if (curr.WhatId != null && curr.WhoID == null && curr.WhatId.getsobjecttype()==opportunity.sobjecttype )  {
            List<OpportunityContactRole> roles = [SELECT OpportunityId, ContactId
            FROM OpportunityContactRole
            WHERE isPrimary = true AND OpportunityId = :curr.whatId
            LIMIT 1];
                
            if(!roles.isEmpty()){
                for(OpportunityContactRole ocr : roles){
                    curr.WhoId = ocr.ContactId;
                }
            }
        }
    }
}

Hello All,

 

I'm trying to create a variable that tracks the number of calls performed on a lead. I thought this would be really simple to do as a formula or with workflow rules, but I couldn't actually figure out a simple way to do it without using Apex. I modified an existing trigger I had created, and I thought I had a functional piece of code, but it doesn't work at all. What I have right now looks like this:

 

trigger callCountUpdate on Task (after insert) {
map<id,lead> leads = new map<id,lead>(), updates = new map<id,lead>();
for(task record:trigger.new)
if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype)
leads.put(record.whoid,null);
leads.putall([select id,call_count__c from lead where id in :leads.keyset()]);
for(task record:trigger.new){
if(record.type == 'Call'){
updates.put(record.whoid,new lead(id=record.whoid,call_count__c = leads.get(record.whoid).call_count__c++));
}
}
update updates.values();
}

 

Can anyone help me figure this out, or just tell me a way to track this without using Apex?

 

Thanks very much!

Hello All,

 

After finding some examples on this forum, I wrote this Apex Trigger for Tasks:

 

trigger taskActivityDate on Task (after insert) {
    
List<Lead> updatedLeads= new List<Lead>();
 
    for(Task newTask:Trigger.new){
            if(string.valueOf(NewTask.WhoId).startsWith('00Q'))
            {
            Lead changedLead = [select id, First_Activity_Date__c from lead where id=:newTask.WhoId];
               if(changedLead.First_Activity_Date__c == NULL){
                   changedLead.First_Activity_Date__c = DATE.newinstance(newTask.CreatedDate.year(),newTask.CreatedDate.month(),newTask.CreatedDate.day());
                   updatedLeads.add(changedLead);
           }}
   
              update updatedLeads;             
   }
}

 

I did not realize that I needed to create tests to prove that this code is functional. I have no idea how to write unit tests, nevertheless a test specific to this Trigger. Would anyone be able to help me write this test, or just provide some good explanation on what the tests need to do in order to cover this code? I've already read some of the basics in other salesforce help forums, but they haven't really helped me very much.

 

Thanks a bunch!

Hello All,

 

After finding some examples on this forum, I wrote this Apex Trigger for Tasks:

 

trigger taskActivityDate on Task (after insert) {
    
List<Lead> updatedLeads= new List<Lead>();
 
    for(Task newTask:Trigger.new){
            if(string.valueOf(NewTask.WhoId).startsWith('00Q'))
            {
            Lead changedLead = [select id, First_Activity_Date__c from lead where id=:newTask.WhoId];
               if(changedLead.First_Activity_Date__c == NULL){
                   changedLead.First_Activity_Date__c = DATE.newinstance(newTask.CreatedDate.year(),newTask.CreatedDate.month(),newTask.CreatedDate.day());
                   updatedLeads.add(changedLead);
           }}
   
              update updatedLeads;             
   }
}

 

I did not realize that I needed to create tests to prove that this code is functional. I have no idea how to write unit tests, nevertheless a test specific to this Trigger. Would anyone be able to help me write this test, or just provide some good explanation on what the tests need to do in order to cover this code? I've already read some of the basics in other salesforce help forums, but they haven't really helped me very much.

 

Thanks a bunch!