• Samwisematrix
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 10
    Replies

I have my code completed, but when I try to upload it to production I am getting an error:   I am very new to code so any help would be greatly appriciated.

 

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LastActDate3: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Trigger.LastActDate3: line 55, column 1: []

 

 

 

Here is my trigger code

 

 

trigger LastActDate3 on Task (after insert, after update) {

Set <ID> OptyIDs = new Set <ID> ();

Set<Id> Ready4Update = new Set<Id>();

List<Opportunity> OppList = new List<Opportunity>();

for (Task t: Trigger.new){

OptyIDs.add(t.WhatID);

}

Map<ID, Opportunity> OptyMap = new Map<ID, Opportunity> ([Select ID,Last_Activity__c from Opportunity where ID in :OptyIDs]);

for (Task t: Trigger.new){

if (t.WhatID != NULL){

 

Opportunity OptyRec = new Opportunity(); if(OptyMap.size()>0 && OptyMap.containsKey(t.WhatId)){ OptyRec = OptyMap.get(t.WhatID); If (t.subject <> null && t.subject.contains('Act-On Email') == false){ Optyrec.Last_Activity__c = t.ActivityDate; } if(!Ready4Update.contains(OptyRec.id)){ OppList.add(OptyRec); Ready4Update.add(OptyRec.id); } }

If (((!t.subject.contains('Act-On Email')) )){

 

Optyrec.Last_Activity__c = t.ActivityDate;

 

}

if(!Ready4Update.contains(OptyRec.id)){

 

   OppList.add(OptyRec);   

   Ready4Update.add(OptyRec.id);

 

}

 

}

}

if(OppList.size() > 0){

update oppList;

}

}

Below is my Code.   I am getting an error that I am attepting to dereference a null object.  I am not sure how to fix it.   I am very new to writtng triggers and any help would be very appriciated. 

 

 

 

trigger LastActDate3 on Task (after insert, after update) {

Set <ID> OptyIDs = new Set <ID> ();

Set<Id> Ready4Update = new Set<Id>();

List<Opportunity> OppList = new List<Opportunity>();

for (Task t: Trigger.new){

OptyIDs.add(t.WhatID);

}

Map<ID, Opportunity> OptyMap = new Map<ID, Opportunity> ([Select ID,Last_Activity__c from Opportunity where ID in :OptyIDs]);

for (Task t: Trigger.new){

if (t.WhatID != NULL){

 

Opportunity OptyRec = OptyMap.get(t.WhatID);

If (((!t.subject.contains('Act-On Email')) )){

 

Optyrec.Last_Activity__c = t.ActivityDate;

 

}

if(!Ready4Update.contains(OptyRec.id)){

 

   OppList.add(OptyRec);   

   Ready4Update.add(OptyRec.id);

 

}

 

}

}

if(OppList.size() > 0){

update oppList;

}

}

   
 

I have a trigger that works very well, but I cannot come up with a class that covers more than 75%

 

Trigger UniqueTask on Task(before insert, before update){
    
    //The map... key:Id, value: list of tasks
    Map<Id, List<Task>> whatIdsMap = new Map<Id, List<Task>>();
    //For each task being inserted, updated
    for(Task t : trigger.new){
      
        if(t.whatId != null){
           
        
            if(!whatIdsMap.containsKey(t.WhatId)){//If not found
                //Create temporary list for tasks
                List<Task> temp = new List<Task>();
                //Add current task to temp list
                temp.add(t);
              
                whatIdsMap.put(t.WhatId, temp);
            }else{//If found
                //Add current task to existing list of tasks under this WhoId
                whatIdsMap.get(t.WhatId).add(t);
            }//End if else
        }//End if
    }//End for
    
  
    for(Account Acc : [Select Id,Account_Owner_Role__c, Days_with_no_Activity__c from Account where Id in :whatIdsMap.keySet()]){
        //Get the tasks for this account and iterate through them
        //Update field value
        if(acc.Days_with_no_Activity__c >120){
        for(Task t :whatIdsMap.get(acc.Id)){
            If(t.Task_Owner_Role__c==acc.Account_Owner_Role__c&&t.Status=='Completed'){
            t.Unique_Activity__c = True;
        }//End for
    }
    }
    }//End for
}//End trigger

I am still pretty new to triggers and I need some help.

 

I need a way to update a custom checkbox on the task record when a custom formula number field on the related account record is more than 120.  The use case is that the formula number field "Days_with_no_Activity__c"  is how many days since the last activiity human activity not mass emails, and i want to update the task record prior to inserting it with a check box Unique_Activity__c.

 

Any help would be greatly appriciated.  I have been stuggling with this for awhile.

What I am trying to do is update a custom date field.  I want to put the date of the last activity that does not contain "Act-On Email" into the field "Last_Activity_by_Account_Owner__c".

 

The trigger works great until I do a mass update to trigger the trigger on previous tasks, but then I run into System.LimitException: Too many SOQL queries: 101.   I believe it is because I have a query inside the for loop, but I am new to Triggers and I dont know how to fix it.

 

My code:

 

trigger LastActDate2 on Task (after insert, after update) {
    
    
    //To do - If the subject of a completed task does not contain "Act-On Email", put the date of the completed task in the
    //"Last_Activity_by_Account_Owner__c" field on the account object

//Create a set of related account ID's
Set <ID> acctIDs = new Set <ID> ();


//For every task, add it's related to account ID to the set
 for (Task t: Trigger.new){
    if (t.accountID != NULL){
      acctIDs.add(t.accountID);
//Create a map to match the task related to ID's with their corresponding account ID's
      Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID,Account_Owner_Role__c, Last_Activity_by_Account_Owner__c from Account where ID in :acctIDs]);
//Create the account object
      Account acctRec = acctMap.get(t.accountID);

//If the account ID isn't null, the subject line contains "Act-On Email", the account record owner's role matches the task record owner's role, and the task has been marked as completed
//Check to see if the Last_Activity_by_Account_Owner__c field is current compared with the latest completed activity    
    If (((!t.subject.contains('Act-On Email'))&&acctMap.get(t.accountID).Account_Owner_Role__c == t.Task_Owner_Role__c && (t.Status == 'Completed')) &&
    (acctMap.get(t.accountID).Last_Activity_by_Account_Owner__c  < t.ActivityDate || acctMap.get(t.accountID).Last_Activity_by_Account_Owner__c  ==null )){
//Update the Last_Activity_by_Account_Owner__c field on the account object with the task's end date  
          acctrec.Last_Activity_by_Account_Owner__c  = t.ActivityDate;
    
      }
    update acctRec;
  }
  }
  }

Below is my Code.   I am getting an error that I am attepting to dereference a null object.  I am not sure how to fix it.   I am very new to writtng triggers and any help would be very appriciated. 

 

 

 

trigger LastActDate3 on Task (after insert, after update) {

Set <ID> OptyIDs = new Set <ID> ();

Set<Id> Ready4Update = new Set<Id>();

List<Opportunity> OppList = new List<Opportunity>();

for (Task t: Trigger.new){

OptyIDs.add(t.WhatID);

}

Map<ID, Opportunity> OptyMap = new Map<ID, Opportunity> ([Select ID,Last_Activity__c from Opportunity where ID in :OptyIDs]);

for (Task t: Trigger.new){

if (t.WhatID != NULL){

 

Opportunity OptyRec = OptyMap.get(t.WhatID);

If (((!t.subject.contains('Act-On Email')) )){

 

Optyrec.Last_Activity__c = t.ActivityDate;

 

}

if(!Ready4Update.contains(OptyRec.id)){

 

   OppList.add(OptyRec);   

   Ready4Update.add(OptyRec.id);

 

}

 

}

}

if(OppList.size() > 0){

update oppList;

}

}

   
 

I have a trigger that works very well, but I cannot come up with a class that covers more than 75%

 

Trigger UniqueTask on Task(before insert, before update){
    
    //The map... key:Id, value: list of tasks
    Map<Id, List<Task>> whatIdsMap = new Map<Id, List<Task>>();
    //For each task being inserted, updated
    for(Task t : trigger.new){
      
        if(t.whatId != null){
           
        
            if(!whatIdsMap.containsKey(t.WhatId)){//If not found
                //Create temporary list for tasks
                List<Task> temp = new List<Task>();
                //Add current task to temp list
                temp.add(t);
              
                whatIdsMap.put(t.WhatId, temp);
            }else{//If found
                //Add current task to existing list of tasks under this WhoId
                whatIdsMap.get(t.WhatId).add(t);
            }//End if else
        }//End if
    }//End for
    
  
    for(Account Acc : [Select Id,Account_Owner_Role__c, Days_with_no_Activity__c from Account where Id in :whatIdsMap.keySet()]){
        //Get the tasks for this account and iterate through them
        //Update field value
        if(acc.Days_with_no_Activity__c >120){
        for(Task t :whatIdsMap.get(acc.Id)){
            If(t.Task_Owner_Role__c==acc.Account_Owner_Role__c&&t.Status=='Completed'){
            t.Unique_Activity__c = True;
        }//End for
    }
    }
    }//End for
}//End trigger

What I am trying to do is update a custom date field.  I want to put the date of the last activity that does not contain "Act-On Email" into the field "Last_Activity_by_Account_Owner__c".

 

The trigger works great until I do a mass update to trigger the trigger on previous tasks, but then I run into System.LimitException: Too many SOQL queries: 101.   I believe it is because I have a query inside the for loop, but I am new to Triggers and I dont know how to fix it.

 

My code:

 

trigger LastActDate2 on Task (after insert, after update) {
    
    
    //To do - If the subject of a completed task does not contain "Act-On Email", put the date of the completed task in the
    //"Last_Activity_by_Account_Owner__c" field on the account object

//Create a set of related account ID's
Set <ID> acctIDs = new Set <ID> ();


//For every task, add it's related to account ID to the set
 for (Task t: Trigger.new){
    if (t.accountID != NULL){
      acctIDs.add(t.accountID);
//Create a map to match the task related to ID's with their corresponding account ID's
      Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID,Account_Owner_Role__c, Last_Activity_by_Account_Owner__c from Account where ID in :acctIDs]);
//Create the account object
      Account acctRec = acctMap.get(t.accountID);

//If the account ID isn't null, the subject line contains "Act-On Email", the account record owner's role matches the task record owner's role, and the task has been marked as completed
//Check to see if the Last_Activity_by_Account_Owner__c field is current compared with the latest completed activity    
    If (((!t.subject.contains('Act-On Email'))&&acctMap.get(t.accountID).Account_Owner_Role__c == t.Task_Owner_Role__c && (t.Status == 'Completed')) &&
    (acctMap.get(t.accountID).Last_Activity_by_Account_Owner__c  < t.ActivityDate || acctMap.get(t.accountID).Last_Activity_by_Account_Owner__c  ==null )){
//Update the Last_Activity_by_Account_Owner__c field on the account object with the task's end date  
          acctrec.Last_Activity_by_Account_Owner__c  = t.ActivityDate;
    
      }
    update acctRec;
  }
  }
  }