+ Start a Discussion
neeedhelpneeedhelp 

How to get values into a list

public void save()

{

for (Integer j = 0; j < selectcategory.size(); j++)
         {

for (Integer i = 0; i< selectlayout.size(); i++)
         {
          system.debug('valuedata'+selectlayout[i].label1);
          str = '<widgetnode><widget Name='+ selectcategory[j].label +'><Imag='+ selectlayout[i].Image1 +' Field='+ selectlayout[i].label1 +'  result='+ selectlayout[i].value +'/></widget></widgetnode>' ;
         system.debug('stringgg'+str);     
       }
     }

}

     widgetpro.Displayed_Widgets__c = str;

}

  I'm getting only last value into str......but in debug I can get all the values.....How should I come out of this

Best Answer chosen by Admin (Salesforce Developers) 
tomcollinstomcollins

What if you change your string assignment to str = str + '<widgetnode...

 

In your current code, you're replacing str on every pass through your loop.

 

Also note that you're not creating valid XML, since your attributes aren't properly quoted.  Instead of:

 

'<widget name=' + foo + '>'

 

You need to use:

 

'<widget name="' + foo + '">'

 

(Hopefully you can see the addition of a quote (") inside the strings before and after foo.)

 

 

All Answers

sanjdevsanjdev

Hi,

 

Try the below  code snippet.

 

List<String> lstStr = new List<String>();
public void save()
{
for (Integer j = 0; j < selectcategory.size(); j++)
{
for (Integer i = 0; i< selectlayout.size(); i++)
{
system.debug('valuedata'+selectlayout[i].label1);
str = '<widgetnode><widget Name='+ selectcategory[j].label +'><Imag='+ selectlayout[i].Image1 +' Field='+ selectlayout[i].label1 +' result='+ selectlayout[i].value +'/></widget></widgetnode>' ;
system.debug('stringgg'+str);
lstStr.add(str);
}
}
}
for(Integer i =o;i<lstStr.size();i++){

widgetpro.Displayed_Widgets__c = lstStr[i].str;
}
}

 

Mark it as resolved if it helps you.

Sanj

neeedhelpneeedhelp

Hi Sanjdev,

 

   Thanks for ur quick reply........I'm getting error like this don't know why....please help me

Initial term of field expression must be a concrete SObject:  

 

widgetpro.Displayed_Widgets__c = lstStr[i].str;

 

and getting error when i used

widgetpro.Displayed_Widgets__c = lstStr[i];


System.DmlException: Insert failed. First exception on row 0; first error: STRING_TOO_LONG, Displayed Widgets: data value too large: null&lt;widget Name=Social Network&gt;&lt;Imag=null Field=Widget Name result=tyh/&gt;&lt;/widget&gt;&lt;widget Name=Social Network&gt;&lt;Imag=http://t2.gstatic.com/images?q=tbn:ANd9GcQPw1U3ihkGF2nkOzBC8EHA-2A8U58q8Bf9tQxaB1t-QhpsP__9 Field=Iframe Code result=fghfgjh/&gt;&lt;/widget&gt;&lt;widget Name=Google Maps&gt;&lt;Imag=null Field=Widget Name  ................& so on

 

How should I get rid of this.......


Baktash H.Baktash H.

I think the String you try to insert is too long for the field. Go into the field description and look whats the maximum size of chars which are allowed in the field.

tomcollinstomcollins

What if you change your string assignment to str = str + '<widgetnode...

 

In your current code, you're replacing str on every pass through your loop.

 

Also note that you're not creating valid XML, since your attributes aren't properly quoted.  Instead of:

 

'<widget name=' + foo + '>'

 

You need to use:

 

'<widget name="' + foo + '">'

 

(Hopefully you can see the addition of a quote (") inside the strings before and after foo.)

 

 

This was selected as the best answer
neeedhelpneeedhelp

Thanks Tom....It helped me a lot