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='[email protected]';
    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();

}
}

1 Answer 1

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
12
  • It's still not updating functional role field on events. Jun 26, 2016 at 11:20
  • see my edited answer
    – crmprogdev
    Jun 26, 2016 at 12:11
  • Code is working fine Now Thanks. Can you please help me through write the test class. @crmprogdev Jun 26, 2016 at 13:10
  • What are you having difficulty with? It would seem to me to be pretty straightforward. create a list of accounts, related contacts and insert a list of Tasks both with and without the field populated, then assert the results for each. If you need help learning Test Methods, visit Trailhead Apex Testing Module. If this post led you to solving your issue, please help the rest of the community by marking it as having answered your question.
    – crmprogdev
    Jun 26, 2016 at 17:53
  • It gives me error. crmprogdev Jun 27, 2016 at 5:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .