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
Michael AllenMichael Allen 

Attaching child records using ID held on parent

Hi all,

I'm very new to Apex, but am convinced this scenario is possible. Problem is I can't find any examples anywhere, which surprises me.

I have a text field on Account called 'Originating_Lead__c' containing the 18 char ID of a Lead.

On the Lead is a lookup to Account.

I want to insert the ID of the Account record into the Account lookup field on the Lead record that corresponds to the ID held in the Originating_Lead__c field on Account.

I'm know I can't do this declaratively as it's "going down the tree", but I'm surprised this issue hasn't crept up before on here - have trawled right through and haven't found anything. Does anybody know of any examples of similar triggers for similar problems? 

Have browsed and used this community extensively but never posted a question - never needed to!

Thanks in advance.
Michael
mjohnson-TICmjohnson-TIC
So when Originating_Lead__c is entered on Account, you want the Lead entered in this field to relate back to the Account? If that's the situation, try below Account trigger (note, the way I wrote it is not necessary - but will best avoid any other dml/trigger governor limits).
 
trigger AccountTrigger on Account (after insert, after update) {
	list<Lead> ll = new list<Lead>();
	for (Integer i = 0; i < trigger.new.size(); i++){
		if((trigger.isInsert && trigger.new[i].Originating_Lead__c != null) || (trigger.isUpdate && trigger.new[i].Originating_Lead__c != trigger.old[i].Originating_Lead__c )){
			Lead l = new Lead(Id=trigger.new[i].Originating_Lead__c);
			l.Account__c = trigger.new[i].Id;
			ll.add(l);
		}
	}
	if(ll != null){
		update ll;
	}
}



 
Michael AllenMichael Allen
Hi mjohnson,

Thanks so much for this. It works a treat.
This has opened up a huge door for me, as we have similar scenarios elsewhere that need addressing in a similar manner.

I may pop back in time if I have issues with them. 

Thanks again
Michael
Brianne WilsonBrianne Wilson
I have a similar issue here: https://developer.salesforce.com/forums?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=906F0000000ArheIAC

The only difference for me is where the lookup field is. If I compared it to your case, it would be that I had a lookup field on the Account to the Lead, and I'd want to find the Lead that matches that 18 character code and attach it TO the account (rather than the other way around). 

If anyone can please help my similar case I'd much appreciate it! 
Michael AllenMichael Allen
Hi all,

Could anybody lend a hand with a test class for this? I've made a start, but I'm working off the basic sample versions from the SF help articles. I'm not sure how to make it do the actual test, as the trigger updates a field on Lead from an ID held on Account. So how do I write a test to look at Lead? Does it need to insert 2x temporary records - a Lead and an Account?

As you can see from earlier, the field 'Originating_Lead__c' on Acct contains an 18 digit ID (populated on creation via URL). The trigger then finds this lead and updates the Account__c lookup with the ID of the Account from which the trigger fired.

Thanks
Michael

Brianne, I read your thread and saw you had success getting a solution. Congrats.