MBO Access JavaScript API Samples

This section shows some sample JavaScript APIs that access MBOs.

Calling a Create Function

  1. Create a JavaScript object, in this case, Department.
    var dep1 = new Department(); 
  2. Set the values for all the fields. The fields names map to the Department MBO create operation’s parameter name.
    dep1.dept_id = "800";
    dep1.dept_name="Dept";
     dep1.dept_head_id="888";
    
  3. Call the create online request function.
    department_create_onlineRequest(dep1, 
                	    		"supusername=supAdmin&suppassword=s3pAdmin", 
                	            function() { alert(“error occurred”)});
  4. For an online request, you should implement the hwc.processDataMessage function, for example:
    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);
                	}
                  
                }

Calling an Update Function With Old Arguments

  1. Set  old arguments values:
    var oldDep = new Department();
    oldDep.dept_id = "800";
    oldDep.dept_name="Dept";
    oldDep.dept_head_id="888";
  2. Set the new values:
    var newDep = new Department();
    newDep.dept_id = "800";
    newDep.dept_name="DeptUpdated";
    newDep.dept_head_id="777";
  3. Call the update submit function:
    department_update_submit(newDep, oldDep, "", true ); 

Passing a Personalization Key Value

  1. Create Sales_order object:
    var sales_order = new Sales_order(); 
  2. Set the onload personalization key value:
    sales_order.pks.put(Sales_rep_PK_pkKey, "667"); 
  3. Call the findAll online request:
    sales_order_findAll( sales_order , "",  function() {});   
    }   
  4. In the process workflowMessage function, to process  incoming message, add:
    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);
                	}
                  
                }

Calling a Create Function on MBOs With a One to Many Relationship

  1. Create a new Department:
    var dep = new Department();
     dep.dept_id="2";
     dep.dept_name="My Dep";
    dep.dept_head_id="1";
  2. Create a new employee:
    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";
  3. Create a second employee:
    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";
  4. Add the two employees to Department:
    dep.Employee.push( emp1 );
    dep.Employee.push( emp2 );   
  5. Call department create online request, it would create a new department and two new employees entries in the database:
    department_create_onlineRequest(dep, 
            	          "",     function() {});

Calling a Delete Function on MBOs With a One to Many Relationship

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:

  1. Call the department_findByPrimaryKey online request to find the department instance:
    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)});
            	 
    }
  2. In the processDataMessage function, find the surrogatekey value for each Employee and create Employee instance and add it to department instance:
    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 );
    }
  3. Call department_delete_onlineRequest to delete the department and all of its children:
    department_delete_onlineRequest( dep, oldDep, function( error) { alert(error)});
            				  
    }
       ........ 
    }