Find your content:

Search form

You are here

Problem using [].substring()

 
Share

I am having some issues using [].substring(). I am limited by using it. I would like to use a different approach because the qty can vary on length and this is not working for this solution. I will appreciate any help!

here is the System.debug

 USER_DEBUG [241]|DEBUG|(relatedObjectRow:[obj=NRProducts__c:{Name=105826985,  Id=a0Cf00000014LzCEAU},  selected=true,  testfield=999])

here is the button code:

public void cloneSelectedObjects2(){

 SOStoupload = new List<Purchase_Order_Details__c>();
  List<String> lista= new  List<String>();

  for (relatedObjects relatedObject : objectChildren)
 {
for (relatedObjectRow row : relatedObject.objectRows) 
{
    if (row.selected)
    {

        lista.add(row.toString());

    } 
}
//here for loop to get values
for (String str : lista)
{
     String[] inputvalues = str.split(',');

  System.debug(inputvalues);

              //create new pODetails
                Purchase_Order_Details__c pod= new Purchase_Order_Details__c(
                Purchase_Order__c = objecttext,
                NRProducts__c =inputvalues[1].substring(4,22),
                Qty_NR__c = Decimal.valueof(inputvalues[3].substring(14,17)) //values from inputField (testField)

                );


                SOStoupload.add(pod);

  }
  }
 insert SOStoupload;  

}

wrapper class here

    public class relatedObjects
 {
  public List<relatedObjectRow> objectRows { get; set; }
public String pluralLabel      { get; set; }
public String relatedName{get;set;}
public String relatedId{get;set;}


public relatedObjects(List<relatedObjectRow> objectRows, 
                      String pluralLabel,
                      String relatedFieldName)

{
  this.objectRows = objectRows;
  this.pluralLabel = pluralLabel;
  this.relatedName = relatedName;
 this.relatedId = relatedId;

   }   
 }     


 public class relatedObjectRow
 {
public sObject obj      { get; set; }
public Boolean selected { get; set; }
public String testfield {get;set;}
public String relatedId{get;set;}

public relatedObjectRow(Sobject obj)
{
  this.obj      = obj;
  // All object rows are selected by default.
  this.selected     = true;
}

 public String getName ()
     {
  try{
    return '' + obj.get('Name');
  } catch (Exception e){
    return '';
  } 
  }

    public String getId ()
     {
  try{
    return '' + obj.get('Id');
  } catch (Exception e){
    return '';
  } 
  }

 }

here is the solution, someone help me with this(Get multiple values from VF Page and pass them to the class):

  public void cloneSelectedObjects2(){

SOStoupload = new List<Purchase_Order_Details__c>();

for (relatedObjects relatedObject : objectChildren) {
    List<relatedObjectRow> selectedRelatedRows = new List<relatedObjectRow>();

    for (relatedObjectRow row : relatedObject.objectRows) {
        if (row.selected) {
            selectedRelatedRows.add(row);
        } 
    }

    //here for loop to get values
    for (relatedObjectRow row : selectedRelatedRows) {

        //create new pODetails
        Purchase_Order_Details__c pod = new Purchase_Order_Details__c();

        // not sure where this objecttext is coming from
        pod.Purchase_Order__c = objecttext;

        // use the sObject.get() to grab the ID field value from the inner object
        pod.NRProducts__c = row.obj.get('Id');

        // get the 'entered value' from the wrapper class and convert to decimal
        pod.Qty_NR__c = Decimal.valueOf(row.EnteredValue);


        SOStoupload.add(pod);

    }
}

insert SOStoupload;  

}


Attribution to: Carlos

Possible Suggestion/Solution #1

One approach is to change

List<String> lista= new  List<String>();

to be a list of lists like:

List<List<String>> lista= new  List<List<String>>();

Then when you are iterating through your relatedObject.objectRows, you could add each field on the object (obj, selected, testfield, relatedId) into the list as it's own string.

That way instead of splitting the string and using .substring to access the information you want, you can just index into the array for each field.


Attribution to: Kyle
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33987

My Block Status

My Block Content