+ Start a Discussion
calmarkcalmark 

class or trigger not working do to something, i have no idea

I am really knew to all this and having trouble deploying a trigger that will udpate a field in a contact when the status of an activity changes.  At this point I have the trigger working just find but the class I created for testing fails and I have no idea why.  Thank you for taking the time to thelp!  

 

The message I get when I run the class is: 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Update_Last_VM_Left: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] Trigger.Update_Last_VM_Left: line 7, column 1: []

 

Stack Trace:

Class.TaskTestMethod.TestTasktrigger: line 12, column 1

 

 

And here's my code:

 

Trigger:

1
2
3
4
5
6
7
8
9
trigger Update_Last_VM_Left on Task (after insert, after update)
{
  Task ts= Trigger.new[0];
  if(ts.Status == 'VM Left'){
      Contact contactToUpdate = new Contact(id=ts.WhoID);
      contactToUpdate.Last_VM_Left__c = date.today();
      update contactToUpdate;
  } 
}

 

Class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@isTest
private class TaskTestMethod{
static testMethod void TestTasktrigger()
    {
            Task[] CreateTask = new Task[]{};
            Contact[] CreateContact = new Contact[]{};
            for(Integer x=0; x<100;x++){
            Task ts = new Task(Subject='Apex Test',Status='VM Left');
            CreateTask.add(ts);    
            }  
        Test.startTest();
        insert CreateTask;
        Test.stopTest(); 
    }
}

 

sfdcfoxsfdcfox

It's because you don't check to see if WhoId is blank (which it is), nor are you actually creating contacts, inserting them into the database, or assigning them to the task. Also note, I'm sure you're aware that your code isn't bulkified, and that's also a no-no. Even worse, your design doesn't appear to take into account the various means by which data might be imported out of order, so your field might be wrong in some cases. Finally, you also fail to check if the WhoId is actualy a contact, and not, for example, a lead.

 

trigger tasktrigger on task ( after insert, after update, after delete, after undelete ) {
  set< id > whoids = new set< id >( );
  if( trigger.old != null ) {
    for( task t : trigger.old ) {
      whoids.add( t.whoid );
    }
  }
  if( trigger.new != null ) {
    for( task t : trigger.new ) {
      whoids.add( t.whoid );
    }
 }
 update [select id from contact where id in :whoids];
}

 

task contacttrigger on contact ( before update ) {
  for( contact c : [select id,(select id, activityduedate, status from tasks where status = 'VM Left' order by activitydate desc limit 1) from contact where id in :trigger.new] ) {
    if( c.tasks.isempty( ) ) {
      trigger.newmap.get( c.id ).last_vm_update__c = null;
    } else {
      trigger.newmap.get( c.id ).last_vm_update__c = c.tasks[0].activityduedate;
  }
}

As for your test method, remember that you must first insert the contacts, then use the contacts' id values in the tasks' whoid field.

 

calmarkcalmark

oh man, you are so right.  I need to stop trying to do this myself.  I wish I could just hire someone, are you a freelancer?  I have tried oDes but I get just a handful of interested parties and then they don't appear to be a good fit.  Any idea where I can find people who have this skillset? 

sfdcfoxsfdcfox

There are plenty of good people out there that are a good fit, but they're (mostly) all hired by companies like Astadia, Blue Wolf, and the like, and usually run about $200-250 an hour for their services. That said, though, I know there are some lurkers here that do inexpensive, high quality work, and I might be able to pass you a reference to some people I know, if they have the bandwidth for such things.

calmarkcalmark

Thanks, I also just posted a job on app exchange, maybe I can find someone there.  Any idea how much freelancers charge?

sfdcfoxsfdcfox

It's tough to say, because freelancers often have their own personal goals. In a different life, when I had all the work I could handle, I was charing $50 an hour, and I was absolutely making a killing. Most freelancers I've known ran the gamut from $25 to $150, and often not a matter of professionalism. And I've done contract work for as low as $12 an hour, too, back when times were not so great. I think the current trend is between $75 and $100, as the economy is still weak, but starting to pick up again (so, I'd expect costs to rise as demand picks up going into 2013).

ngabraningabrani

Mark,

I have applied for the position. You can contact me at ngabrani At astreait dot com. I am including sample code for the trigger and test case. It may need to be polished a little bit, before it goes live.

Thanks,

Naveen


trigger updateRelatedContact on Task (after insert,after update) {
    set<id> ContactIDset = new set<id>();

    //Here we will check whether task is being inserted or updated
    for(task ts: trigger.new){
        // If task is new and status is VM Left then only Field will be updated
        if(trigger.isinsert && ts.whoid != null && ts.status == 'VM Left' ){
            string s = ts.whoid;
            if(s.substring(0,3) == '003')
                ContactIDset.add(ts.whoid);
        }     
        //Here we are checking if task is getting update and earlier status is different than lastest and new status is VM Left
        if(trigger.isupdate && system.trigger.oldmap.get(ts.id).status != ts.status && ts.whoid != null && ts.status == 'VM Left' ){
              string s = ts.whoid;
              if(s.substring(0,3) == '003') //here we are checking that Whoid is consisting record of Contact object only 
                   ContactIDset.add(ts.whoid);
             }  
         }
         list<contact> contactList = [select id,Last_VM_Left__c from contact where id IN: ContactIDset];


         for(contact con: contactList){
             con.Last_VM_Left__c = system.today();
             // contactList.add(con);
          }
          upsert contactList;
}


TEST CASE

@istest(seealldata = true)

public class UpdateRelatedContactTest{
    static testMethod void TestAccountOwnerUpdateUsingOECount(){
    list<user> userList = [select id from user where isactive =: true limit 1];

    contact con = new contact();
    con.lastname = 'test';
    con.Last_VM_Left__c = system.today();
    insert con;

    Task t = new task();
    t.whoid = con.id;
    t.ownerid =userlist[0].id;
    t.subject = 'test';
    t.status = 'test';
    t.priority = 'normal';
    insert t;

    t.status = 'VM left';
    upsert t;
  }
}

 

 

sfdc.geekssfdc.geeks

Hi Mark,

 

I hope you should have a solution for this by now. If not, or you are seeking for any for any help further I am willing to help you on coding part. I am having 2+ years experience on coding/developing. You can reach me at sfdc.geeks@gmail.com

 

Cheers!