I have this json object coming in for boxes:
Boxes:
[ { count: 5, Fragile: true },
{ count: 10, Fragile: false } ],
I have box records that need to be updated based on if they are fragile or not and that they haven’t been used. Basically when that json comes through, we want to update existing box records and send the boxes' information back to our 3rd party.
public static void createPack() {
pack = new Package__c();
if(packageWrapper.boxesNeeded != null && packageWrapper.boxesNeeded.size()>0){
pack.Extra_Boxes__c = true;
}
insert pack;
List<Decimal> countList = new List<Decimal>();
List<Boolean> fragilelList = new List<Boolean>();
for(BoxesNeeded boxesNeeded : packageWrapper.boxesNeeded){
if( packageWrapper.boxesNeeded != null && packageWrapper.boxesNeeded.size()>0){
numberOfBoxes = packageWrapper.boxesNeeded.size();
countList.add(boxesNeeded.weight);
fragileList.add(boxesNeeded.fragile);
}
List<box__c> boxesToUse = [SELECT ID, width__c, height__c, length__c FROM box__C WHERE Used__C = FALSE AND Fragile__c IN : fragileList LIMIT :numberOfBoxes];
for(Integer i = 0; i < boxesToUse.size(); i++){
box__C BoxToUse = BoxesToUse.get(i);
BoxToUse.Package__c = Pack.id;
BoxToUse.Date_Used__c = Date.today();
for(Integer c = 0; c < countList.size(); c++){
BoxToUse.count__c =countList.get(c);
}
}
upsert BoxesToUse;
for(Box__c BoxToUse : BoxesToUse){
boxWrappers.add(new boxWrapper(boxToUse.width__c, boxToUse.height__c, boxToUse.length__c,,boxToUse.fragile__c)
}
}
}
My wrappers:
public class BoxesNeeded{
public Decimal count {get;set;}
public Boolean fragile {get;set;}
public BoxesNeeded(Decimal count, Boolean fragile){
this.count = count;
this.fragile = fragile;
}
}
global class boxWrapper{
Integer width {get;set;}
Integer height {get;set;}
Integer length {get;set;}
Boolean fragile {get;set;}
public BoxWrapper(Integer width, integer height, integer length, boolean fragile){
this.width= width;
this.height = height;
this.length = length;
this.fragile = fragile;
}
}
After I retrieve the box records we send back a json to our third party of the records it should look like this(however it includes twice the amount. this snippet does not include it. just showing what it should look like):
result.body { success: true,
shipId: ’SH-0343452',
Boxes:
[ { width: 12,
height:8,
length:12,
fragile: true,
Count: 5 },
{ width: 8,
Height:10,
length:12,
fragile: false,
Count: 10
},
],
New Edit: After reviewing what we planned on doing with this JSON object:
Boxes:
[ { count: 5, Fragile: true };
{count: 10, Fragile: false};
I have gotten a clearer picture on what needs to be done.
With the JSON object above we will then want to update existing box records in our organization. Which is why we need to query for existing box records that are not used that are either fragile vs non fragile.
So this json object shows two box records, with a count of 5 and a count of 10, one is fragile and one is not.
SO we will be updating 2 box records that have not been used yet.
So Box 1:
Count__c = 5; <=field that needs to be updated, grab the first count
(5) from json
Fragile__c = true<= we are not updating this, we need to find a un-used
box record that is fragile
Box 2:
Count__c = 10; <=field that needs to be updated, grab the second
count(10) from json
Fragile__c = false<= we are not updating this, we need to find a un-
used box record that is not fragile
Once that is updated, we will then return back a json object to our third party with this information about the box: Width Height length Fragile Count
So to do so, I have created a wrapper class for Boxes Needed:
public class BoxesNeeded{
public Decimal count {get;set;}
public Boolean fragile {get;set;}
public BoxesNeeded(Decimal count, Boolean fragile){
this.count = count;
this.fragile = fragile;
}
}
Then to send the box information once updated to our 3rd party, the boxWrapper will look like this:
global class boxWrapper{
Integer width {get;set;}
Integer height {get;set;}
Integer length {get;set;}
Boolean fragile {get;set;}
Integer count {get; set;}
public BoxWrapper(Integer width, integer height, integer length, boolean
fragile, integer count){
this.width= width;
this.height = height;
this.length = length;
this.fragile = fragile;
this.count = count;
}
}
I have added new data types to capture non-fragile vs fragile counts of boxes needed:
Public Integer fragileCount {get; set;}
Public integer nonFragileCount {get;set;}
Then like mentioned below, my For Loop will count how many of each I need(changed var name to boxNeeded to differentiate from Object BoxesNeeded:
for(BoxesNeeded boxNeeded : packageWrapper.boxesNeeded){
if(boxNeeded.fragile == true){
fragileCount ++;
}
else if(boxNeeded.fragile == false){
nonFragileCount ++;
}
}
Then the two queries also mentioned below will bring back the box records I need to update:
List<Box__c> fragileBoxesToUse = [SELECT ID, width__c, height__c,
length__c FROM box__C WHERE Used__C = FALSE AND Fragile__c =true LIMIT :fragileCount FOR UPDATE];
List<Box__c> nonFragileBoxesToUse = [SELECT ID, width__c, height__c,
length__c FROM box__C WHERE Used__C = FALSE AND Fragile__c = false LIMIT :nonFragileCount FOR UPDATE];
Now to loop through the two lists:
For(Box__c boxToUse : fragileBoxesToUse){
BoxToUse.Package__c = Pack.id;
BoxToUse.Date_Used__c = Date.today();
BoxToUse.Count__c = ??? I need to get the count from the JSON
boxWrappers.add(new boxWrapper(boxToUse.width__c,
boxToUse.height__c, boxToUse.length__c,,boxToUse.fragile__c,
boxToUse.count__c);
}
upsert FragileBoxesToUse;
For(Box__c boxToUse : nonfragileBoxesToUse){
BoxToUse.Package__c = Pack.id;
BoxToUse.Date_Used__c = Date.today();
BoxToUse.Count__c = ??? I need to get the count from the JSON
boxWrapper(boxToUse.width__c, boxToUse.height__c,
boxToUse.length__c,,boxToUse.fragile__c, boxToUse.count__c);
}
upsert nonFragileBoxesToUse;
Two more things, to combine my two List queries would I need to write a dynamic soql query? Also after clarifying what "Count" is, how would I grab the correct "count" to update my box records? Count does not represent how many box records that need to be updated but it represents a field on the box record to indicate how many items will go in the box. (Count__c)
So in all, the goal from this Json is to update existing box records and return the updated box records to our third party.
So in this json example we will be updating two box records and then return two box record's information.
Recent Edit: After changing it to List nonfragileCounts, when i grab nonFragileCount.get(i) in my loop to update the count field i receive:
List index out of bounds: 0
for(BoxesNeeded boxNeeded : packageWrapper.boxesNeeded){
if(boxNeeded.fragile == true){
fragileCount.add(boxNeeded.count);
}
else if(boxNeeded.fragile == false){
nonFragileCount.add(boxNeeded.count);
}
}
List<Box__c> nonFragileBoxesToUse = [SELECT ID, width__c, height__c,
length__c FROM box__C WHERE Used__C = FALSE AND Fragile__c = false
LIMIT :nonFragileCount.size() FOR UPDATE];
for(Integer i = 0; i < nonFragileBoxesToUse.size(); i++){
Box__c BoxToUse = nonFragileBoxesToUse.get(i);
BoxToUse.Package__c = Pack.id;
BoxToUse.Date_Used__c = Date.today();
BoxToUse.Count__c = nonFragileCount.get(i);
}
upsert nonFragileBoxesToUse;