Skip to main content
2 of 2
added 757 characters in body
Vigneshwaran G
  • 6.1k
  • 3
  • 23
  • 39

Your trigger won't update Account__c when you create the Task under an Account. Its because Account__c field is overridden to blank in this line

for(Task t : trigger.new){
    t.Account__c = mapAccounts.get(t.whatId);
}

Reason:

When you do a get from the map mapAccounts using t.whatId which is an Account Id it returns null and assigns it to Account__c because your map is populated with key as id of Property relation object and value as id of Account. So when you do a get from the map with id of Account it will always return null. So change that to

for(Task t : trigger.new){
    if(t.Account__c == NULL){
        t.Account__c = mapAccounts.get(t.whatId);
    }
}

Explanation:

The added if condition checks whether the Account__c field is null and assign the value returned from mapAccounts.

For task created on Property relation object, the Account__c will be null it will be populated here. For task created on Account, the Account__c is already populated in this line.

if(strTempId != null && strTempId.startsWith('001')){
    System.debug('++++++++InIF+++++++++++');
    t.Account__C = t.WhatId;
}

So it won't be null and the if condition gets skipped keeping the value.

Hope it helps.

Vigneshwaran G
  • 6.1k
  • 3
  • 23
  • 39