+ Start a Discussion
sfbpinnasfbpinna 

Updating Lead or Contact based on a Task

Looking at writing a simple Apex trigger to update a lead or contact field based on a task being added.

 

Have written an after insert Task trigger.  Quick question...

 

What is the best way to determine if the WhatID references a lead or contact?  Can the ID field be treated as a string and what are the ID prefixes for the Lead and Contact table?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

Here is a snippet that uses Dynamic Apex to figure out what object is related to the Task using key prefix. The code stores a map of all the object key prefixes and object names so you can lookup the name by the prefix.  

 

 

trigger TaskTrigger on Task (after insert, after update) { Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Map<String,String> keyPrefixMap = new Map<String,String>{}; Set<String> keyPrefixSet = gd.keySet(); for(String sObj : keyPrefixSet){ Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe(); String tempName = r.getName(); String tempPrefix = r.getKeyPrefix(); System.debug('Processing Object['+tempName + '] with Prefix ['+ tempPrefix+']'); keyPrefixMap.put(tempPrefix,tempName); } for(Task t: Trigger.new){ if(t.WhatId!=null){ String tPrefix = t.WhatId; tPrefix = tPrefix.subString(0,3); System.debug('Task Id[' + t.id + '] is associated to Object of Type: ' + keyPrefixMap.get(tPrefix)); } } }

 

 

 

All Answers

aalbertaalbert

Here is a snippet that uses Dynamic Apex to figure out what object is related to the Task using key prefix. The code stores a map of all the object key prefixes and object names so you can lookup the name by the prefix.  

 

 

trigger TaskTrigger on Task (after insert, after update) { Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Map<String,String> keyPrefixMap = new Map<String,String>{}; Set<String> keyPrefixSet = gd.keySet(); for(String sObj : keyPrefixSet){ Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe(); String tempName = r.getName(); String tempPrefix = r.getKeyPrefix(); System.debug('Processing Object['+tempName + '] with Prefix ['+ tempPrefix+']'); keyPrefixMap.put(tempPrefix,tempName); } for(Task t: Trigger.new){ if(t.WhatId!=null){ String tPrefix = t.WhatId; tPrefix = tPrefix.subString(0,3); System.debug('Task Id[' + t.id + '] is associated to Object of Type: ' + keyPrefixMap.get(tPrefix)); } } }

 

 

 

This was selected as the best answer
sfbpinnasfbpinna
Awesome.  Thank you.
Eric_SantiagoEric_Santiago

This is good, but how can we take it one step further to instantiate the whatId object in order to perform DML functions on it. Say if I determine that the related object is a case, I want to get the Subject.But, if its an some other object I want to get some other field. Essentially, you can hardcode the fields since you don't know what object type to expect.

 

From the describe document, I think we need a statement like;

 

 

SObject whatObject = Database.query('select id from ' & keyPrefixMap.get(t.whatId) & ' where id = ' & t.whatId);

 I also saw an example of getting a map of fields associated with an object, but it looks like the object is explicitly named.

 

 

Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();

 Any ideas on how to put all this together to get a value of a field on the related object?

 

 

 

 

Walter@AdicioWalter@Adicio

Hello,

 

i read here...

http://community.salesforce.com/t5/Apex-Code-Development/Finding-Objecttype-for-a-Record-ID/m-p/63826#M2840

 

and here...

http://community.salesforce.com/t5/Apex-Code-Development/Updating-Lead-or-Contact-based-on-a-Task/m-p/114550

 

about putting the object type and object pre-fixes into a map.

 

which is giving me something like this for example:

Object[Contact] with Prefix [003]

 

Can you help me understand if I have an ID value, how do i use the map to avoid an action if the ID is for a certain object type? For example i want to avoid an action if the ID belongs to a record of 'Contact' object type.

Im not seeing how to make that connection yet.

Chris987654321Chris987654321

Thank you!!!!!

Chris987654321Chris987654321

Here's what I did to have something only happen if the what id is of type Student_Enrollment__c. This is a custom object of ours:

 

if(t.WhatId!=null){
                    		//using the prefix, this will set objectType to the object name of the what id
                    		string tPrefix = t.WhatId;
        					tPrefix = tPrefix.subString(0,3);
        					objectType = keyPrefixMap.get(tPrefix);
        					System.debug('Task Id[' + t.id + '] is associated to Object of Type: ' + objectType);
                    	}
                    	
                    	//only update the what id if there is not already one set, or if the one that is set is a student enrollment object
                    	if (t.WhatId == null || 
                    		(t.WhatId != null && objectType == 'Student_Enrollment__c')) { 
                            t.WhatId = enrollment_to_student.get(t.WhoId);
                            System.debug('WhatId was changed to ' + t.WhatId);
                        }
 

 

sushant sussushant sus
i know this question is solved but i what to put my thought . their is one simple way in which we dont have to write so my line
that i use .... example
for(Attachment a: Trigger.new)
    {
      Id myId = a.parentid;  
         system.debug('1111'+myId);
      if(myId.getSobjectType()==Schema.Opportunity.SObjectType){