Adding Groovy Class Sources
To add Groovy class sources:
1. In Max Designer, on the Developer Tools () launchpad menu, click Sources, and then in the list view, in the top left corner, click Create ().
2. On the record page, in the Name field, enter a name for the source, for example, Shopping Operation Source, and then in the Application field, click the application to be associated with this source, for example,Shopping App.
3. In the Overview tab, in the Code field, add the following Groovy code, and then in the top left corner, click Save and Close ().
com.maxservice.operation.ShoppingOperation extends TemplateOperationEventHandler

package com.maxservice.operation

import com.servicemax.core.event_handler.TemplateOperationEventHandler

class ShoppingOperation extends TemplateOperationEventHandler {

public Object realExecute(Map <String, Object > params) {

// Current shopping item record
def shoppingItem = affectedMaxObject

if (ShoppingOperationUtils.createTaskForSalesRepresentative(shoppingItem)) {
logger.info("Genearted task for repersentive with UUID: ${shoppingItem.io_uuid}")
} else {
logger.error("Failed to genearted task for repersentive with UUID: ${shoppingItem.io_uuid}")
}
}
4. Repeat the previous steps to create another Source record named Shopping Operation Utilities Source, with the following Groovy code implemented in the Code field:
package com.maxservice.operation

import org.joda.time.DateTime

import com.servicemax.core.Database
import com.servicemax.core.MaxObject
import com.servicemax.core.Record
import com.servicemax.core.utils.Definitions
import com.servicemax.core.utils.UserInfo

class ShoppingOperationUtils {

/**
* Create task for shopping interest, so the sales people can follow up.
*
* @param shoppingItem
* @return
*/
public static boolean createTaskForSalesRepresentative(MaxObject shoppingItem){
def territory = shoppingItem.dev_territory
if (territory) {
def task = Record.newRecord('io_task')

//For the assigned to field, it's array of [recordid, objectid]
def assignedToRecordId = territory.dev_sales_representative.io_uuid
def assignedToObjectId = Definitions.getObjectDefinition('io_user').getRecordID()
task.io_assigned_to = [
assignedToRecordId,
assignedToObjectId
]

//Set the task owner to current login user.
task.io_owner = UserInfo.getUserId()
task.io_name = 'Follow up on - ' + shoppingItem.dev_name

//Set related to field to current shopping item to follow up
def relatedToRecordId = shoppingItem.io_uuid
def relatedToObjectId = Definitions.getObjectDefinition('dev_shopping_item').getRecordID()
task.io_related_to = [
relatedToRecordId,
relatedToObjectId
]

task.io_due_date = DateTime.now().plusDays(2)
Database.insert(task)
return true
}
return false
}
}
For more information:
Was this helpful?