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
Nerv123Nerv123 

S-Control help needed with Updating a custom field in Opportunity with a custom field in Accounts

Please help with your input....I have created a S-Control which will be hidden(height and width = 0) and put in System Information Section.
Accounts has a custom field called "Company_Number__c". This is an auto-number field
Opportunity has a custom field called "Account_Number__c". This is a text  field.
 
When a new opportunity is opened, either from Lead conversion process, pressing a button on accounts or from Creating a New Opportunity directly,
Account_Number__c should automatically inherit the value Company_Number__c from the related account on the opportunity.
 
My S-Control on Opportunity Page is attached below. Where am I going wrong? No value is being displayed in the Account_Number__c right now.
 
 
 

<script type="text/javascript" src="/soap/ajax/9.0/connection.js" > </script>

<script type="text/javascript" >

 var AccountDetails  = sforce.connection.query("select Company_Number__c from Account where Name = '{!Opportunity.AccountId}'");

 var AccountArray = AccountDetails.getArray("records");

var OpportunityRec =new sforce.SObject("Opportunity");

 OpportunityRec.Id="{!Opportunity.Id}";

 if(AccountDetails.size >0 {

 OpportunityRec.Account_Number__c = AccountArray[0].Company_Number__c;

 if(sforce.connection.update([OpportunityRec])[0].getBoolean("success"))

{

window.parent.location.href="{!URLFOR($Action.Opportunity.View, Opportunity.Id, null,true)}";

 }

else

{

alert("Problem in Updating the Company Number from Account to Opportunity"); }

}

</script>

 

Your help is appreciated!

WhyserWhyser
Actually, if you are willing to wait for the Summer '08 release, there will be a new feature enabled in Salesforce, called the Cross Objects Formulas
 
What it enables you to do is to display fields from related objects, so for instance, if you wanted to display your "Account.Account_Number__c" field in your opportunities, you would be able to create a formula field in the opportunity that will display that particular field from the account.
 
You can read more about it here:
 
 
 
 
ANOTHER thing you can do, is create a workflow that will update your Opportunity.Account_Number__c field.
 
1) Create a Workflow Rule against the Opportunity object that will run when the Opportunity.Account_Number__c equals null or blank
2) Create a Workflow Field Update against the Opportunity object and the field "Opportunity.Account_Number__c" that will update it with the "Account.Account_Number__c" value.
3) Associate the Workflow Rule you created in Step 1) with the Field Update that you created in Step 2)
 
What this will do is when the opportunity is created or editted, it will run the workflow rule against the opportunity to see if the Opportunity.Account_Number__c is blank. If this evaluates to true, then it will run the Field Update in which it will update the Opportunity.Account_Number__c with the Account.Account_Number__c value.
 
This will save you some coding time and any potential mistakes. Workflow rules are your friend. :smileywink:
Nerv123Nerv123

Thanks, that worked! Just curious though, any pointers on what is wrong with my coding?

 

Thanks again

WhyserWhyser

I think where you might be failing is your first SOQL statement

select Company_Number__c from Account where Name = '{!Opportunity.AccountId}'

I think the problem may be stemming from your condition where you are comparing the NAME of the company with the Account ID from the opportunity. You should probably get no results returned from that.