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
bmartinez76bmartinez76 

S-Control for Opportunity Object

Hello everyone.  I am new to these boards and to S-Controls.  I am not a programmer per say,but I have been assigned to role of admin for my company.  I have so far done a great job setting up the objects and creating simple formulas and validation rules.  I am now trying to create a process in the opportunity tab that will fetch the renewal billing month from the account object and place it in the opportunity renewal billing month field. So when entering an opportunity, the person would look up the company and then once selected the S-Control would search the account object for that company and retrieve the renewal billing information and paste it on the opportunity.  I was told that an S-Control can achieve this.  If anyone can lead me in the right direction it would be greatly appreciated.  Thank you in advance.
sfdcfoxsfdcfox
Probably, you'd want a button to do that (Setup | App Setup | Customize | Opportunities | Buttons and Links). That said, your code would look something like what follows.

Code:
{!RequireScript("/soap/ajax/9.0/connection.js")}
function updateOpportunity()
{ if("{!Opportunity.AccountId}" == "") // They didn't select an account!
    return
  // Get the account billing information.
  Account = sforce.connection.retrieve("BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry","Account",["{!Opportunity.AccountId}"])
  // Create an object to represent the opportunity.
  Opportunity = new sforce.SObject("Opportunity"]
  // Populate the ID and the fields we're updating.
  Opportunity["Id"] = "{!Opportunity.Id}"
  Opportunity["Billing_Street__c"] = Account["BillingStreet"]
  Opportunity["Billing_City__c"] = Account["BillingCity"]
  Opportunity["Billing_State__c"] = Account["BillingState"]
  Opportunity["Billing_PostalCode__c"] = Account["BillingPostalCode"]
  Opportunity["Billing_Country__c"] = Account["BillingCountry"]
  // Try to update the opportunity.
  sforce.connection.update([Opportunity])
  // Refresh the page.
  window.top.location.href=window.top.location.href
}

try // As in, try to update the opportunity.
{ updateOpportunity()
} catch(e) // Catch any errors.
{ // Show the error to the user.
  alert("Error: "+(e.faultCode?e.faultCode:e.message?e.message:e)+
  "\n\nPlease contact your administrator in regards to this error.")
}
The behavior for this button should be "Execute JavaScript" so that Salesforce knows it should run your code (instead of sending the user to a new page or something). Now, all you need to do is add the button to your page layout, and you're ready to go. I did the fields from memory, so you might need to check the field names.

~ sfdcfox ~
bmartinez76bmartinez76
Thank you so much, sfdcfox!  I don't think I would have been able to create this on my own.  I will study the coding here to see if I can get started on creating my own S-controls for future needs. Thanks again!
bmartinez76bmartinez76

I read the coding here before attempting to create the button so I could better understand what I was doing.  Since I really only wanted to retrieve the renewal month field from the account object I removed the other fields (Billing City,State, Etc) you initially added to the coding. I created this button in the opportunity object and set it up as a detailed button to add to my admin page layout so no one but me would see it. I created an opp and then saved it and tried my custom button but got the following error below:

"A problem with the OnClick Javascript for this button or link was encountered: Expected ‘(‘"

Is there anyway to incorporate this button when you click on new opportunity to replace the save or save & new button so that the users will be forced to retrieve this data before saving? Or will the users be forced to click on the custom button after creating the opportunity to populate the data?

Thank you for your help in advance as it is very much appreciated.

Coding used in button:

{!RequireScript("/soap/ajax/9.0/connection.js")}
function updateOpportunity()
{ if("{!Opportunity.AccountId}" == "") // They didn't select an account!
return
// Get the account billing information.
Account = sforce.connection.retrieve("Renewal Month","Account",["{!Opportunity.AccountId}"])
// Create an object to represent the opportunity.
Opportunity = new sforce.SObject("?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Opportunity"]
// Populate the ID and the fields we're updating.
Opportunity["Id"] = "{!Opportunity.Id}"
Opportunity["BRenewalMonthB__c"] = Account["Renewal Month"]
// Try to update the opportunity.
sforce.connection.update([Opportunity])
// Refresh the page.
window.top.location.href=window.top.location.href
}

try // As in, try to update the opportunity.
{ updateOpportunity()
} catch(e) // Catch any errors.
{ // Show the error to the user.
alert("Error: "+(e.faultCode?e.faultCode:e.message?e.message:e)+
"\n\nPlease contact your administrator in regards to this error.")
}