K2 blackpearl Product Documentation: Installation and Configuration Guide
Configuring credentials to execute Salesforce SmartObject methods

Configuring credentials to execute Salesforce SmartObject methods

Problem Summary:

A Salesforce service instance is created and some smart objects are built from it.

The application that uses the smart objects can not execute their methods unless the end user has cached the Salesforce credentials in Workspace while being logged in as themselves. The application needs to use impersonation, so as long as the impersonated user has cached his credentials beforehand, the smart object methods will execute successfully. If the user did not cache his credentials, the exception thrown is "No Credentials Cached for this label".

Workaround:

To avoid the requirement that each end user needs to cache Salesforce.com credentials in order for a smart object method to execute, do the following:

  1. Ensure that the web application using the smart objects is running under an app pool account that is in turn configured to run under a service account user identity (ie. not a predefined service).
  2. In your browser, log into workspace as that app pool service account user and drill into "K2 Management > Smart Objects > Services > Salesforce Service". Select the service instance that you are using and click ’Credentials’, then enter the Salesforce credentials. Ensure that you append the Salesforce token to your password.
  3. In the web application code, you need to:
    (i) remove impersonation,
    (ii) sign into the smart object server,
    (iii) execute the smart object method(s), then once you are done with the connection,
    (iv) resume impersonation. By removing impersonation during sign in, the k2 server will pick up the service account user in the app pool that this application is running under.

Web application code:

Copy

using SourceCode.SmartObjects.Client;

.

.

.

 

object retVal = null, paramValue = "Some_Value";

 

// Stop impersonation

System.Security.Principal.WindowsImpersonationContext ctx = System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero);

 

SmartObjectClientServer clientServer = new SmartObjectClientServer();

clientServer.CreateConnection();

clientServer.Connection.Open("Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=[YOUR_K2_HOST];Port=5555"); // conn string hard-coded for simplicity.

SmartObject soOpportunity = clientServer.GetSmartObject("sfOpportunity"); // or whatever your smart object is named.

 

try

{

// replace quoted values with your own values.

smartObject.Methods["methodToExecute"].Parameters["paramName"].Value = (string)paramValue;

smartObject.MethodToExecute = "methodToExecute";

clientServer.ExecuteScalar(soOpportunity);

retVal = smartObject.Properties["returnPropertyName"].Value;

.

.

.

.

}

catch (SourceCode.Hosting.Exceptions.AuthenticationException authEx)

{

if (ctx != null)

{

ctx.Undo();

}

throw new Exception("SourceCode.Hosting.Exceptions.AuthenticationException: " + authEx.Message);

} //*/

catch (Exception ex)

{

// Resume impersonation

if (ctx != null)

{

ctx.Undo();

}

throw new Exception(ex.Message);

}

finally

{

// Resume impersonation

if (ctx != null)

{

ctx.Undo();

}

}

 

 


K2 blackpearl Product Documentation: Installation and Configuration Guide 4.6.10