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
LukdanLukdan 

S-control in Opportunity Page Looping

I'm totally new to scontrols and java. I'm trying to insert this code right into the opportunity so it runs on page load and only once.
To stop the loop, I need an if statement placed inside comparing the 2 fields involved, but having no success...any suggestions on how or where to insert? Thanks in advance...

IF Code:
string Projecttext = "{!Opportunity.Project_Text__c}";
string Project = "{!Opportunity.Project__c}";

if ( Projecttext != Project )
{ 

 
Main Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<script src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js"
type="text/javascript"></script>
<script language="javascript">
<!--
function init()
{
sforceClient.registerInitCallback(doit);
sforceClient.setLoginUrl("https://www.salesforce.com/services/Soap/u/7.0");
var ir = sforceClient.init("{!API.Session_ID}", "{!API.Partner_Server_URL_70}", true);
}


function goBack(Id)
{
var r = "{!Scontrol.URL}".split(/salesforce.com/);
var whichId = "";
if (Id != null) whichId = Id;
var returnUrl = r[0] + "salesforce.com/"+whichId;
window.parent.parent.location.href = returnUrl;
}

function doit()
{
var myCase = new Sforce.Dynabean("Opportunity");
myCase.set("Id", "{!Opportunity.Id}");
myCase.set("Project_Text__c", "{!Opportunity.Project__c}");

var saveResults = sforceClient.Update(myCase)[0];

if (Sforce.Util.dltypeof(saveResults) != "SaveResult" ||
saveResults.success == false ) {
alert("Error updating Case " + saveResults);
}

goBack("{!Opportunity.Id}");
}

</script>
</head>
<body onLoad="init();">
Updating Project Text....
</body>
</html>

 

Ron HessRon Hess
so, if you are just copying one opportunity field into another , i think you can use the workflow field update feature, which requires no code and should be much faster than an onpage scontrol
LukdanLukdan
Hi Ron,
Thanks for your response. The field I'm copying with the scontrol is a lookup to a custom object. If I copy the field using the update feature (I did try this first), the field value is actually the ID (link) of the custom object and not the name. The scontrol grabs the actual text value of the name and pastes it into the second field. I want this to happen without the user clicking a custom link or button, which is why I am trying to put it directly into the opportunity page layout so it happens during the page load. Ultimately I want to validate this name to contain a certain phrase. Is there a way to place the if statement within the scontrol code so it only runs if the 2 fields differ?

Ron HessRon Hess
i think one way is to perform the update, then when you reload the page and pass in an an extra paramater on the url address, say  "&didFieldCopy=true"

then in your scontrol, you check for $Request.didFieldCopy ( $Request is a special merge field , look it up)

if didFieldCopy  is true, you don't do the calc again.