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
Krystal D.  CarterKrystal D. Carter 

Autopop Custom Task Field from Related To Custom object

I have a need to autopopulate a custom task field called "Signer" which would come from my custom "Agreement" object related to my task.  I'd need the logic to look something like this: 

If Task's "Related To ID" starts with "a0Z" (which denotes the custom Agreement object) 

THEN populate Task's Signer__c with the Related Agreement's Signer's name. (Signer on the Agreement is a lookup field, so I want the Name (String) from the record selected in the lookup ie: Agreement.Signer__r.Name__c (something like that.

lease help! Thanks, community!
Best Answer chosen by Krystal D. Carter
James LoghryJames Loghry
The primary issue with your requirement is that workflows, formulas, processes don't allow you to read easily from the "WhatId" or the Related To record.  You're probably looking at writing an Apex Trigger on the Task object, that looks for the WhatId, and queries for any Agreement records that match the WhatId.  From there you can populate the task record.

I've whipped up a quick and dirty example below, but haven't checked it for any errors
 
trigger TaskTrigger on Task(before insert){
    Set<Id> parentIds = new Set<Id>();
    for(Task t : Trigger.new){
        parentIds.add(t.WhatId);
    }

    Map<Id,Agreement__c> agreementMap = new Map<Id,Agreement__c>(
        [Select Signer__r.Name__c From Agreement__c]
    );

    for(Task t : Trigger.new){
        Agreement__c aggrement = agreementMap.get(t.WhatId);
        if(agreement != null){
            t.Signer__c = agreement.Signer__r.Name__c;
        }
    }
}

 

All Answers

JayantJayant
Since we can't traverse to parent record through Related To field, you may need a trigger to do this.

You may want to try it out first and if you need any help with the trigger let us know.
James LoghryJames Loghry
The primary issue with your requirement is that workflows, formulas, processes don't allow you to read easily from the "WhatId" or the Related To record.  You're probably looking at writing an Apex Trigger on the Task object, that looks for the WhatId, and queries for any Agreement records that match the WhatId.  From there you can populate the task record.

I've whipped up a quick and dirty example below, but haven't checked it for any errors
 
trigger TaskTrigger on Task(before insert){
    Set<Id> parentIds = new Set<Id>();
    for(Task t : Trigger.new){
        parentIds.add(t.WhatId);
    }

    Map<Id,Agreement__c> agreementMap = new Map<Id,Agreement__c>(
        [Select Signer__r.Name__c From Agreement__c]
    );

    for(Task t : Trigger.new){
        Agreement__c aggrement = agreementMap.get(t.WhatId);
        if(agreement != null){
            t.Signer__c = agreement.Signer__r.Name__c;
        }
    }
}

 
This was selected as the best answer
Krystal D.  CarterKrystal D. Carter
Thank you so much.  Yes I know I couldn't do it from a workflow, or formula, etc.  A trigger is the only way.  Let me try this out and see what I get.  Thanks!