function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ronen SlifkinRonen Slifkin 

Trigget on task , can't update WhoID

I didn't copied all of the code of the trigger only the line that's doesn't get updated .. the task is created with all the data except WhoID.
In the debug maptoContact.get(t.WhatId) returns the Id of the wanted contact..

what is wrong here ?

trigger Task_BeforeInsertUpdateDelete on Task (before delete, before insert, before update)
{

trigger Task_BeforeInsertUpdateDelete on Task (before delete, before insert, before update)

{

 

    for(Task t : Trigger.new)

    {
           t.WhoId = maptoContact.get(t.WhatId);

    }
}

 

Anup JadhavAnup Jadhav
can you debug out t.whatid during before insert?
logontokartiklogontokartik
Hi,

More context on this one is helpful, also what is maptoContact? and how is it populated? Thank you.
MikeGillMikeGill
You have a map there. How have you populated the map?
Ronen SlifkinRonen Slifkin

Here is the full trigger

The line under the //======================================== are getting hit as expected and all is seems to be ok on the debug
but whoid isn't fet updated... (the last part of the trigger)



 trigger Task_BeforeInsertUpdateDelete on Task (before delete, before insert, before update)
{
    Set<ID> setId = new Set<ID>();
    Set<ID> setEligibilityIds = new set<ID>();           
    Map<ID,ID> mapEligibiltytoContact = new Map<ID,ID>(); 
  
    if(Trigger.isInsert || Trigger.isUpdate)
    {
       

        for(Task t : Trigger.new)
        {
            if(t.WhatId != null)
            {
                t.Reference__c = sys_Utilities.objectDetermination(t.WhatId);
               
                // In case the task is created from Contact and the Account is populated.
                if (t.Reference__c == 'Family' && t.WhoId != null)
                {
                   t.Reference__c = sys_Utilities.objectDetermination(t.WhoId);
                }
            }
            else  
            {
                if(t.WhoId != null)
                {
                    t.Reference__c = sys_Utilities.objectDetermination(t.WhoId);
                }
            }
            
            if(t.Reference__c == 'Family') t.Related_To_Family__c = t.WhatId;
            else if(t.Reference__c == 'Opportunity' || t.Reference__c == 'Eligibility' || t.Reference__c == 'Case' || t.Reference__c == 'PKD' || t.Reference__c == 'Questionnaire') setId.add(t.WhatId);
            else if(t.Reference__c == 'Family Member' || t.Reference__c == 'Lead') setId.add(t.WhoId);
            else if((t.Reference__c == null || t.Reference__c =='') && t.WhoId != null) // 'Last chance - reference not exists, going via 'Who' when it Contact'
            {
                string stringTmpID = t.WhoId;
                if (stringTmpID.startsWith('003')) setId.add(t.WhoId); 
            }
           
            if(Trigger.isInsert)
            {
             
             // collect the eligibilty ids into set when the whatid is eligibility and there is no contact in whoid
             // ========================================================================================
             if (t.Reference__c == 'Eligibility' && t.WhoId == null)
             {
                     setEligibilityIds.add(t.WhatId);
             }
             
             
        }
       
        for(Opportunity opp : [SELECT Id, AccountId, RecordType.Name from Opportunity where Id IN :setId])
        {
            mapIDtoAccID.put(opp.Id, opp.AccountId);
            mapOppIdtoRecordType.put(opp.Id, opp.RecordType.Name); 
        }  
       
      
        //=============================================================================
        for(Eligibility__c            elc : [Select Id, Contact__c           From Eligibility__c   where Id IN :setEligibilityIds]) mapEligibiltytoContact.put(elc.Id ,elc.Contact__c); 

        for(Eligibility__c            el  : [Select Id, Contact__r.AccountId From Eligibility__c   where Id IN :setId]) mapIDtoAccID.put(el.Id,  el.Contact__r.AccountId);
        for(Case                      c   : [Select Id, AccountId            From Case             where Id IN :setId]) mapIDtoAccID.put(c.Id,   c.AccountId);
        for(Contact                   con : [Select Id, AccountId            From Contact          where Id IN :setId]) mapIDtoAccID.put(con.Id, con.AccountId);
        for(Lead                      l   : [Select Id, ConvertedAccountId   From Lead             where Id IN :setId]) mapIDtoAccID.put(l.Id,   l.ConvertedAccountId);
        for(Personal_Klita_Details__c pkd : [Select Id, help_accountid__c    From Personal_Klita_Details__c where Id IN :setId]) mapIDtoAccID.put(pkd.Id,   pkd.help_accountid__c);
        for(Questionnaire__c          q   : [Select Id, Contact__r.AccountId From Questionnaire__c where Id IN :setId]) mapIDtoAccID.put(q.Id,   q.Contact__r.AccountId);
      

        for(Task t : Trigger.new)
        {
            if(t.WhatId != null && mapIDtoAccID.containsKey(t.WhatId))   
            {
                t.Related_To_Family__c = mapIDtoAccID.get(t.WhatId);
               
                if(t.Reference__c == 'Opportunity' && mapOppIdtoRecordType.containsKey(t.WhatId))
                {
                    t.Reference__c = mapOppIdtoRecordType.get(t.WhatId);
                }   
            }  
            else if(t.WhoId != null && mapIDtoAccID.containsKey(t.WhoId)) t.Related_To_Family__c = mapIDtoAccID.get(t.WhoId);
           
           
            // Set contact  in Tasks which their parent is Eligibilty
            if (Trigger.isInsert)
            {
                System.debug('Ronen map : ' + mapEligibiltytoContact);
                System.debug('Ronen: ' + t.WhatId);
                System.debug('Ronen: ' + mapEligibiltytoContact.get(t.WhatId));
             
               //=============================================================================  
                t.WhoId = mapEligibiltytoContact.get(t.WhatId); 
          
                System.debug('Ronen: t.WhoId' + t.WhoId);
           
            }              
        }
    }
}

logontokartiklogontokartik
Here is a similar problem. Maybe you can try his solution and see if it works?

https://developer.salesforce.com/forums/ForumsMain?id=906F000000092CIIAY
Ronen SlifkinRonen Slifkin
one more ....
1) WhoID is a contacts (not lead id) - in debug I see an valid Id of a contact
2) WhatID is an Id of an custom object (Eligibilty object) in the ttrigger
Ronen SlifkinRonen Slifkin
hello  logontokartik , I bumped into that post too (he managed to update thw whoid in after trigger)/
I wonders why can't it be accomplished in the befroe ? maybe Im missing something.
logontokartiklogontokartik
Hi Ronen, 

DId you try hardcoding some value to whoid in your trigger and see if its getting assigned? If not, can you check the profile to make sure you have edit access to the field? I dont see a reason why it wouldnt update in before trigger, maybe I am missing something here. Also, in your debug log, are you getting the value for mapEligibiltytoContact.get(t.WhatId)??
Ronen SlifkinRonen Slifkin

Hello all ,
logontokartik I Check the profile and I have access to the fields in task.
also tried hardcoding an Id into the whoid, no success. here is partiel debug

[a note: if I through the UI creating new task and choosing there contact, and saving , ub the new task I can see the contact name]

 

 

 

07:17:11.239 (239484019)|SYSTEM_METHOD_ENTRY|[140]|System.debug(ANY)
07:17:11.239 (239499339)|USER_DEBUG|[140]|DEBUG|Ronen map : {a0F11000000HTxCEAW=0031100000A5HozAAF}
07:17:11.239 (239507469)|SYSTEM_METHOD_EXIT|[140]|System.debug(ANY)
07:17:11.239 (239540022)|SYSTEM_METHOD_ENTRY|[141]|MAP<Id,Id>.get(Object)
07:17:11.239 (239614205)|SYSTEM_METHOD_EXIT|[141]|MAP<Id,Id>.get(Object)
07:17:11.239 (239639293)|SYSTEM_METHOD_ENTRY|[141]|String.valueOf(Object)
07:17:11.239 (239656608)|SYSTEM_METHOD_EXIT|[141]|String.valueOf(Object)
07:17:11.239 (239672858)|SYSTEM_METHOD_ENTRY|[141]|System.debug(ANY)
07:17:11.239 (239683924)|USER_DEBUG|[141]|DEBUG|Ronen: 0031100000A5HozAAF
07:17:11.239 (239691460)|SYSTEM_METHOD_EXIT|[141]|System.debug(ANY)
07:17:11.239 (239721287)|SYSTEM_METHOD_ENTRY|[143]|MAP<Id,Id>.get(Object)
07:17:11.239 (239796990)|SYSTEM_METHOD_EXIT|[143]|MAP<Id,Id>.get(Object)
07:17:11.239 (239947877)|SYSTEM_METHOD_ENTRY|[145]|String.valueOf(Object)
07:17:11.239 (239990407)|SYSTEM_METHOD_EXIT|[145]|String.valueOf(Object)
07:17:11.240 (240009419)|SYSTEM_METHOD_ENTRY|[145]|System.debug(ANY)
07:17:11.240 (240026250)|USER_DEBUG|[145]|DEBUG|Ronen: t.WhatId a0F11000000HTxCEAW
07:17:11.240 (240034618)|SYSTEM_METHOD_EXIT|[145]|System.debug(ANY)
07:17:11.240 (240087999)|SYSTEM_METHOD_ENTRY|[146]|String.valueOf(Object)
07:17:11.240 (240126739)|SYSTEM_METHOD_EXIT|[146]|String.valueOf(Object)
07:17:11.240 (240144047)|SYSTEM_METHOD_ENTRY|[146]|System.debug(ANY)
07:17:11.240 (240154924)|USER_DEBUG|[146]|DEBUG|Ronen: t.WhoId 0031100000A5HozAAF
07:17:11.240 (240162392)|SYSTEM_METHOD_EXIT|[146]|System.debug(ANY)
07:17:11.240 (240172325)|SYSTEM_METHOD_ENTRY|[121]|system.ListIterator.hasNext()
07:17:11.240 (240191758)|SYSTEM_METHOD_EXIT|[121]|system.ListIterator.hasNext()
07:17:11.427 (240244089)|CUMULATIVE_LIMIT_USAGE
07:17:11.427|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 8 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 10
  Number of Mobile Apex push calls: 0 out of 10

07:17:11.427|CUMULATIVE_LIMIT_USAGE_END

07:17:11.240 (240393671)|CODE_UNIT_FINISHED|Task_BeforeInsertUpdateDelete on Task trigger event BeforeInsert for [new]

 

 


 

           , I checke