0

We have an APP that create tasks. Some of them should be related to accounts. Sometime the whatid is null so the task is linked to nothing.

We have a custom field in task object that contains relevant data relative to account (some phone numbers, actually).

I already have a code that make a map and value the task.

I already have a trigger that call my code.

I can see that, at this point, whatid is valued.

Then the task is created and ... whatid is null !

I looked at other triggers and i add my code at the end of all of them, to be sure noting overrite whatid with null value.

If I run my code on a new Task then insert it, the whatid is correct. So I am assuming that there is a problem with valuing whatid in a trigger.

Is there someone to help me ? :)

Trigger

trigger TaskTrigger on Task (before insert,before update) {
for(Task t : Trigger.new){
    if(
        t.WhatId == NULL
        && !String.isBlank(t.Description)
        && t.Description.contains('<toNumber>')
    ){
        t=BoundMissedCall.boundMissedCall(t);
        system.debug('TaskTrigger : before insert, after boundMissedCall = '+t);
    }    
}}

Code :

public abstract class BoundMissedCall{
public static Map<String, Account> mapPhoneAccount {get;set;}

static{
    mapPhoneAccount = new Map<String, Account>();
    List<Account> accounts = [SELECT id, Name, dealership_phone_mecanic__c FROM Account 
                              WHERE dealership_phone_mecanic__c != ''
                             ];
    for(Account a : accounts){
        // for each field in each account, put in the map
        if(!String.isBlank(a.dealership_phone_mecanic__c)) mapPhoneAccount.put(getCorrectPhoneNumber(a.dealership_phone_mecanic__c),a);
    }
}

public static Task boundMissedCall(Task t){
    if(t.WhatId != NULL)                        return t;
    if(String.isBlank(t.Description))           return t;
    if(!t.Description.contains('<toNumber>'))   return t;

    // operations
    Matcher phoneMatcher = Pattern.compile('<toNumber>(.*)</toNumber>').matcher(t.Description);
    if(phoneMatcher.find()){
        String phone = phoneMatcher.group(1);
        system.debug('BoundMissedCall.boundMissedCall() found "'+phone+'" in "'+t.Description+'"');
        phone = getCorrectPhoneNumber(phone);

        Account a = mapPhoneAccount.get(phone);
        if(a != NULL) {
            system.debug('BoundMissedCall.boundMissedCall() found account "'+a.Name+'" with ID "'+a.Id+'" ; setting ...');
            t.WhatId = Id.valueOf(a.id); 
        }
    }
    else{
        system.debug('BoundMissedCall.boundMissedCall() found NO phone in "'+t.Description+'"');
    }
    return t;
}
public static String getCorrectPhoneNumber(String phone){
    phone = phone.replaceAll('[^0-9]','');
    return phone.right(10);
}

}

  • 1
    Share your code as well. – Tushar Sharma Aug 2 '16 at 10:22
  • done (11 more to go!) – Guillaume Aug 2 '16 at 10:35
  • These two conditions can give you a chance to get what id = null :- if(String.isBlank(t.Description)) return t; if(!t.Description.contains('<toNumber>')) return t – Ashwani Aug 2 '16 at 10:37
  • Of course.But i tested it to check with system.debug that my task has correct value, then has WhatId valued with an id. But when the trigger is over WhatId is null. – Guillaume Aug 2 '16 at 10:39
  • check for WF and some other trigger which might other developer created in org – Tushar Sharma Aug 2 '16 at 10:40
0

Sorry guys, the answer was simple.

I add another test : querying my current task record to update my apex instance.

system.debug(t) -> whatId is null

t = [select ... where id=:t.id]

system.debug(t) -> whatId is valued

The way the currend apex instance is updated or not is a little confusing.

  • 1
    Make sure you mark your answer as the correct answer. That way if other people come here they can see clearly that it works, and other people won't waste their time trying to provide you with an answer. – Nick Cook Aug 2 '16 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.