[+]
[+]
[+]
[-]
  
  
 [+]
 [+]
 [+]
  
  
  
 [+]
 [+]
 [-]
   
   
   
   
   
   
   
   
   
 [+]
 [+]
 [+]
 [+]
  
 [+]
  
Updated on 8/14/2019
Workflow Documentation
Workflow Script Example
Direct link to topic in this publication:

PREVIOUS  |  NEXT

Workflow Script Example

Quick Links

Use the Workflow script type to update custom fields, create users or related cases, and various other actions.

You can add the script to the Workflow Events section for any action when you configure its Workflow Action Settings

Workflow Script Example

The following example describes how to create a script that creates a new user. 

In this example, we show you how to create a script that creates a new user when an end user executes a workflow action, and describe how to add the script to a Workflow action.  

Create Workflow script

[Back to top]

The following example describes how to create a script that creates a new user. 

  1. Go to Setup > Development > Scripts.
  2. Select  Script from the New menu, or click the Create New link in the instructional text on the Scripts workspace.
  3. Name the script Create New User.
  4. Select the Workflow script type.
  5. Enter the following code in the code window:
    def userType = resp.getUserType('customer');
    def dep = resp.getDepartment('approvers');
    
    def loginName = 'jsmith';
    def firstName = 'John';
    def lastName = 'Smith';
    def email = 'jsmith@sapsalescloud.com';
    def password = 'testpass123';
    
    def newUser = resp.users.newUser();
    newUser.setLoginName(loginName)
    newUser.setFirstName(firstName);
    newUser.setLastName(lastName);
    newUser.setEmail(email);
    newUser.setDepartment(dep);
    newUser.setUserType(userType);
    
    resp.save(newUser, password);
    
  6. Click Save and Publish.

Add Script to Workflow Action

[Back to top]

The following steps describe how to add the Create New User script to a workflow action.

  1. Go to Setup > Processes > Workflows.
  1. Click the Edit button of the workflow you want.
  2. Create a new action or select an existing action to which to add the new script. 
  3. Open the Events panel and select the After workflow action link in the Workflow Events section.
  4. In the Script Name field, select the Create New User script you created.
  5. Save your action.

For more information about creating an action, see Add New Action

Expected Results

[Back to top]

After you create the script and add it to a workflow action, the following user record will be created when an end user executes the action:

  • Login name: jsmith
  • Password: testpass123
  • First Name: John
  • Last Name: Smith
  • Email: jsmith@sapsalescloud.com
  • Department: Approvers
  • User Type: Customer

You can confirm the new user was created by going to:

  • More > Users
  • Setup > Users > Users

Case Examples

Change status of the current case

The following example changes the status of the current case to Closed.

def s = resp.getStatus('Closed');     

currentCase.setStatus(s);

resp.update(currentCase);

Change project of the current case

The following example moves the current case to the project called WPM.

def p = resp.getProject('WPM');

currentCase.setProject(p);

resp.update(currentCase);

Create an account and connect it to a case

def a = resp.newAccount();

a.name = 'TestAccount';

a.description = 'some text... ';

resp.save(a);

currentCase.account = a;

resp.update(currentCase);

Create a contact and connect it to a case 

def contact = resp.newContact();                

contact.firstName = 'John';          

contact.lastName = 'Smith';                

contact.email = 'sometestmail12@mail.com';

resp.save(contact);

currentCase.setContact(contact);

resp.update(currentCase);

Create contact and account, set account to a contact, and set both to a case

def contact = resp.newContact();

contact.firstName = 'John';

contact.lastName = 'Smith';

contact.email = 'sometestmail@mail.com';

 

def account = resp.newAccount();

account.name = 'TestAccount';

account.description = 'some text... ';

resp.save(account);

 

contact.account = account;

resp.save(contact);

 

def caze = resp.getCase("NewFeature-RPR-10");

caze.account = account;

caze.contact = contact;

resp.update(caze);


Custom Field Examples

Set custom field and save case (javascript): 

def caseType = resp.getCaseType('RMA');

def newCase = resp.newCase(caseType);

newCase.setCustomField('duration',10);

resp.save(newCase);

Check for selected values in custom field (javascript)

if (currentCase.isCustomFieldSelectedAny('browser', 'firefox, chrome')){

   //some code

}

or 

var cf = currentCase.getCustomField('browser');

if (cf != null && cf.isSelectedAny( 'firefox, chrome')){

     //some code

}

Check for all selected values in custom field (javascript)

if (currentCase.isCustomFieldSelectedAll('browser', 'firefox, chrome')){

   //some code

}

or 

def cf = currentCase.getCustomField('browser');

if (cf != null && cf.isSelectedAll( 'firefox, chrome')){

     //some code

}

Set custom field type

The following examples show how to set values for custom fields in a specific case.

Integer

currentCase.setCustomField('duration',13);

resp.update(currentCase);

Decimal 

currentCase.setCustomField('price', 1999.99);

resp.update(currentCase);

Date 

currentCase.setCustomField('eta', '2012-07-25');

resp.update(currentCase);

Date and time

currentCase.setCustomField('time_of_arrival', '2012-07-26 13:15:16');

resp.update(currentCase);

Currency 

currentCase.setCustomField('currency', '399.99');

resp.update(currentCase);

Radio button 

currentCase.setCustomField('bug_fixed', 'yes');  

resp.update(currentCase);

Checkbox 

currentCase.setCustomField('optional_hardware_ordered', 'printer, scanner');  

resp.update(currentCase);

Send Email Examples

Send email to a user 

def message = resp.newMessage();

message.subject = 'Mail to user...';

message.body = "Hi user, I'm sending you an email.";

def u = resp.getUser('jsmith');

message.addRecipient(u);

resp.send(message);

Send email to a contact 

def message = resp.newMessage();

message.subject = 'Mail to contact...'; \\

message.body = 'Hi contact, I\'m sending you an email.';  

def c = resp.getContact('accountName-jsmith@domain.com');  

message.addRecipient(c);

resp.send(message);

Send email to multiple addresses 

def message = resp.newMessage();

message.subject = 'Mail to specific address...';

message.body = "Hello All, I'm sending you an email.";

def address = "jsmith@domain.com";

def address2 = "jsmith@domain.com";

def address3 = "jsmith@domain.com";

message.addRecipient(address);

message.addRecipient(address2);

message.addRecipient(address3);

resp.send(message);


Forward Application to Internal Address

actionResult.setForward('homepage');

Disable Notifications and Display Message

actionResult.setNotificationsEnabled(false);

actionResult.addMessage('Notifications are sent to no one!!!');


Creating new user : THIS EXAMPLE IS FROM THE OLD HELP. THERE'S A DIFFERENT ONE AT THE TOP OF THIS TOPIC. WHICH ONE SHOULD I USE?

def uType = resp.getUserType('customer');

def dep = resp.getDepartment('sales');

def u = resp.newUser();

u.loginName = 'jsmith';

u.firstName = 'John';

u.lastName = 'Smith';

u.email = 'jsmith@johnsmail.com';

u.userType = uType;

u.department = dep;

resp.save(u, 'password');

 

Add Container and Edit Rows

def userDetails=currentCase.getContainer('pump_details');

currentCase.addContainer('software_versions_table');

 

resp.save(currentCase)

 

def row1=userDetails.getRow(0)

def cell= userDetails.getCell(0,'type_of_user');

def column=userDetails.getColumn('type_of_user');

def rows=userDetails.getRows();

 

def updateMap = new java.util.HashMap();

updateMap.put('type_of_user', 'abcd');

updateMap.put('number_of_users', '10');

updateMap.put('total_monthly_fee', '15');

 

userDetails.updateRow(0,updateMap);

 

resp.update(currentCase)

 


Related Articles