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
customDevcustomDev 

Automatically populate fields on opportunity from Account

Hi,
 
We need to automatically populate certain fields on an Opportunity from the corresponding account when an Account is selected on an opportunity. This is a one time population only at the time of the opportunity creation. Am looking for the best way to do this.
 
The fields are available on the Account.
 
Thanks
Chirag MehtaChirag Mehta

For me tooo its the same required

There's a LookUp field of Contact in Custom Object

so when i select contact , i want the contact address to be populated into Custom Object 's Address field (one time migration)

 

I think only way possible is use of Integration Stuff

 

 

customDevcustomDev

figured it out, may not be the best way to do this but it works,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Opportunity: {!Opportunity_Name}</title>
<link href="/dCSS/Theme2/default/common.css" type="text/css" media="handheld,print,projection,screen,tty,tv" rel="stylesheet" >
<link href="/dCSS/Theme2/default/custom.css" type="text/css" media="handheld,print,projection,screen,tty,tv" rel="stylesheet" >
<link href="/css/assistive.css" type="text/css" media="aural,braille,embossed" rel="stylesheet" >
<link rel="shortcut icon" href="https://www.salesforce.com/favicon.ico">
<script src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>

<script language="javascript">
var accountId="{!Account_ID}";

//Initiate the script
function initApp()
{
sforceClient.init("{!API_Session_ID}","{!API_Partner_Server_URL_70}"); //login to salesfroce using session Id and Server Url
updateMyField();
}

function updateMyField()
{
try
{
getAccountMyField();//Query Account Information
}
catch(e){}
}

//This method retrieves the information from the account
function getAccountMyField()
{
sforceClient.Query("Select my_field from Account where Id ='" + accountId + "'",insertOpportunityMyField);
}

//This method sets the information on the opportunity
function insertOpportunityMyField(qrAccount)
{
var strMyField;
if(qrAccount!=null && qrAccount.records.length>0)
{
for(i=0;i<qrAccount.records.length;i++)
{
strMyField = qrAccount.records[i].get("my_field");
}
}
else{
strMyField="";
}

var opportunity = new Sforce.Dynabean("opportunity");
opportunity.set("Id","{!Opportunity_ID}");
opportunity.set("my_field",strMyField);

var sr = sforceClient.Update([opportunity])[0];
if (sr.success == true) {
alert("Update was successful...");
} else {
alert("Update failed: " + sr.errors[0].message);
}

top.location.replace("/{!Opportunity_ID}/e?retURL=/{!Opportunity_ID}&save=1");

}

</script>
</head>
<body onLoad="javascript:initApp()";>
</body>
</html>

michaelforcemichaelforce

A decent way to handle that is to create a custom link (or even better, an image hyperlink field made to look like a button) on the Account page.

Call it "Opportunity Quick-Create" or something like that, and simply go to the opportunity creation URL using merge fields to define the info you want to populate (e.g. [new opp url]&Opp_Industry__c={!Acc_Industry__c})

Then the users have two choices... create an opp from scratch... or create an opp with your quick link and have some of the work done for them.  No one can say no to that.

 

customDevcustomDev

Thanks Michael,

Part of the requirement was that the users have a single creation process. We have actually done this through external customization and looks like APEX can do that. We were trying to see if it could be done with minimal impact to users in terms of training/process.

 

michaelforcemichaelforce
Winter '07 will apparently allow you to override "new" buttons... so that any way you create a new object (e.g. from another object, from a quick link, or from the objects tab) they will all run your custom code.
 
The problem then is that you would have to somehow determine the correct Account to pull from.
customDevcustomDev
Yep, so in our case the trigger would be the selection of the account on the opportunity. That would be the pre-requisite and would also trigger the the population of the fields on the opportunity from the account fields.
 
The biggest issue that i am running into with all this is user permissions. Once the system has pre-populated the fields, the user should not be allowed to modify them. Ideally, i would like to do this without redirecting users to another layout. So the user clicks on a custom link to populate the field, the field get populated, however, is then read only to the user. Any ideas?
michaelforcemichaelforce
For that you could allow, based on profile, the user to edit the field(s)... but instead of showing the field(s) on the page layout... display a formula field which mirros it (them) instead.  Formula fields are read-only.
customDevcustomDev
The issue that I am running into with that is if a field is not visible to the user, then they can not write to it. i was hoping there was a possibility of Scontrol logging in from the SControl as an admin user, updating the opportunity and then transfering the control back to the original user.
michaelforcemichaelforce
Or perhaps it's possible to have the S-Control create a new record with the proper inherited info, and save.  Then change the Record Type which would allow for a different page layout.  Then have a return URL which is the edit screen for the newly created record.
 
Not sure if it's possible, but it's worth a shot.
customDevcustomDev
Yep, spoke to a couple of folks at Salesforce.com and currently that looks like the only option. Am looking forward to the winter release.