GetContextInfo() is perhaps the most commonly used method within the Sage CRM API. This is both within internal code (Create scripts, Validate scripts etc) and application extensions which maybe either COM ASP or .NET API.
GetContextInfo() returns a named field from the given table based on the current context. The concept of context is another subject that I have written about previously in an number of articles. Which entities are in context is determined by the information in the URL. How the key values within the URL are used to build context is discussed in the article "Key Values in URLs".
Below are examples of how the GetContextInfo is used. The values that is returned can then be used for any other code that needs to be run.
JScript COM Example
var strCompName = CRM.GetContextInfo("company","comp_name");
var intUserId = CRM.GetContextInfo("user","user_userid");
C# .NET API Example
string strCompName = GetContextInfo("company", "comp_name");
string strUserId = GetContextInfo("user", "user_userid");
The obvious difference in the usage above is that since JScript is a loosely typed language that if GetContextInfo() returns something that looks like a number then it can be treated like a number. In C# the language is strongly typed and GetContextInfo() returns only strings.
But how does GetContextInfo obtain the information?
We can write
string strCompName = GetContextInfo("company", "comp_name");
we may know that this is a valid piece of code for any entity in context but when we consider the URL that creates the context it will only at most hold the unique ID for a record. The URL has the name/value pairs " &key0=1 &key1=28 &key2=30". So we may know quickly the unique ID for the company is 28, but how is the company name "Gatecom" returned?
Consider the following code snippet
var intOppoID = CRM.GetContextInfo("opportunity","oppo_opportunityid");
var strDescription = CRM.GetContextInfo("opportunity","oppo_description");
var intPersID = CRM.GetContextInfo("person","pers_personid");
var strName = CRM.GetContextInfo("person","pers_lastname");
var intCompID = CRM.GetContextInfo("company","comp_companyid");
var strCompName = CRM.GetContextInfo("company","comp_name");
var intUserID = CRM.GetContextInfo("user","user_userid");
var strUserName = CRM.GetContextInfo("user","user_lastname");
The code above if used in an ASP page called from the Opportunity tab would result in the following SQL being generated
select * from vSummaryOpportunity where Oppo_OpportunityId=65
select * from vSummaryPerson where Pers_PersonId=1662
select * from vSummaryCompany where Comp_CompanyId=56
select * from vUsers where User_UserId=1 AND COALESCE(user_disabled, N'') = N'' AND (COALESCE(user_istemplate, N'') = N'' OR user_istemplate = N'N')
What does this SQL mean?
Only the first GetContextInfo() that interrogates an entity will result in an SQL statement being submitted to the database. The result of the select is then cached in memory within IIS for the duration of the page. All subsequent GetContextInfo() requests for the same entity will use the result cached in memory. When the page is finished processing all the GetContext() data cached in memory is lost.
This means that within an ASP page it is a very convenient way of retrieving data for the entities in context.
N.B.
Only system entities can be referenced using GetContextInfo. Custom entities are not recognised.