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
PaulATPimptastiPaulATPimptasti 

s-Control to Populate Button URL with Values from Related Objects

Hi

I've been trying to build a custom button that will allow me to pre-populate the edit mode of a new transaction with the values from two objects.

ie. to issue refunds we need to pull in financial information from the transaction that created the asset, and also pass in the information of the asset so we can create a relationship between the refund record, the asset and the original transaction.

The code below works if you hardcode the Asset.Id value and call it from the AJAX ToolKit, but if I use the code behind a Custom Detail Page button, I get a blank screen.

Any help would be great as I'm well and truely stuck!!

Thanks

Paul

<html>
<head>
<script src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">
function init()
{
//look purchase information
var QueryString = "SELECT BillingContactRecord__c , ProductSinglePrice__c, Quantity__c, Discount__c, VatPct__c FROM Transaction_Log__c WHERE Id = '" + {!Asset.Purchase_RecordId__c} + "'";

// Issue the SOQL query using the AJAX Toolkit.
var QueryResults = sforce.connection.query(QueryString);
var m = document.getElementById('main');
var QueryRecords = QueryResults.getArray("records");
// Write out the results.

// IF the VAT ID lookup fails, then search on Name, PostalCode and BillingStreet (1st line of address)
for (var i=0; i<QueryRecords.length; i++){
var QueryRecord = QueryRecords[i];
var BillingContactRecord__c = QueryRecord.BillingContactRecord__c;
var ProductSinglePrice__c = QueryRecord.ProductSinglePrice__c;
var Quantity__c = QueryRecord.Quantity__c;
var Discount__c = QueryRecord.Discount__c;
var VatPct__c = QueryRecord.VatPct__c;
}

var newUrl = "/a08/e?retURL=%2F" + purchaseRecordID +
"&RecordType=01220000000D0iC&CF00N200000017Hkw_lkid=" + BillingContactRecord__c +
"&CF00N200000017Hkw=" + getContactInfo(BillingContactRecord__c) +
"&00N200000017Eo9=" + ProductSinglePrice__c +
"&00N200000017EoS=" + Quantity__c +
"&00N200000017Ep1=" + Discount__c +
"&00N200000017Eow=" + VatPct__c +
"&CF00N200000017kz6=" + {!Asset.Name}
"&CF00N200000017kz6_lkid=" + {!Asset.Id};
m.innerHTML += newUrl ;
window.parent.location.replace(newUrl);
}
function getContactInfo(contactID)
{
//look purchase information
var QueryString = "SELECT FirstName, LastName FROM Contact WHERE Id = '" + contactID + "'";

// Issue the SOQL query using the AJAX Toolkit.
var QueryResults = sforce.connection.query(QueryString);
var QueryRecords = QueryResults.getArray("records");
// Write out the results.

// IF the VAT ID lookup fails, then search on Name, PostalCode and BillingStreet (1st line of address)
for (var i=0; i<QueryRecords.length; i++){
var QueryRecord = QueryRecords[i];
var FullName = QueryRecord.FirstName + " " + QueryRecord.LastName
return FullName
}
}
</script>
</head>
<body onload="init()">
<div id="main"></div>
</body>
</html>
werewolfwerewolf
It probably has to do with the context of the page from which you're setting the URL.  If you're calling it from an Scontrol then the context is inside an iframe (so the main page is window.parent).  If you're in a custom button then the context will either be the main frame or a popup window (depending on how you set up your custom button) -- so setting window.parent.location may not be the correct call.

Also, you might try debugging it with Firebug to make sure that everything's working the way you think it should.