| K2 blackpearl Installation and Configuration Guide > Installation > Integration Configuration > Salesforce > Configuring credentials to execute Salesforce SmartObject methods | Send feedback |
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".
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:
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(); } } |