Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
steven_j
Advisor
Advisor
This is a submission of the Enhance your bot building with templates blog post series.

Introduction


In this post I will show you how to retrieve credentials and text variables from the Factory.

What will we learn in this tutorial



  • How to make variables in a Factory environment

  • How to declare and get credentials from Factory

  • How to declare and get text variables from Factory

  • How to save these variables to local data

  • How to write these variables to an excel sheet (for troubleshooting)


Steps to follow



  1. Create a new environment in Factory

  2. Create a credential variable and a text variable in this environment

  3. Create a project and workflow in Desktop Studio

  4. Add activities to the workflow

  5. Edit the activities in editor to retrieve variables and save them to local data

  6. Write the variables to an Excel sheet (for troubleshooting)


Prerequisites



  • Desktop Studio and Agent (1.0.9.41), connected to Factory Tenant

  • Excel (for troubleshooting)


Instructions


1. Create a new environment in Factory



  1. Navigate to your Cloud Factory Tenant and click the “Environments” tab.

  2. Create a new environment. You can make the “Name” and “Type” fields whatever suits your needs.




2. Create a credential variable and a text variable in this environment



  1. Click inside the environment you just created, then click the “Variables” tab.

  2. Click the “Add Variable” button to add a credential variable.

  3. Make the name of the credential variable “credential_var” and make it of type “Credential”. You can enter any Username and Password.

  4. Repeat the same step but make a text variable named “text_var” and make it of type “Text”.



3.Create a project and workflow in Desktop Studio



  1. Create a new project and give it a name.

  2. Go to ‘Scripts’ tab.

  3. Right click on ‘Global’, click ‘Include library Script’, then enable ‘Excel Integration’ and click "Save'.

  4. Go to the ‘Workflow’ tab and create a new workflow. Name it whatever you would like to.


4. Add activities to the workflow


*For this step, you can either add pre-built activities or custom activities to your workflow. For either method, you will still need to edit the code in the “Scripts” perspective. This blog will follow the method of creating custom activities, but will also discuss how to add the pre-built activites.

  1. Add three “custom” activities and an “end scenario” as follows:




5. Edit the activities in editor to retrieve variables and save them to local data



  1. Build the project to generate a script skeleton in the “Scripts” tab.

  2. Place necessary code from the code below into the corresponding function blocks in your script (you may not want to copy the lines starting with "ctx.workflow" as these are unique tags that allow your project to easily link function blocks to flowchart blocks in your workflow):


// ----------------------------------------------------------------
// Step: credential_management
// ----------------------------------------------------------------
GLOBAL.step({ credential_management: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('GetCredentials', '3d71c04b-4acb-486a-b85d-ef2e0ac06469');

// Declare the credential. "credential_var" is the name of your credential in Factory
ctx.cryptography.credential({ credential_var: {
server: true
}});
// Get the credential. "credential_var" is the name of your credential in Factory
sc.localData.credential = {};
ctx.cryptography.credentials.credential_var.get(function(code, label, credential) {
if (code == e.error.OK) {
sc.localData.credential.user = credential.userName.get();
sc.localData.credential.password = credential.password.get();
} else {
ctx.log('Error during credential retrieval',1);
}
sc.endStep();
return;
});
}});

// ----------------------------------------------------------------
// Step: text_management
// ----------------------------------------------------------------
GLOBAL.step({ text_management: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('GetCredentials', 'd40354be-dffd-4e0c-883c-2b866bed48b0');

// Declare the text variable. "text_var" is the name of your text variable in Factory
ctx.setting({ text_var: {
server: true
}});
// Get the text variable. "text_var" is the name of your text variable in Factory
ctx.settings.text_var.get(function(code, label, setting) {
if (code === e.error.OK) {
sc.localData.text_var = setting.value;
} else {
ctx.log('Error during text variable retrieval',1);
}
sc.endStep();
return;
});
}});

6. Write the variables to an Excel sheet (for troubleshooting)


This step will only cover writing custom code to write the variables to an Excel file, as this is not the primary concern of this post. This is only meant to troubleshoot your project and make sure you are getting the credentials and variables correctly. In a real project, this should not be done because saving the credentials may cause security concerns.

To write the variables to Excel, please use the following code. Adjust the file location if necessary:
// ----------------------------------------------------------------
// Step: write_variables_to_excel
// ----------------------------------------------------------------
GLOBAL.step({ write_variables_to_excel: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('GetCredentials', '6b2e6052-111d-4d2c-ab61-a0c8f2886ad3') ;
// Write credential and text variable to an Excel file.
// This is only to check that you are obtaining the credentials correctly.
ctx.options.excel.newXlsInstance = false;
ctx.options.excel.visible = true;
ctx.options.excel.displayAlerts = false;
// Initialize Excel
ctx.excel.initialize();
// Create an Excel file (If file alread exists, use open)
if (ctx.fso.file.exist('C:\\Users\\Public\\get_vars.xlsx')){
ctx.excel.file.open('C:\\Users\\Public\\get_vars.xlsx');
} else {
ctx.excel.file.create();
}
// Activate the Excel sheet
ctx.excel.sheet.activate('Sheet1');
// Set Excel cell values
ctx.excel.sheet.setCell(1,1,'user');
ctx.excel.sheet.setCell(2,1,'password');
ctx.excel.sheet.setCell(3,1,'text_var');
ctx.excel.sheet.setCell(1,2,sc.localData.credential.user);
ctx.excel.sheet.setCell(2,2,sc.localData.credential.password);
ctx.excel.sheet.setCell(3,2,sc.localData.text_var);
// Save Excel file, close and end the program
ctx.excel.file.saveAs('C:\\Users\\Public\\get_vars.xlsx', 61);
ctx.excel.file.close();
ctx.excel.end();
sc.endStep(); // end Scenario
return;
}});

 

Note regarding using pre-built activities method:


If you wanted to use prebuilt activities instead of custom code, the workflow could have been formatted as follows (make sure you set the “Declare” and “Get” activity properties accordingly):


After building the project, these pre-built activities will automatically generate the following code when the project is built (only auto-generated code for “Credential” activities is show but the same pattern follows with “Setting” activities):
// ----------------------------------------------------------------
// Step: Declare_credential
// ----------------------------------------------------------------
GLOBAL.step({ Declare_credential: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('newWorkflow', '7c375cf2-ef79-485b-b00b-fa664a5da4dd') ;
// Declare credential

ctx.cryptography.credential({ credential_var: {
comment: "credential variable",
server: true
}});
sc.endStep(); // Get_credential
return;
}});

// ----------------------------------------------------------------
// Step: Get_credential
// ----------------------------------------------------------------
GLOBAL.step({ Get_credential: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('newWorkflow', 'ada621eb-a97f-41c7-bf31-86ca10f8b0f2') ;
// Get credential

ctx.cryptography.credentials.credential_var.get(function(code, label, credential) {
if (code == e.error.OK) {
// TODO : set login/password here
sc.endStep(); // Declare_setting
return;
}
});
}});

As can be seen, the general skeleton for the necessary code is provided, but must be filled in to create the credential retrieval functionality. This automatically generated code can be filled in with the custom code from Step 5 accordingly to create this functionality.

Conclusion


This blog post should help you understand how to retrieve credentials from the Cloud Factory and store them in local data. You should now be able to use this technique to retrieve any credentials or variables from the Factory you may need to access your application in a project.