
import AP.Common.GDT as apCommonGDT;
businessobject ZEmpBO raises Succ_Emp_Crt, Succ_Emp_Upd, Err_Emp{
message Succ_Emp_Crt text "Employee ID &1 has been created." : EmployeeID;
message Succ_Emp_Upd text "Employee ID &1 has been updated.": EmployeeID;
message Err_Emp text "Error: &1": LONG_Description;
element EmpID : ID;
element FirstName : LONG_Name;
element LastName : LONG_Name;
element CreateEmp:Indicator;
element UpdateEmp:Indicator;
action EmpBOViaAPICall;
}
import ABSL;
import AP.PDI.bo;
import AP.Common.GDT;
import AP.PDI.Utilities;
var urlParams : collectionof NameAndValue;
var headerParams : collectionof NameAndValue;
var jsonKey: collectionof String;
var headerParam: NameAndValue;
var resourceName : String;
var msg : LONG_Description;
var empID: EmployeeID;
var key: String;
var result_get: RESTCallResult;
var firstName; var lastName; var startDate; var endDate; var body;
// Define header to Fetch CSRF Token
headerParam.Name = "x-csrf-token";
headerParam.Value = "fetch";
headerParams.Add(headerParam);
if( this.CreateEmp == true) // Create employee Case
{
// Make a call to C4C OData API to fetch CSRF Token
result_get = WebServiceUtilities.ExecuteRESTService("ZC4CODATA_REST_SRV","ZC4CODATA_REST","GET","",urlParams,headerParams,"","");
// Status code 200 means call was successfull
if (result_get.Code == "200 ")
{
headerParams.Clear();
headerParam.Name = "x-csrf-token";
headerParam.Value = result_get.HeaderParameters.Where(n=>n.Name == "x-csrf-token").GetFirst().Value; // Read CSRF Token value from response header
headerParams.Add(headerParam);
headerParam.Name = "Accept";
headerParam.Value = "application/json"; // To get the response in JSON format
headerParams.Add(headerParam);
// Now make a POST call to EmployeeCollection
resourceName = "EmployeeCollection"; // Specify resource collection/entityset
firstName = "\"" + this.FirstName.content + "\"";
lastName = "\"" + this.LastName.content + "\"";
startDate = "\"" + Context.GetCurrentSystemDateTime().content.ToString() + "\"";
endDate = "\"" + DateTime.ParseFromString("9999-12-12T00:00:00").content.ToString() + "\"";
body = "{ \"FirstName\":" + firstName + ", \"LastName\":" + lastName + ", \"EmployeeValidityStartDate\":" + startDate + ", \"EmployeeValidityEndDate\":" + endDate + "}";
var result_post = WebServiceUtilities.ExecuteRESTService("ZC4CODATA_REST_SRV","ZC4CODATA_REST","POST",resourceName,urlParams,headerParams,"application/json",body,result_get.Cookies); // This is the JSON payload which will be sent to the backend
if( result_post.Code == "200 " || result_post.Code == "201 " || result_post.Code == "202 " || result_post.Code == "204 ") // Any code of 2XX will be considered as successfull hence checking all the possible http codes
{
key = "d.results.EmployeeID"; // Specify Json path to be read
jsonKey.Add(key);
empID.content = Json.ParseKeyValues(jsonKey,result_post.Content).KeyValue.GetFirst().Value; // Parse JSON Key value and read Employee created ID.
Succ_Emp_Crt.Create("S",empID);
return;
}
}
else
{
// Raise error message and return
msg.content = "CSRF Token couldnot be fetched.";
Err_Emp.Create("E",msg);
return;
}
}
if(this.UpdateEmp == true) // Update Employee
{
resourceName = "EmployeeCollection?$filter=EmployeeID eq '"+ this.EmpID +"'"; // Get the employee record to be updated
headerParam.Name = "Accept";
headerParam.Value = "application/json"; // Get the employee record in JSON format
headerParams.Add(headerParam);
// Execute the employee read odata call
result_get = WebServiceUtilities.ExecuteRESTService("ZC4CODATA_REST_SRV","ZC4CODATA_REST","GET",resourceName,urlParams,headerParams,"","");
if (result_get.Code == "200 " ) // HTTP 200 means call to backend was successfull
{
headerParams.Clear();
headerParam.Name = "x-csrf-token";
headerParam.Value = result_get.HeaderParameters.Where(n=>n.Name == "x-csrf-token").GetFirst().Value; // Read CSRF token value from the response header
headerParams.Add(headerParam);
headerParam.Name = "Accept";
headerParam.Value = "application/json";
headerParams.Add(headerParam);
key = "d.results[1].ObjectID"; // Define key to read the JSON payload
jsonKey.Add(key);
resourceName = "EmployeeCollection('" + Json.ParseKeyValues(jsonKey,result_get.Content).KeyValue.GetFirst().Value + "')"; // Parse JSON payload and format the resourcename
firstName = "\"" + this.FirstName.content + "\"";
lastName = "\"" + this.LastName.content + "\"";
startDate = "\"" + Context.GetCurrentSystemDateTime().content.ToString() + "\"";
endDate = "\"" + DateTime.ParseFromString("9999-12-12T00:00:00").content.ToString() + "\"";
body = "{ \"FirstName\":" + firstName + ", \"LastName\":" + lastName + ", \"EmployeeValidityStartDate\":" + startDate + ", \"EmployeeValidityEndDate\":" + endDate + "}";
var result_patch = WebServiceUtilities.ExecuteRESTService("ZC4CODATA_REST_SRV","ZC4CODATA_REST","PATCH",resourceName,urlParams,headerParams,"application/json",body,result_get.Cookies); // Payload to be sent top the backend with API call
if( result_patch.Code == "200 " || result_patch.Code == "201 " || result_patch.Code == "202 " || result_patch.Code == "204 " )
{
empID.content = this.EmpID;
Succ_Emp_Crt.Create("S",empID);
return;
}
}
else
{
// Raise error message and return
msg.content = "CSRF Token couldnot be fetched.";
Err_Emp.Create("E",msg);
return;
}
}
// Batch Operation to update nickname of 2 employees in a single API call
result_get = WebServiceUtilities.ExecuteRESTService("ZC4CODATA_REST_SRV","ZC4CODATA_REST","GET",resourceName,urlParams,headerParams,"","");
if (result_get.Code == "200 " ) // HTTP 200 means call to backend was successfull
{
headerParams.Clear();
headerParam.Name = "x-csrf-token";
headerParam.Value = result_get.HeaderParameters.Where(n=>n.Name == "x-csrf-token").GetFirst().Value; // Read CSRF token value from the response header
headerParams.Add(headerParam);
resourceName = "$batch"; // Define batch as the nedpoint
body = "--batch_guid_01\nContent-Type: multipart/mixed; boundary=changeset_guid_01\n\n--changeset_guid_01\nContent-Type: application/http\nContent-Transfer-Encoding: binary\n\nPATCH EmployeeCollection('00163E6BB3EA1EE8B8EDA3DFB0B052AF') HTTP/1.1\n" + "Content-Type: application/json\nContent-Length: 10000\n\n{\n \"NickName\" : \"BatchMode Update 1\"\n}\n\n--changeset_guid_01\nContent-Type: application/http \nContent-Transfer-Encoding: binary \n\nPATCH " + "EmployeeCollection('00163EAF87791EDB9EA113551554898F') HTTP/1.1\nContent-Type: application/json\nContent-Length: 10000\n\n{\n \"NickName\" : \"BatchMode Update 2\"\n}\n\n--changeset_guid_01-- \n--batch_guid_01--"; // Hardcoded payload with 2 employee ID
var result_batch = WebServiceUtilities.ExecuteRESTService("ZC4CODATA_REST_SRV","ZC4CODATA_REST","POST",resourceName,urlParams,headerParams,"multipart/mixed; boundary=batch_guid_01",body,result_get.Cookies);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
23 | |
22 | |
14 | |
12 | |
10 | |
9 | |
7 | |
7 | |
6 |