You need to sign in to do that
Don't have an account?
sfbpinna
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!
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
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 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?
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.
Thank you!!!!!
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:
that i use .... example
for(Attachment a: Trigger.new)
{
Id myId = a.parentid;
system.debug('1111'+myId);
if(myId.getSobjectType()==Schema.Opportunity.SObjectType){