Im trying to get values from API request.. there`s 2 user data from the response and I want to get the data of each user and put it on the list.. but my code only store the last user data from the response and abandon the other one..
this is my code
for (User us : userSF){
Purecloud__c ipcccc = new Purecloud__c();
ipcccc.Name = us.Name+'_'+year+'-'+month+'-'+day;
ipcccc.Agent__c = '-';
ipcccc.Agent_Name__c = us.Id;
ipcccc.Queue_Group__c = 'AFI';
ipcccc.External_ID__c = us.SIP__c+'_'+year+'-'+month+'-'+day;
ipcccc.Date__c = date.valueof(startInterval);
ipcccc.Type__c = 'Daily';
tempAgent.add(ipcccc);
}
String JSONstring = '{ "interval": "'+startInterval+'/'+endInterval+'", "timeZone": "Asia/Jakarta", "groupBy": [ "userId" ], "filter": { "type": "or", "predicates": [';
String bodypart1 = '{"type":"dimension","dimension":"userId","operator":"matches","value":"';
String bodypart2 = '"}';
String endString = '] }, "metrics": [ "tSystemPresence" ] }';
String comma = ',';
Integer sizeUser = userSF.size();
for (integer i = 0; i < sizeUser; i++){
JSONstring += bodypart1 + userSF[i].SIP__c + bodypart2;
if(i + 1 < sizeUser){
JSONstring += comma;
}
}
JSONstring += endString;
System.debug('jsonString = ' + jsonString);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('POST');
req.setTimeout(120000);
req.setHeader('content-type', 'application/json');
req.setHeader('Authorization', 'Bearer '+ token);
req.setBody(JSONstring);
try{
HttpResponse res = new HttpResponse();
res = h.send(req);
JSONParser parser;
if (test.isRunningTest()==false) parser = JSON.createParser(res.getBody());
system.debug('*body*'+parser);
while (parser.nextToken() != null) {
if(parser.getCurrentToken() == JSONToken.FIELD_NAME){
while (parser.nextToken() != JSONToken.END_ARRAY) {
if (parser.getText() == 'userId' && parser.nextToken() != null){
purecloudid = parser.getText();
system.debug('*id*'+parser.getText());
}
else if (parser.getText() == 'qualifier'){
parser.nextToken();
status = parser.getText();
if(parser.getText() == 'AVAILABLE') { //AVAILABLE
system.debug('Status Available ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
system.debug('CURRENT POSITION ==>'+parser.getText());
parser.nextToken();
availableSec = parser.getText();
system.debug('Available Second ==>'+parser.getText());
system.debug('String ava ==>'+availableSec);
}
if(parser.getText() == 'AWAY') { //AWAY
system.debug('Status AWAY ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
parser.nextToken();
awaySec = parser.getText();
system.debug('AWAY Second ==>'+parser.getText());
system.debug('String AWAY ==>'+awaySec);
}
if(parser.getText() == 'MEETING') { //MEETING
system.debug('Status MEETING ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
parser.nextToken();
meetingSec = parser.getText();
system.debug('MEETING Second ==>'+parser.getText());
system.debug('String MEETING ==>'+meetingSec);
}
if(parser.getText() == 'TRAINING') { //TRAINING
system.debug('Status TRAINING ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
parser.nextToken();
trainingSec = parser.getText();
system.debug('TRAINING Second ==>'+parser.getText());
system.debug('String TRAINING ==>'+trainingSec);
}
if(parser.getText() == 'BUSY') { //BUSY
system.debug('Status BUSY ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
parser.nextToken();
busySec = parser.getText();
system.debug('BUSY Second ==>'+parser.getText());
system.debug('String BUSY ==>'+busySec);
}
if(parser.getText() == 'BREAK LUNCH') { //BREAK LUNCH
system.debug('Status BUSY ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
parser.nextToken();
breakLunchSec = parser.getText();
system.debug('breakLunchSec Second ==>'+parser.getText());
system.debug('String breakLunchSec ==>'+breakLunchSec);
}
if(parser.getText() == 'BREAK PRAY') { //BREAK PRAY
system.debug('Status BUSY ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
parser.nextToken();
breakPraySec = parser.getText();
system.debug('breakPraySec Second ==>'+parser.getText());
system.debug('String breakPraySec ==>'+breakPraySec);
}
if(parser.getText() == 'BREAK REST') { //BREAK REST
system.debug('Status BUSY ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
parser.nextToken();
breakRestSec = parser.getText();
system.debug('breakRestSec Second ==>'+parser.getText());
system.debug('String breakRestSec ==>'+breakRestSec);
}
if(parser.getText() == 'BREAK OTHER') { //BREAK OTHER
system.debug('Status BUSY ==>'+ parser.getText());
parser.nextToken();
parser.nextToken();
parser.nextToken();
parser.nextToken();
breakOtherSec = parser.getText();
system.debug('breakOtherSec Second ==>'+parser.getText());
system.debug('String breakOtherSec ==>'+breakOtherSec);
}
}
}
}
agentAPI1.SIP__c = purecloudid;
agentAPI1.External_ID__c = purecloudid+'_'+year+'-'+month+'-'+day;
agentAPI1.Available_Sum__c = Decimal.valueof(Integer.valueof(availableSec)/1000);
agentAPI1.Away_Sum__c = Decimal.valueof(Integer.valueof(awaySec)/1000);
agentAPI1.Meeting_Sum__c = Decimal.valueof(Integer.valueof(meetingSec)/1000);
agentAPI1.Training_Sum__c = Decimal.valueof(Integer.valueof(trainingSec)/1000);
agentAPI1.Break_Lunch_Sum__c = Decimal.valueof(Integer.valueof(breaklunchSec)/1000);
agentAPI1.Busy_Sum__c = Decimal.valueof(Integer.valueof(busySec)/1000);
agentAPI1.Break_Pray_Sum__c = Decimal.valueof(Integer.valueof(breakpraySec)/1000);
agentAPI1.Break_Rest_Sum__c = Decimal.valueof(Integer.valueof(breakrestSec)/1000);
agentAPI1.Break_Other_Sum__c = Decimal.valueof(Integer.valueof(breakotherSec)/1000);
upsertAPI1.add(agentAPI1);
}
Set<Purecloud__c> myset = new Set<Purecloud__c>();
List<Purecloud__c> resultAPI1 = new List<Purecloud__c>();
myset.addAll(upsertAPI1);
resultAPI1.addAll(myset);
string s = '';
for (Object r : resultAPI1) s += '\n' + r;
system.debug('resultAPI1 ==>'+s);
//if(tempAgent.size()>0) upsert tempAgent External_ID__c;
//if(upsertAPI1.size()>0) upsert resultAPI1 External_ID__c;
}
catch(exception e){
system.debug('*error*'+e);
}
as you can see theres 2 user data, but there
s only 1 user data saved on list..
this is the response
{ "results": [ { "group": { "userId": "4f798b1d-d27a-4415-b793-02b420586e73" }, "data": [ { "interval": "2018-10-01T01:00:00.000Z/2018-10-07T01:00:00.000Z", "metrics": [ { "metric": "tSystemPresence", "qualifier": "AVAILABLE", "stats": { "sum": 295693992 } }, { "metric": "tSystemPresence", "qualifier": "OFFLINE", "stats": { "sum": 222706008 } } ] } ] }, { "group": { "userId": "ab24ddb8-f1ef-4cca-ae72-9f20f5975332" }, "data": [ { "interval": "2018-10-01T01:00:00.000Z/2018-10-07T01:00:00.000Z", "metrics": [ { "metric": "tSystemPresence", "qualifier": "AWAY", "stats": { "sum": 949 } }, { "metric": "tSystemPresence", "qualifier": "MEAL", "stats": { "sum": 2409 } }, { "metric": "tSystemPresence", "qualifier": "MEETING", "stats": { "sum": 31909 } }, { "metric": "tSystemPresence", "qualifier": "AVAILABLE", "stats": { "sum": 216862072 } }, { "metric": "tSystemPresence", "qualifier": "OFFLINE", "stats": { "sum": 301492112 } }, { "metric": "tSystemPresence", "qualifier": "TRAINING", "stats": { "sum": 7885 } }, { "metric": "tSystemPresence", "qualifier": "BUSY", "stats": { "sum": 1839 } }, { "metric": "tSystemPresence", "qualifier": "BREAK", "stats": { "sum": 825 } } ] } ] } ] }