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
Deloitte IT Admin 2Deloitte IT Admin 2 

Trigger to populate a Lookup when creating a record

Hi All,

I've created a trigger that pupulate the Account lookup field on my custom object.
As shown in figure, when I create a new custom object, my trigger do not write the Account:
User-added image

But when i press 'Save', the Account field is populated:

User-added image

I would like to pupulate the Account also in the first picture, when I create a new object.

Here's my code:
 
trigger afterPlanningTrigger on Pipeline__c (after insert) {

    for(Pipeline__c p: Trigger.new)
    {
        List<Pipeline__c> pi = [select id, Account__c, Opportunity__r.Account.id,Opportunity__r.Owner.id from Pipeline__c where id=:p.id];
        
        pi[0].Account__c = pi[0].Opportunity__r.Account.id;
        pi[0].Opportunity_Owner__c = pi[0].Opportunity__r.Owner.id;
       
        update pi[0];
    }
}

 
Best Answer chosen by Deloitte IT Admin 2
William TranWilliam Tran
You can do that via URL hacking (not triggers).

As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.

Thanks