This section shows some sample JavaScript APIs that access MBOs.
var dep1 = new Department();
dep1.dept_id = "800"; dep1.dept_name="Dept"; dep1.dept_head_id="888";
department_create_onlineRequest(dep1, "supusername=supAdmin&suppassword=s3pAdmin", function() { alert(“error occurred”)});
hwc.processDataMessage = function processDataMessage(incomingWorkflowMessage, noUI, loading, fromActivationFlow, dataType) { if ( (incomingWorkflowMessage.indexOf("<XmlWidgetMessage>") === 0) || (incomingWorkflowMessage.indexOf("<XmlWorkflowMessage>") === 0) || (incomingWorkflowMessage.indexOf("<M>") === 0)) { var workflowMessage = new WorkflowMessage(incomingWorkflowMessage); if ( workflowMessage.getRequestAction() == Department.createAction ){ alert("Department id=" + workflowMessage.getValues().getData('Department_create_dept_id_paramKey').getValue() + " has been created!"); }else if ( workflowMessage.getRequestAction() ==Sales_order.findAllAction){ alert("Return Item count ="+ workflowMessage.getValues().getData('Sales_order').value.length ); //By default database it should return 54 items. } }else{ alert("TODO: Please fix me, incomingWorkflowMessage="+ incomingWorkflowMessage); } }
var oldDep = new Department(); oldDep.dept_id = "800"; oldDep.dept_name="Dept"; oldDep.dept_head_id="888";
var newDep = new Department(); newDep.dept_id = "800"; newDep.dept_name="DeptUpdated"; newDep.dept_head_id="777";
department_update_submit(newDep, oldDep, "", true );
var sales_order = new Sales_order();
sales_order.pks.put(Sales_rep_PK_pkKey, "667");
sales_order_findAll( sales_order , "", function() {}); }
hwc.processDataMessage=function processDataMessag (incomingWorkflowMessage, noUI, loading, fromActivationFlow, dataType) { if ( (incomingWorkflowMessage.indexOf("<XmlWidgetMessage>") === 0) || (incomingWorkflowMessage.indexOf("<XmlWorkflowMessage>") === 0) || (incomingWorkflowMessage.indexOf("<M>") === 0)) { var workflowMessage = new WorkflowMessage(incomingWorkflowMessage); if ( workflowMessage.getRequestAction() == Sales_order.findAllAction){ alert("Return Item count ="+ workflowMessage.getValues().getData('Sales_order').value.length ); //By default database it should return 54 items. } }else{ alert("TODO: Please fix me, incomingWorkflowMessage="+ incomingWorkflowMessage); } }
var dep = new Department(); dep.dept_id="2"; dep.dept_name="My Dep"; dep.dept_head_id="1";
var emp1 = new Employee(); emp1.emp_id = "1"; emp1.manager_id = "2"; emp1.emp_fname ="Yan"; emp1.emp_lname= "Gong"; emp1.street ="King Street"; emp1.city="Waterloo"; emp1.state ="ON"; emp1.zip_code ="n2v3l4"; emp1.phone="518-8836863"; emp1.status="A"; emp1.ss_number="024601768" emp1.salary ="324234"; emp1.start_date="1996-12-30"; emp1.termination_date ="1999-12-20"; emp1.birth_date ="1956-12-20"; emp1.bene_health_ins ="Y"; emp1.bene_life_ins ="Y"; emp1.bene_day_care="Y"; emp1.sex="F";
var emp2 = new Employee(); emp2.emp_id = "2"; emp2.manager_id = "2"; emp2.emp_fname ="Yan2"; emp2.emp_lname= "Gong2"; emp2.street ="King Street"; emp2.city="Waterloo"; emp2.state ="ON"; emp2.zip_code ="n2v3l4"; emp2.phone="518-8836863"; emp2.status="A"; emp2.ss_number="024601768" emp2.salary ="324234"; emp2.start_date="1996-12-30"; emp2.termination_date ="1999-12-20"; emp2.birth_date ="1956-12-20"; emp2.bene_health_ins ="Y"; emp2.bene_life_ins ="Y"; emp2.bene_day_care="Y"; emp2.sex="F";
dep.Employee.push( emp1 ); dep.Employee.push( emp2 );
department_create_onlineRequest(dep, "", function() {});
To delete an MBO and its children, you need to find the MBO instance online request and, from the processDataMessage function, after the online request, you need to find each child’s surrogate key value from the incoming message, create a child JavaScript instance, then add the child JavaScript instance to the parent JavaScript instance. Subsequently, when the delete function is called on the parent instance, the children are also deleted. The details of this are shown in this example in bold font.
If the delete operation has old value arguments, you also need to set old values for parent and child MBOs. This example assumes the delete operation has old value arguments, and the data (1 department and 2 employee) has been inserted into back end:
function deleteDepartment() { var dep = new Department(); dep.dept_id="2"; alert("before delete Deparatment and its children Empoyee, we need to call findByPrimary key first.") department_findByPrimaryKey( dep, "" , function(error) {alert(error)}); }
if ( workflowMessage.getRequestAction() === Department.findByPrimaryKeyAction){ var employees = workflowMessage.getValues().getData('Department_employees').value; if ( workflowMessage.getValues().getData('Department_dept_id_attribKey').getValue()== '2'){ var dep = new Department(); dep.dept_id=workflowMessage.getValues().getData('Department_dept_id_attribKey').getValue(); dep.dept_head_id=workflowMessage.getValues().getData('Department_dept_head_id_attribKey').getValue(); dep.dept_name=workflowMessage.getValues().getData('Department_dept_name_attribKey').getValue(); var oldDep = new Department(); oldDep.dept_id=workflowMessage.getValues().getData('Department_dept_id_attribKey').getValue(); oldDep.dept_name=workflowMessage.getValues().getData('Department_dept_name_attribKey').getValue(); oldDep.dept_head_id=workflowMessage.getValues().getData('Department_dept_head_id_attribKey').getValue(); for( var i = 0; i < employees.length ; i++ ) { var emp = new Employee(); emp.emp_id = employees[i].getData('Employee_emp_id_attribKey').getValue(); emp.manager_id = employees[i].getData('Employee_manager_id_attribKey').getValue(); emp.emp_fname = employees[i].getData('Employee_emp_fname_attribKey').getValue(); emp.emp_lname = employees[i].getData('Employee_emp_lname_attribKey').getValue(); emp.dept_id = employees[i].getData('Employee_dept_id_attribKey').getValue(); emp.street = employees[i].getData('Employee_street_attribKey').getValue(); emp.city = employees[i].getData('Employee_city_attribKey').getValue(); emp.state = employees[i].getData('Employee_state_attribKey').getValue(); emp.zip_code = employees[i].getData('Employee_zip_code_attribKey').getValue(); emp.phone = employees[i].getData('Employee_phone_attribKey').getValue(); emp.status = employees[i].getData('Employee_status_attribKey').getValue(); emp.ss_number = employees[i].getData('Employee_ss_number_attribKey').getValue(); emp.salary = employees[i].getData('Employee_salary_attribKey').getValue(); emp.start_date = employees[i].getData('Employee_start_date_attribKey').getValue().substr(0, 10); emp.termination_date = employees[i].getData('Employee_termination_date_attribKey').getValue().substr(0, 10); emp.birth_date = employees[i].getData('Employee_birth_date_attribKey').getValue().substr(0, 10); emp.bene_health_ins = employees[i].getData('Employee_bene_health_ins_attribKey').getValue(); emp.bene_life_ins = employees[i].getData('Employee_bene_life_ins_attribKey').getValue(); emp.bene_day_care = employees[i].getData('Employee_bene_day_care_attribKey').getValue(); emp.sex = employees[i].getData('Employee_sex_attribKey').getValue(); //set surrogateKey for employ emp._surrogateKey =employees[i].getData('_surrogateKey').getValue(); dep.Employee.push(emp ); dep.OldValue_Employee.push( emp ); }
department_delete_onlineRequest( dep, oldDep, function( error) { alert(error)}); } ........ }