Solved

Client Script: Getting the row from response of createInsertRequest

  • 22 August 2022
  • 4 replies
  • 134 views

Userlevel 4
Badge +11

Hi,

 

what function can be called to get row from response of createInsertRequest?

 

I have script to insert person_cal_except table.  The aim is to get row and exception_id of the new created record..

 

My script is like this:

var newRec = {};
        newRec.person_id = personId;
        newRec.exception_type = exceptionType;
        newRec.start_dttm = startDttm;
        newRec.end_dttm = endDttm;
        newRec.user_def1 = approved;
        newRec.available = available;
        var personCalExceptReq = createInsertRequest('person_cal_except', newRec);
        var response = executeRequest(personCalExceptReq);
    
        if (response['$type'] == 'ErrorResponse')      
          {
            displayUserToast(getMessage('cUnabletoAddCalException','Information') + personId + '  ' + response.message,'error'); 
          }
          else
          {
              // alert('response' + size(response));
                exceptionId = ?????? ;
                // alert('exception Id: ' + exceptionId);
                  
                               
                displayUserToast(getMessage('cCalExceptionCreated','Information') + exceptionId, 'INFO'); 
               }

 

Thanks in advance for your response,

~Juni

icon

Best answer by Saranga Amaraweera 23 August 2022, 08:24

View original

4 replies

Userlevel 6
Badge +26

Hey @JuniSihombing 

I haven’t tried it myself, but try one of the following

  • response.metrix_run_log_id
  • response.error

Cheers!

Userlevel 4
Badge +11

Hi @Shneor Cheshin,

thank you for your feedback.

I tried using  response.metrix_run_lod_id and response.error . Unfortunately both are undefined parameters.

~Juni

Userlevel 7
Badge +22

Hi @JuniSihombing ,

I have tested your client script with some sample data and was able to access the result in the way mentioned below. 

var startDttm = new Date();
var endDttm=startDttm+days(1);
var available = "N";
var personId = getControlValue('person', 'person_id');
var last_name = getControlValue('person', 'last_name');

if (last_name != null && last_name=='TEST')
{
var newRec = {};
newRec.person_id = personId;
newRec.exception_type = "SICK";
newRec.start_dttm = startDttm;
newRec.end_dttm = endDttm;
newRec.available = available;
var personCalExceptReq = createInsertRequest('person_cal_except', newRec);
var response = executeRequest(personCalExceptReq);

if (response['$type'] == 'ErrorResponse')
{
displayUserToast(getMessage('cUnabletoAddCalException','Information') + personId + ' ' + response.message,'error');
}
else if (response != null)
{
calExceptArray = normalizeToArray(response);
//if we have only one result from the insertion, simply add this
var myRow = calExceptArray[0];
alert('The Cal Except Id is: ' + myRow.item['exception_id']);
//if we have multiple results from the insertion, then traverse through
for(var i=0; i < size(calExceptArray); ++i)
{
var theRow = calExceptArray[i];
alert('The cal except id generated from the loop is: ' + theRow.item['exception_id']);
}

}

}

As mentioned, if your response returns only one result, then you do not need to traverse through the elements. You can simply alter your script as below

calExceptArray = normalizeToArray(response);
var myRow = calExceptArray[0];
alert('The cal except id is ' + myRow.item['exception_id']);

Hope this helps.

Userlevel 4
Badge +11

Thank you @Saranga Amaraweera .

The solution you gave works!

 

cheers,

~Juni

Reply