Adding a Button to Generate an SData feed based on Current Context using the ASP/COM API

1 minute read time.

I have discussed before how useful SData can be. If you know the feed details then you can use SData to extract data from Sage CRM into all sorts of applications.

But there is a small snag and that is we can't expect users to know that if they want to pull out the Opportunities that belong to the company "Gatecom" that they should enter

http://localhost/sdata/crmj/sagecrm/-/opportunity?where=oppo_primarycompanyid eq 28 &SID=188741193641488

Below is a image that shows an install of Sage CRM that has had an extra button added to the System Action "OpportunitiesList" using the Buttons Group feature. The button will show up in any of the default locations the System Action "opportunitieslist" is used e.g. the Company tab, the Person tab, the My CRM tab and the Team tab.

When the button is pressed the user is given the correct URL for the entity. They can then use CTRL+C to copy the URL.

Below is the ASP code to create this behaviour.

[code language="javascript"]

<%
//Code assumes that this is for the Opportunity Entity
//Assign System Action that page should return to.
var strOriginalSystemAction = "184";
//Get the details of the server, installname, and Session ID.
var strHostName = CRM.Host;
var strInstallName = sInstallName.toLowerCase();
var strSID = Request.QueryString("SID");

//Check which context the Opportunity List is being called from.
//Establish foreign key name and value.
var strForeignKeyName;
var intForeignKeyValue;
var iDomKey = Number(Request.QueryString("key0"));
switch (iDomKey)
{
case 1:
strForeignKeyName = "oppo_primarycompanyid";
intForeignKeyValue = Request.QueryString("key1");
break;
case 2:
strForeignKeyName = "oppo_primarypersonid";
intForeignKeyValue = Request.QueryString("key2");
break;
case 4:
strForeignKeyName = "oppo_assigneduserid";
intForeignKeyValue = Request.QueryString("key4");
break;
case 5:
strForeignKeyName = "oppo_channelid";
intForeignKeyValue = Request.QueryString("key5");
break;
default:
strForeignKeyName = "oppo_assigneduserid";
intForeignKeyValue = Request.QueryString("key4");
}
//Build SData URL
var strSdataUrl = strHostName + "/sdata/" + strInstallName + "j/sagecrm/-/opportunity?where=" + strForeignKeyName +" eq " + intForeignKeyValue + " &SID=" + strSID;
//Create prompt for user to copy SData URL and return to Original System action
CRM.AddContent("");
CRM.AddContent("");
Response.Write(CRM.GetPage());
%>
[/code]

You can see a C# version of this code that use the .NET API in the article "Adding a Button to Generate an SData feed based on Current Context using the .NET API".