Understanding GetContextInfo()

2 minute read time.

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.

  • Jeff!

    Thank you for the information! I will read through it.

    In regards to using the addressid, I didn't design this area so I cannot fully speak to the "why". However, to get to the site data, you first have to get to the address data. Therefore, I think the connection to the company and the address is made in the url and then using that information, a connection is made to the custom entity in the asp pages. All the tabs to get to the site data are setup the address tabs.

    You cannot directly connect the site data to the company because there is nothing to connect them on. Therefore, the address table has been used to make that connection (the "bridge").

    I hope I am making some sense...:-)

    Hopefully, I can put it all together once I get more information on getting to records that are not in context.

    Thanks!

  • Michele

    There are links to articles that discuss handling context in ASP pages for custom entities here: community.sagecrm.com/.../a-round-up-of-articles-about-creating-asp-pages-the-essential-guide-to-extending-the-interface-using-the-com-asp-api.aspx

    But having said that.

    If you are in the context of a company then you will have the default address available to you.

    var myaddressid = CRM.GetContextInfo("company","comp_primaryaddressid");

    If you find yourself looking at a system address screen this may ignore GetContextInfo() BUT you will know which context you are in because of the URL.

    &Key0=1&Key1=28&Key2=30&Key3=54&Addr_AddressId=54

    You can see that the Key Value (community.sagecrm.com/.../key-values-in-urls.aspx) for Address is 'Key3' and not only is that displayed but also explicit use is made of the Addr_AddressId with URLs.

    I am not sure why you have used Key58 to obtain the address information. Key58 is typically used for custom entities.

  • Jeff --

    Is there a place in the community that explains how to get the correct data to display on the screen if the entity in context is a secondary or custom entity. Also, I think I am going to have to create a "bridge" between two tables to get to the data that I want to display ... which is an external table. I have been sketching it out ... but I am not sure how to put it together in an ASP page.

    I think I understand the concept of GetContextInfo() for primary entities that are in context. The above post demonstrates excellent examples of how to apply GetContextInfo() along with what happens when used in an ASP page and what SQL is generated..I love that!! Is there something similar to this for secondary or custom entities (I say secondary or custom entities because one of my entities is the address table.)?

    To fully explain what I am trying to do might be too much to write in the comments...but briefly, I want to display a list of contacts from an external database. The contacts are associated with different "sites" associated with the company record. The "sites" are connected to the company through the address table. The address table represents the "bridge" that connects the company to the sites. There is no common identifier on the company and site tables to bring them together. However, there is a field on the address table called addr_smalias.

    From what I have found in the community, I might be on the right path with CRM.FindRecord (for the non primary entities) and I think I could pull it off if I was just dealing with the company and address tables ... or the company and site tables (assuming they had a common identifier). It is adding in that third element that has me stumped in the javascript.

    I am hoping you can give me a shove in the right direction.

    This is the context information in my URL .... Key0=58&Key1=2258&Key58=2261

    Therefore, I have added the following to my ASP page to define the companyid and the addressid (Key58) is referencing the addressid

    var CompanyID = Request.QueryString("Key1");

    var AddressID = Request.QueryString("Key58");

    I am using a list from the SERVICESITE custom entity named "SMContactsList"

    Therefore, I have added the following to my ASP page to define my block

    var listBlockName = "SMContactsList";

    var myBlock = CRM.GetBlock(listBlockName);

    If I where writing SQL to return the records that I want, I would write it as follows (NOTE:SERVICESITE is an external table in CRM)...

    SELECT

    Company.comp_companyid,

    Address.Addr_AddressId,

    SERVICESITE.ALIAS,

    SERVICESITE.C1NAME,

    SERVICESITE.C1PHONE

    FROM Address

    LEFT JOIN Address_Link ON Address.Addr_AddressId = Address_Link.AdLi_AddressId

    LEFT JOIN Company ON Address_Link.AdLi_CompanyID = Company.Comp_CompanyId

    LEFT JOIN SERVICESITE ON Address.addr_smalias = SERVICESITE.ALIAS

    WHERE Address.Addr_AddressId='2261'

    Any assistance you can provide would be greatly appreciated....Thank you!!!

  • Robert

    Yep, custom fields in system entities are fine. You can use CRM.GetContextInfo("company","comp_mycustomfield") to grab data OK. You just cant use GetContextInfo with a custom table.

  • Can custom fields in system entities be referenced?