+ Start a Discussion

Q. Updating a field

Hi -


I'd like to use the S-Control below to update a field on the Contact screen.  Can anyone provide some guidance on how it needs to be modified.  Thank you.  




//create an sobject
var myObj = new sforce.SObject("Contact");

//set the fields, note this is a merge field giving the ID of the current record
myObj.Id = "{!Contact.Id}";
myObj.Test__c = "Hope This Works"';

//call api





Can you give some more background on what you are trying to do?  S-controls are usually fired from a button or custom link that you click from the Detail page to do something.   Also they are being phased out eventually by using Apex and/or visualforce pages.


There are plenty of ways to update a field on the Contact you are looking at - can you give the scenario?


My objective is (when a contact’s record is open) to loop through all the opportunities where the contact is listed in the contact role section and if the contact has an opportunity in the "Agreement Received" stage then update a field on the contact object with "Agreement Received".  The S Control below successfully does the query part.  Not set on using an S-Control to accomplish this, in fact would prefer to use Visual Force.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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" >


<script src="/soap/ajax/10.0/connection.js" type="text/javascript"></script>
<script type="text/javascript" src="/js/functions.js"></script>
function Init() {

// Developed March 2008
// by Ben Ingram
// beningram@hotmail.com
// May be re-used or altered so long as the credit remains :)
// This s-Control for salesforce.com provides
// visibility of all oportunities for which a
// contact is listed as an Opportunity Contact
// To Implement this s-Control:
// 1. Add a new s-Control of type HTML
// 2. Insert this code (all) into the HTML Body of the new Control
// 3. Name the control something appropriate such as 'Contact_Opportunity_Roles'
// 4. Edit the appropriate page layout for 'Contacts'
// 5. Create a 'new section' to contain this inline s-control
// I recommend immediately preceding the related list area
// 6. Drag the s-control from the list on the right into your new section
// 7. Set the properties of this s-control to set the height = 100
// 8. Enjoy :)


// Query the SOQL to return the related OpportunityContact records, along with the parent Opportunity details as required
var SearchString="ContactId From OpportunityContactRole Where (ContactId = '{!Contact.Id}') AND (Opportunity.StageName='Agreement Received') ";
var queryResults = sforce.connection.query(SearchString);
var queryRecords = queryResults.getArray('records');
var txtOutput = ""

if (queryRecords.length > 0) { //check that the update was successful
//create an sobject
var myObj = new sforce.SObject("Contact");

//set the fields, note this is a merge field giving the ID of the current record
myObj.Id = "{!Contact.Id}";
myObj.Test__c = "new status"';

//call api
sforce.connection.update([myObj]);//If Statement is true
} else { //otherwise unsuccessful
updateContact.Opportunity_in_Agreement_Rec_d__c=false; //If Statement is false








How about using a trigger to do this?  You could write a trigger on the Opportunity so that if the stage is changed to "Agreement Received" AND any related Contact's custom field doesn't say "Agreement Received", update that Contact's custom field.  That way anytime an Opportunity is updated, the related Contacts are also updated, rather than waiting until the Contact record is opened or edited.

Niki -


Thank you for the suggestion. 


Can you point me towards a well documented example of an Apex Trigger whose purpose is to detect when a field value changes in one object, then trigger a field in a second object to updated based on this change?


Thank you,