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
NongNong 

How to get Account information from Task trigger?

Dear All,

 

Could you please help me to give some example APEX code to get value of Account custom field from Task trigger.

 

As i would like to set default value on Task custom field depending on selected Rating of Account object that this task is under.

 

Thank you very much in advance for your help.

 

Best Regards

Anong

NongNong

Dear All,

 

I have found the solution from other topic so i put the source code here  for other people reference in the future.

 

=================================================================

trigger AccountRating_TaskBudget on Task (before insert) {
 
            String strRating ='';
            Integer iBudget =0;                 
            Task tsk=Trigger.new[0];
            
                  if(tsk.whatid!= null || tsk.whatid!= '')
                  {                  
                        strRating=[select Rating from Account where id = :tsk.whatid].Rating ;
                        strRating=strRating.toUpperCase();                                                           

                        //set budget via Account rating
                        if (strRating == 'A1') {
                            iBudget =500;
                        } else  if (strRating == 'A2') {
                            iBudget =500;
                        } else  if (strRating == 'A3') {
                            iBudget =200;
                        } else  if (strRating == 'A4') {
                            iBudget =200;
                        } else  if (strRating == 'A5') {
                            iBudget =100;                                        
                        } else {
                            iBudget = 0;
                        }                 

                        tsk.Budget__c = iBudget;
                      
                  }//if     
}

====================================================================

 

Regards

Anong

kyle.tkyle.t

Hi, I think there is a problem in your if statement.  You should be using && not ||.

 

if(tsk.whatid!= null && tsk.whatid!= '') --> If tsk.whatid does not equal null AND does not equal the empty string

 

it is theoretically possible for the whatid to be equal to null but not the empty string and vice-versa.

 

You can also consolidate your code a little more and bulkify it. 

 

 

trigger AccountRating_TaskBudget on Task (before insert) {
        
	for (Task tsk : Trigger.new){			
	    //check to see if the task is associated to a record and if that record is an account
		if(tsk.whatid!= null && tsk.whatid!= '' && String.valueOf(tsk.whatid).startswith('001'))
		{   
			//get the account rating and set it to uppercase
			String strRating = tsk.Account__r.Rating.toUpperCase();			                                       

			//set budget via Account rating
			if (strRating == 'A1' || strRating == 'A2') {
				tsk.Budget__c =500;
			} else  if (strRating == 'A3' || strRating == 'A4') {
				tsk.Budget__c =200;		
			} else  if (strRating == 'A5') {
				tsk.Budget__c =100;                                        
			} else {
				tsk.Budget__c = 0;
			}                 
		}//if
	}		
}

 

 

 

NongNong

Hello kyle.t ,

 

Thank you very much for your suggestion.

After i tried with your suggestion i got the error ' Invalid foreign key relationship: Task.Account__r at'  , could you please help.

 

Best Regards

Anong

Pradeep_NavatarPradeep_Navatar

Below is the sample code to update the Account_Rating__C custom field in Task from the Rating__c custom field of selected Account :

 

            trigger TaskPopulateRating on Task (before insert)

            {

                        list<Account> acc;

                        Task T = Trigger.New[0];

                        String str = T.whoId;

 

                        if(str != null)

                        {

                                    str  = str.substring(0,3);

                        }

                        if(str == '001')

                        {

                                    acc = [select Rating__c From Account where id=:T.whoId]; 

                                    T.Account_Rating__C = acc.Rating__c;

                        }

            }

kyle.tkyle.t

Sorry about that, forgot you can use the dot notation with Tasks.  I created this in a dev environment that I have and it should work for you.  You need to gather up all of the accounts before you enter the for loop and make a map out of them.  once you have that, you can apply your logic.

 

 

trigger accountTask_Rating on Task (before insert) {
	List<Task> acctTaskList = new List<Task>();
	List<ID> acctIDList = new List<Id>();
	//Loop through the tasks in the trigger and get a list of all the ones that are attached to accounts
	//also create a list of IDs so we can query for the accounts
	for(Task t : trigger.new){
		System.debug('t.whatid is: '+t.whatid);
		if(t.whatid!= null && String.valueOf(t.whatid).startswith('001')){
			acctTaskList.add(t);
			acctIDList.add(t.whatid);
			System.debug('Added Account' + t.whatid);
		}		
	}
	
	//using the list of IDs created above, query for the account information
	List<Account> acctList = [select Id, Rating__c from Account where id IN :acctIDList];
	
	//create a map of the accounts returned in the query
	Map<Id,Account>acctMap = new Map<Id,Account>();
	for(Account a : acctList){
		acctMap.put(a.id, a);
	}
	
	//we now have what we need to process the tasks, 
	for (Task tsk : acctTaskList){			
		//get the account rating from the map and set it to uppercase
		String strRating = acctMap.get(tsk.whatId).Rating__c.toUpperCase();			                                       

		//set budget via Account rating
		if (strRating == 'A1' || strRating == 'A2') {
			tsk.Budget__c = 500;
		} else  if (strRating == 'A3' || strRating == 'A4') {
			tsk.Budget__c = 200;		
		} else  if (strRating == 'A5') {
			tsk.Budget__c = 100;                                        
		} else {
			tsk.Budget__c = 0;
		}                 	
	}			
}