Wednesday, February 11, 2009

Re-directing Salesforce users to another page

I did this awhile back and know that other organizations have similar requirements. This is an S-Control, so I'll need to re-do it in Visualforce at some point, but it works for now. What my client wanted to do was this: on saving a record, re-direct the user to a record other than the one that was just saved. In some cases, this can be accomplished by overriding the Edit button to pass the record ID you want the user to land on into the retURL parameter. But that wasn't an option here, because they wanted the user to go to a record that was being created by Apex code on save. So, I created the S-Control below and pass the S-Control's ID to the retURL parameter. It obviously needs to be tweak for use in any other Salesforce instance, but this should give anyone with a similar requirement a good starting point:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

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

<script type="text/javascript">
function initPage() {
var newid = "{!$Request.newid}";
var strSQL;
var strRe;

//Redirecting after matching child case to AP case
if (newid.substring(0,3) == "a0V"){
strSQL = "Select Id, Child_Case__c from Match_Child_Case__c where Id = '" +newid +"'";
}

//Redirecting after creating child/case
if (newid.substring(0,3) == "003"){
strSQL = "Select Id from Child_Case__c where Contact__c = '" +newid +"'";
}


if (strSQL != null){
var result = sforce.connection.query(strSQL);
var records = result.getArray("records");

i=0;

while ( i<records.length) {
var rec = records[i];

if (newid.substring(0,3) == "a0V"){
strRe = rec.Child_Case__c;
}

if (newid.substring(0,3) == "003"){
strRe = rec.Id +"/e";
}
i++;
}
}

if (strRe != null){
window.parent.location.href = "/" +strRe;
} else {
//window.parent.location.href = "/" +newid;
}
}
</script>

</head>

<body onLoad="javascript:initPage();">

</body>
</html>