Fill lookup on task with ID from 'Related to' Object in Trigger

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












When a new Task is inserted(before insert) I want to get a lookup field from the object in it's 'whatId'(related to) and assign that to a lookup on the task. This is what I have, I'm pretty sure there is no danger of the 'whatId' being blank so don't worry about that:



 List<Id> pmgIds = new List<Id>();
for(SObject temprecord : Trigger.New)
pmgIds.add(temprecord.get('WhatId'));


for(Practice_Management_Guide__c pmg : [SELECT Id,Name,Client__c
FROM Practice_Management_Guide
WHERE Id IN :pmgIds])
for(SObject temprecord:Trigger.New)
if(temprecord.get('WhatId') == pmg.Id)
temprecord.Client__c = pmg.Client__c;






I'm curious is there a better way to do this ? Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?










share|improve this question





















  • WhatId can be null, but it would be ignored by the query anyways (it's only interested in values that are actually possible). You're right in presuming that you don't need to check for null in this case.
    – sfdcfox
    3 hours ago
















up vote
1
down vote

favorite












When a new Task is inserted(before insert) I want to get a lookup field from the object in it's 'whatId'(related to) and assign that to a lookup on the task. This is what I have, I'm pretty sure there is no danger of the 'whatId' being blank so don't worry about that:



 List<Id> pmgIds = new List<Id>();
for(SObject temprecord : Trigger.New)
pmgIds.add(temprecord.get('WhatId'));


for(Practice_Management_Guide__c pmg : [SELECT Id,Name,Client__c
FROM Practice_Management_Guide
WHERE Id IN :pmgIds])
for(SObject temprecord:Trigger.New)
if(temprecord.get('WhatId') == pmg.Id)
temprecord.Client__c = pmg.Client__c;






I'm curious is there a better way to do this ? Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?










share|improve this question





















  • WhatId can be null, but it would be ignored by the query anyways (it's only interested in values that are actually possible). You're right in presuming that you don't need to check for null in this case.
    – sfdcfox
    3 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











When a new Task is inserted(before insert) I want to get a lookup field from the object in it's 'whatId'(related to) and assign that to a lookup on the task. This is what I have, I'm pretty sure there is no danger of the 'whatId' being blank so don't worry about that:



 List<Id> pmgIds = new List<Id>();
for(SObject temprecord : Trigger.New)
pmgIds.add(temprecord.get('WhatId'));


for(Practice_Management_Guide__c pmg : [SELECT Id,Name,Client__c
FROM Practice_Management_Guide
WHERE Id IN :pmgIds])
for(SObject temprecord:Trigger.New)
if(temprecord.get('WhatId') == pmg.Id)
temprecord.Client__c = pmg.Client__c;






I'm curious is there a better way to do this ? Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?










share|improve this question













When a new Task is inserted(before insert) I want to get a lookup field from the object in it's 'whatId'(related to) and assign that to a lookup on the task. This is what I have, I'm pretty sure there is no danger of the 'whatId' being blank so don't worry about that:



 List<Id> pmgIds = new List<Id>();
for(SObject temprecord : Trigger.New)
pmgIds.add(temprecord.get('WhatId'));


for(Practice_Management_Guide__c pmg : [SELECT Id,Name,Client__c
FROM Practice_Management_Guide
WHERE Id IN :pmgIds])
for(SObject temprecord:Trigger.New)
if(temprecord.get('WhatId') == pmg.Id)
temprecord.Client__c = pmg.Client__c;






I'm curious is there a better way to do this ? Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?







apex trigger






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









SallyRothroat

33812




33812











  • WhatId can be null, but it would be ignored by the query anyways (it's only interested in values that are actually possible). You're right in presuming that you don't need to check for null in this case.
    – sfdcfox
    3 hours ago
















  • WhatId can be null, but it would be ignored by the query anyways (it's only interested in values that are actually possible). You're right in presuming that you don't need to check for null in this case.
    – sfdcfox
    3 hours ago















WhatId can be null, but it would be ignored by the query anyways (it's only interested in values that are actually possible). You're right in presuming that you don't need to check for null in this case.
– sfdcfox
3 hours ago




WhatId can be null, but it would be ignored by the query anyways (it's only interested in values that are actually possible). You're right in presuming that you don't need to check for null in this case.
– sfdcfox
3 hours ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted











Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?




Yes, you're entirely right. The only exceptions are a handful of core fields (Type and Name, off the top of my head) that you can access directly by querying through the What relationship.



As with any cross-relationship fields, you do have to query to obtain data from another object within your trigger. The trick with WhoId and WhatId (and OwnerId) is that the lookups are polymorphic, so you cannot query from the trigger through the relationship. Instead, you have to accumulate the Ids as you do here and write a separate query against a specific parent object.




I'm curious is there a better way to do this ?




There are some changes you need to make in your code. In particular, this double-loop structure is one of the least efficient ways to search.



Instead, do



Map<Id, Practice_Management_Guide__c> pmgMap = new Map<Id, Practice_Management_Guide__>(
[SELECT Id,Name,Client__c FROM Practice_Management_Guide WHERE Id IN :pmgIds]
);


Then, when you loop back over Trigger.new, you can just do pmgMap.get(thisTask.WhatId).



You don't need to treat Trigger.new as untyped sObjects. You can assign it to a list of typed objects (List<Task>) and refer to fields directly on your Tasks rather than using get().






share|improve this answer




















  • That's great thank you
    – SallyRothroat
    3 hours ago










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238802%2ffill-lookup-on-task-with-id-from-related-to-object-in-trigger%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted











Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?




Yes, you're entirely right. The only exceptions are a handful of core fields (Type and Name, off the top of my head) that you can access directly by querying through the What relationship.



As with any cross-relationship fields, you do have to query to obtain data from another object within your trigger. The trick with WhoId and WhatId (and OwnerId) is that the lookups are polymorphic, so you cannot query from the trigger through the relationship. Instead, you have to accumulate the Ids as you do here and write a separate query against a specific parent object.




I'm curious is there a better way to do this ?




There are some changes you need to make in your code. In particular, this double-loop structure is one of the least efficient ways to search.



Instead, do



Map<Id, Practice_Management_Guide__c> pmgMap = new Map<Id, Practice_Management_Guide__>(
[SELECT Id,Name,Client__c FROM Practice_Management_Guide WHERE Id IN :pmgIds]
);


Then, when you loop back over Trigger.new, you can just do pmgMap.get(thisTask.WhatId).



You don't need to treat Trigger.new as untyped sObjects. You can assign it to a list of typed objects (List<Task>) and refer to fields directly on your Tasks rather than using get().






share|improve this answer




















  • That's great thank you
    – SallyRothroat
    3 hours ago














up vote
3
down vote



accepted











Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?




Yes, you're entirely right. The only exceptions are a handful of core fields (Type and Name, off the top of my head) that you can access directly by querying through the What relationship.



As with any cross-relationship fields, you do have to query to obtain data from another object within your trigger. The trick with WhoId and WhatId (and OwnerId) is that the lookups are polymorphic, so you cannot query from the trigger through the relationship. Instead, you have to accumulate the Ids as you do here and write a separate query against a specific parent object.




I'm curious is there a better way to do this ?




There are some changes you need to make in your code. In particular, this double-loop structure is one of the least efficient ways to search.



Instead, do



Map<Id, Practice_Management_Guide__c> pmgMap = new Map<Id, Practice_Management_Guide__>(
[SELECT Id,Name,Client__c FROM Practice_Management_Guide WHERE Id IN :pmgIds]
);


Then, when you loop back over Trigger.new, you can just do pmgMap.get(thisTask.WhatId).



You don't need to treat Trigger.new as untyped sObjects. You can assign it to a list of typed objects (List<Task>) and refer to fields directly on your Tasks rather than using get().






share|improve this answer




















  • That's great thank you
    – SallyRothroat
    3 hours ago












up vote
3
down vote



accepted







up vote
3
down vote



accepted







Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?




Yes, you're entirely right. The only exceptions are a handful of core fields (Type and Name, off the top of my head) that you can access directly by querying through the What relationship.



As with any cross-relationship fields, you do have to query to obtain data from another object within your trigger. The trick with WhoId and WhatId (and OwnerId) is that the lookups are polymorphic, so you cannot query from the trigger through the relationship. Instead, you have to accumulate the Ids as you do here and write a separate query against a specific parent object.




I'm curious is there a better way to do this ?




There are some changes you need to make in your code. In particular, this double-loop structure is one of the least efficient ways to search.



Instead, do



Map<Id, Practice_Management_Guide__c> pmgMap = new Map<Id, Practice_Management_Guide__>(
[SELECT Id,Name,Client__c FROM Practice_Management_Guide WHERE Id IN :pmgIds]
);


Then, when you loop back over Trigger.new, you can just do pmgMap.get(thisTask.WhatId).



You don't need to treat Trigger.new as untyped sObjects. You can assign it to a list of typed objects (List<Task>) and refer to fields directly on your Tasks rather than using get().






share|improve this answer













Am I right in saying that you have to query to get other fields from an object related by 'WhatId' ?




Yes, you're entirely right. The only exceptions are a handful of core fields (Type and Name, off the top of my head) that you can access directly by querying through the What relationship.



As with any cross-relationship fields, you do have to query to obtain data from another object within your trigger. The trick with WhoId and WhatId (and OwnerId) is that the lookups are polymorphic, so you cannot query from the trigger through the relationship. Instead, you have to accumulate the Ids as you do here and write a separate query against a specific parent object.




I'm curious is there a better way to do this ?




There are some changes you need to make in your code. In particular, this double-loop structure is one of the least efficient ways to search.



Instead, do



Map<Id, Practice_Management_Guide__c> pmgMap = new Map<Id, Practice_Management_Guide__>(
[SELECT Id,Name,Client__c FROM Practice_Management_Guide WHERE Id IN :pmgIds]
);


Then, when you loop back over Trigger.new, you can just do pmgMap.get(thisTask.WhatId).



You don't need to treat Trigger.new as untyped sObjects. You can assign it to a list of typed objects (List<Task>) and refer to fields directly on your Tasks rather than using get().







share|improve this answer












share|improve this answer



share|improve this answer










answered 3 hours ago









David Reed

24.3k41642




24.3k41642











  • That's great thank you
    – SallyRothroat
    3 hours ago
















  • That's great thank you
    – SallyRothroat
    3 hours ago















That's great thank you
– SallyRothroat
3 hours ago




That's great thank you
– SallyRothroat
3 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238802%2ffill-lookup-on-task-with-id-from-related-to-object-in-trigger%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke