1

I have a requirement. Contact related to activity, I need to pull his functional role ( custom field) on Activity page layout. So whenever I create an activity and relate with ant contact so his/her functional role should populate automatic.

Please help! How can I achieve it? I tried formula field but unable to get. I tried through trigger but it is not updating the field.

trigger updatefunctionalrole on Event (before insert, before update) {
// Create a map between the contact ID and its functional role value
Map<ID,String> confunrole = new Map<ID,String>();
List<Event> conEvents = new List<Event>();

// Loop through the triggered Events and add all of the contact IDs (for those associated with conortunities)
for (Event e : trigger.new) {
    // Only Events associated with contact
    if (e.whoID!= null && ((String)e.whoID).startsWith('003 ')) {
        // And only newly inserted events or those being reparented to an contact
        if (trigger.isInsert || (trigger.isUpdate && e.WhoID != trigger.oldMap.get(e.id).WhoID)) {
            confunrole.put(e.whoID,'');
            conEvents.add(e);
        }
    }
}
// Query the contact and add their functional role to the map
for (contact con : [SELECT Functional_Role__c FROM contact WHERE ID IN :confunrole.keySet()]) {
    confunrole.put(con.id,con.Functional_Role__c);
}
// Update the contact functional role field on the Event with the relevant value
for (Event e : conEvents) {
    e.contact_functional role__c = confunrole.get(e.whoID);
}
}

Test class is

@isTest             
private class updatefunctionalroleTest 
{
static testmethod void testbatch1() 
{
Account acc = new Account(Name='Test Account');
insert acc;



List<contact> cons = new List<contact>();
for (integer i=0; i<50; i++)
{
    contact con = new contact();
    con.AccountId=acc.Id;
    con.First Name='My';
    con.Last Name ='contact'+i;
    con.Email='na@na.com';
    con.Phone='+12345';
    con.Functional_Role__c = 'Information';
    con.Title = 'CEO';
    con.Level__c = 'Director';
    cons.add(con);
}
insert cons;

Event Evt = new Event();
Evt.whatId = acc.Id; 
Evt.Subject = 'Testing';
Evt.ShowAs = 'Busy';
Evt.Location = 'UAE';
Evt.ActivityDate = System.today()+15;
insert Evt ;

Test.startTest();
   updatefunctionalrole  obj = new  updatefunctionalrole ();
   Database.executeBatch(obj, 200);
Test.stopTest();

}
}
Russel baker 1
  • 377
  • 1
  • 8
  • 30

1 Answers1

2

Your last loop needs to operate on trigger.new. Change it as below:

// Update the contact functional role field on the Event with the relevant value
for (Event e : trigger.new) {
    e.contact_functional role__c = confunrole.get(e.whoID);
}

I would also recommend that you change the line below in your code and simply use a set of Ids instead. I'd avoid populating that map until you have both valid key and values.:

confunrole.put(e.whoID,''); 

EDIT:

Here's another issue I see with your code:

if (e.whoID!= null && ((String)e.whoID).startsWith('003 ')) 

This should be written as

if (e.whoID!= null && (String.valueOf(e.whoID)).startsWith('003')) 

That extra space would have been causing you issues. That having been said, it might be preferable to use a schema.describe method on the Id to equate the type with contact.

I suggest you also add system.debug statements to your code to help you find these issues. See How do I start to debug my own Apex code?.

EDIT 2 :

Your test class doesn't appear to be for the trigger. You seem to be trying to test a batch class. For the trigger you need two test methods. One method for beforeInsert and another for beforeUpdate. In your existing code, for a beforeInsert method, change your code as follows:

list<Event>Evts = new list<Event>();
for (integer i=0; i<50; i++)
{
    Event Evt = new Event();
    Evt.whatId = acc.Id;
    Evt.whoId = cons[i].Id; 
    Evt.Subject = 'Testing';
    Evt.ShowAs = 'Busy';
    Evt.Location = 'UAE';
    Evt.ActivityDate = System.today()+15;
    Evts.add(Evt);
}

Test.startTest();

    insert Evts ;
    //causes trigger to execute

Test.stopTest();

    // assert expected results
    for (integer i=0; i<50; i++)
    {    
        system.assertEquals(Evts[i].contact_functional role__c = cons[i].functional role__c);    
    }

} // end of beforeInsertTestMethod

  // Should have a No functional role__c TestMethod here
  {
      // create your contacts without a functional role__c if possible
      // you want to test that functionality in your trigger anyway
      // can use same test method to test AfterUpdate trigger  
      // Perform an update to contact functional role__c 
      // Do something to update the event (change date, etc)
      // Assert new functional role__c for each contact
  }

} // end of unit test class
crmprogdev
  • 40,955
  • 9
  • 58
  • 115