cancel
Showing results for 
Search instead for 
Did you mean: 

Script use in SAP Digital Manufacturing

ksinghal
Explorer
0 Kudos

Hi Experts,

I am using Script in SAP DMC production process where I have to find a labor name from StructureArray. below I have mentioned Structure Array format and Condition what I have used.

Structure Array

[{"supervisor":"abc@gmail.com","labor":"def@gmail.com""shiftModel":"NIGHT","startDate":"2023/12/27","endDate":"2023/12/28","startTime":"22:00","endTime":"06:00","duration":"8h"},

{"supervisor":"abc@gmail.com","labor":"xyz@gmail.com"shiftModel":"MORNING","startDate":"2023/12/27","endDate":"2023/12/27","startTime":"06:00","endTime":"14:00","duration":"8h"},

{"supervisor":"abc@gmail.com","labor":"jkl@gmail.com","shiftModel":"MORNING","startDate":"2023/12/27","endDate":"2023/12/27","startTime":"06:00","endTime":"14:00","duration":"8h",}]

Condition:- If "startTime":"06:00","endTime":"14:00" is this then I need to print labor details which is available in this StructureArray.

Code:-

var i;

for (var i=0; i<$input.LaborReport.length;i++)

{

if($input.SStartTime >="22:01" && $input.SEndTime <= "06:00")

{

$output.FetchUser = $input.LaborReport[i].labor;

}

if ($input.SStartTime >="06:01" && $input.SEndTime <= "14:00")

{

$output.FetchUser = $input.LaborReport[i].labor;

}

if ($input.SStartTime >="14:01" && $input.SEndTime <= "22:00")

{

$output.FetchUser = $input.LaborReport[i].labor;

}

}

But I am only getting value which is available at array[0]. Can anyone please help me on this.

Thanks & Regards


Accepted Solutions (1)

Accepted Solutions (1)

benwildridge
Explorer
0 Kudos

Try this, I have just tested it and it returns all 3 entries [ 'def@gmail.com', 'xyz@gmail.com', 'jkl@gmail.com' ] which I presume is what we would expect given the test data

var matchingUsers = []; // Array to store matching labor details

for (var i = 0; i < $input.LaborReport.length; i++) {

    var laborStartTime = $input.LaborReport[i].startTime;
    var laborEndTime = $input.LaborReport[i].endTime;

    if (
            (laborStartTime >= "06:00" && laborEndTime <= "14:00") || 
            (laborStartTime >= "14:00" && laborEndTime <= "22:00") || 
            (laborStartTime >= "22:00" || laborEndTime <= "06:00") 
    ) {
        matchingUsers.push($input.LaborReport[i].labor);
    }
}
$output.FetchUsers = matchingUsers;
benwildridge
Explorer
0 Kudos

Apologies, I'm struggling to understand what the actual output and format you want.

Given the example data

Structure Array

[{"supervisor":"abc@gmail.com","labor":"def@gmail.com""shiftModel":"NIGHT","startDate":"2023/12/27","endDate":"2023/12/28","startTime":"22:00","endTime":"06:00","duration":"8h"},

{"supervisor":"abc@gmail.com","labor":"xyz@gmail.com"shiftModel":"MORNING","startDate":"2023/12/27","endDate":"2023/12/27","startTime":"06:00","endTime":"14:00","duration":"8h"},

{"supervisor":"abc@gmail.com","labor":"jkl@gmail.com","shiftModel":"MORNING","startDate":"2023/12/27","endDate":"2023/12/27","startTime":"06:00","endTime":"14:00","duration":"8h",}]

You have provided the example if statement for each shift time and but then you are also looping through multiple records. What are you expecting if there are multiple records that meet mulitple criteria? Your original query would only ever return 1 record because you hit the first if statement for the first record and write it. Then for the next record it would find it another time but overwrite the $output.FetchUser entry with the new record rather than adding to it.

Are you expecting and output of [{SHIFT N: *entries*}, {SHIFT M: *entries*}, {SHIFT A: *entries*}]

Or do you want to work out which shift to find based on an input?

ksinghal
Explorer
0 Kudos

Hi Ben,

My issue is resolved in Previous step only.

I have use wrong condition over there. Sorry for inconvenience for it.

Thanks & regards

benwildridge
Explorer
0 Kudos

Excellent news

Answers (1)

Answers (1)

benwildridge
Explorer
0 Kudos

Hi Kartic,

If I am understanding your requirement correctly, you are wanting to return any entries from the structure array that match the start time of 6:00 and end time of 14:00?

If so does this do what you require?

var startTime = "06:00";
var endTime = "14:00";
var matchingUsers = []; // Array to store matching labor details

for (var i = 0; i < $input.LaborReport.length; i++) {
    var laborStartTime = $input.LaborReport[i].startTime;
    var laborEndTime = $input.LaborReport[i].endTime;

    if (laborStartTime === startTime && laborEndTime === endTime) {
        matchingUsers.push($input.LaborReport[i].labor);
    }
}

$output.FetchUsers = matchingUsers; // Store all matching labor details in output
If not let me know and I'll try to assist