Skip to main content
Notice removed Authoritative reference needed by J.koh
Bounty Ended with David Reed's answer chosen by J.koh
added 569 characters in body
Source Link
J.koh
  • 189
  • 1
  • 2
  • 18

Another note for this lineRecent Edit: I changed fragileCountAfter changing it to a List from just an IntegernonfragileCounts, 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;

Now the JSONParser response is false instead of true when parsing in my test class. When i remove the if statement the response is true. snippet of test:

packService.JSONResponse response =packService.consumeJSON(); System.assertEquals(true, response.success);

Another note for this line: I changed fragileCount to a List from just an Integer.

  if(boxNeeded.fragile == true){
        fragileCount.add(boxNeeded.count);
    }

Now the JSONParser response is false instead of true when parsing in my test class. When i remove the if statement the response is true. snippet of test:

packService.JSONResponse response =packService.consumeJSON(); System.assertEquals(true, response.success);

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;
added 318 characters in body
Source Link
J.koh
  • 189
  • 1
  • 2
  • 18

Another note for this line: I changed fragileCount to a List from just an Integer.

  if(boxNeeded.fragile == true){
        fragileCount.add(boxNeeded.count);
    }

Now the JSONParser response is false instead of true when parsing in my test class. When i remove the if statement the response is true. snippet of test:

packService.JSONResponse response =packService.consumeJSON(); System.assertEquals(true, response.success);

Another note for this line: I changed fragileCount to a List from just an Integer.

  if(boxNeeded.fragile == true){
        fragileCount.add(boxNeeded.count);
    }

Now the JSONParser response is false instead of true when parsing in my test class. When i remove the if statement the response is true. snippet of test:

packService.JSONResponse response =packService.consumeJSON(); System.assertEquals(true, response.success);

added 3764 characters in body
Source Link
J.koh
  • 189
  • 1
  • 2
  • 18

Two things,New Edit: After reviewing what we planned on doing with this JSON object:

Boxes:
    [ { count: 5, Fragile: true };
    {count: 10, Fragile: false};

I do not gethave gotten a clearer picture on what needs to be done.

With the return value for fragile correctly, it is always false even though someJSON object above we will then want to update existing box records have truein our organization. When aWhich 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 comes overshows two box records, with at leasta count of 5 and a count of 10, one is fragile box it always duplicates the amountand one is not. For example if

SO we sendwill be updating 2 box information overrecords 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, it sends 4we will then return back. a json object to our third party with this information about the box: I think it is because of “numberOfBoxes”Width Height length Fragile Count

So to do so, it retrievesI 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 2 records but dupilicates because fragile comes over once as true andbox information once as falseupdated to our 3rd party, which is why the result returns 4, if that makes sense.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;
   }
}

How can I retrieve the correct numberhave added new data types to capture non-fragile vs fragile counts of box records depending onboxes needed:

 Public Integer fragileCount {get; set;}
 Public integer nonFragileCount {get;set;}

Then like mentioned below, my json object and returnFor 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 our third party?update:

  Boxes 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: 5from the JSON 
    boxWrappers.add(new boxWrapper(boxToUse.width__c, Fragile: 
 true }  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: 10from the JSON 
    boxWrapper(boxToUse.width__c, Fragile:boxToUse.height__c, false
    boxToUse.length__c,,boxToUse.fragile__c, boxToUse.count__c);

}
upsert ]nonFragileBoxesToUse;

Basically with the json aboveTwo more things, to combine my two List<Box__c> 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 going to update two existing box records in my organizationand return the updated box records to our third party. It

So in this json example we will then return one true fragilebe updating two box records and one false fragilethen return two box with their dimensionsrecord's information.

Two things, I do not get the return value for fragile correctly, it is always false even though some box records have true. When a json object comes over with at least one fragile box it always duplicates the amount. For example if we send 2 box information over, it sends 4 back. I think it is because of “numberOfBoxes”, it retrieves the 2 records but dupilicates because fragile comes over once as true and once as false, which is why the result returns 4, if that makes sense.

How can I retrieve the correct number of box records depending on my json object and return the records to our third party?

  Boxes: 
  [ { count: 5, Fragile: true },
  { count: 10, Fragile: false } ]

Basically with the json above, it is going to update two existing box records in my organization. It will then return one true fragile box and one false fragile box with their dimensions.

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<Box__c> 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.

Notice added Authoritative reference needed by J.koh
Bounty Started worth 50 reputation by J.koh
added 245 characters in body
Source Link
J.koh
  • 189
  • 1
  • 2
  • 18
Loading
added 245 characters in body
Source Link
J.koh
  • 189
  • 1
  • 2
  • 18
Loading
Source Link
J.koh
  • 189
  • 1
  • 2
  • 18
Loading