+ Start a Discussion
SandrineSandrine 

Prepopulate a lookup on the creation of account

Hello,

object: Account
  custfield__1 (Lookup to Obj1)

Obj1
  Name (Standard field: mandatory)

Usecase:
when account is created, the Name is autofilled or updated with value "XYZ" and make sure with a SOQL that the record is well present.

How could i implement it with Trigger ?

thanks you for suggestion.
 
Best Answer chosen by Sandrine
Vivian Charlie 1208Vivian Charlie 1208

Hi Sandrine,

 

Do you want the lookup to be populated after the Account is created or when the user clicks on the Create button?

 

1. If needed after the record is inserted you can use a before trigger to search for the record and populate the id

 

2. If needed on clicking the Create button, you will have to override the new (create) button with a VF page. Use the action attribute of apex:page to query XYZ record.

If a record is found, then redirect to the standard record creation page by url-hacking the parameters.

https://na1.salesforce.com/001/e?retURL=%2F001%2Fo&CF00N9000000F4tOt_lkid=012900000011ul8&CF00N9000000F4tOt=XYZ&ent=Account

Steps : 

  • https://na1.salesforce.com/001/e? : Record creation base url // replace instance else use (/001/e?)
  • retURL : (/001 i.e Account tab) where should the page be redirected after successful creation
  • CF00N9000000F4tOt_lkid : Id of the lookup field to populate // replace with your custom field id
  • 012900000011ul8 : Replace with record id of XYZ
  • CF00N9000000F4tOt : Name of XYZ record to be set here

Note :

1. In order to auto-populate any field that field must be on the page layout

2. Inorder to find the field id go to Setup-Customize-Account-Fieldname // in the url you will find the id

OR

2. Go to the objects creat/edit page, right click on field and select Inspect you will see such a snippet

<input id="CF00N9000000F4tOt" maxlength="255" name="CF00N9000000F4tOt" onchange="getElementByIdCS('CF00N9000000F4tOt_lkid').value='';getElementByIdCS('CF00N9000000F4tOt_mod').value='1';" size="20" tabindex="10" title="Requester" type="text">
 

Thanks

Vivian

All Answers

Vivian Charlie 1208Vivian Charlie 1208

Hi Sandrine,

 

Do you want the lookup to be populated after the Account is created or when the user clicks on the Create button?

 

1. If needed after the record is inserted you can use a before trigger to search for the record and populate the id

 

2. If needed on clicking the Create button, you will have to override the new (create) button with a VF page. Use the action attribute of apex:page to query XYZ record.

If a record is found, then redirect to the standard record creation page by url-hacking the parameters.

https://na1.salesforce.com/001/e?retURL=%2F001%2Fo&CF00N9000000F4tOt_lkid=012900000011ul8&CF00N9000000F4tOt=XYZ&ent=Account

Steps : 

  • https://na1.salesforce.com/001/e? : Record creation base url // replace instance else use (/001/e?)
  • retURL : (/001 i.e Account tab) where should the page be redirected after successful creation
  • CF00N9000000F4tOt_lkid : Id of the lookup field to populate // replace with your custom field id
  • 012900000011ul8 : Replace with record id of XYZ
  • CF00N9000000F4tOt : Name of XYZ record to be set here

Note :

1. In order to auto-populate any field that field must be on the page layout

2. Inorder to find the field id go to Setup-Customize-Account-Fieldname // in the url you will find the id

OR

2. Go to the objects creat/edit page, right click on field and select Inspect you will see such a snippet

<input id="CF00N9000000F4tOt" maxlength="255" name="CF00N9000000F4tOt" onchange="getElementByIdCS('CF00N9000000F4tOt_lkid').value='';getElementByIdCS('CF00N9000000F4tOt_mod').value='1';" size="20" tabindex="10" title="Requester" type="text">
 

Thanks

Vivian

This was selected as the best answer
Lokesh KumarLokesh Kumar
Hi sandrine,

As requirement is not genric and not clear but you can try below code.
 
trigger updateaccount on Account(before insert){
	if(Trigger.New.size() > 150)
	for(Account acc : Trigger.new){
	    Obj1 obj = new Obj1(Name='XYZ');
		insert obj;
		acc.custfield__1  = obj.id;
	}
}

Thanks
Lokesh