Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Re: How to use SObject columns as parameter?
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
How to use SObject Fields as parameter?
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-09-2012 11:06 PM - edited 12-10-2012 06:34 PM
Hi!
I have a set of Fields should be update, please teach me how to do ,thanks!
Set<String> Fieldset = new Set<String>{'Fax','Website'};
account a1 = [select id,Fax,Website from account limit 1];
account a2 = [select id,Fax,Website from account limit 1 offset 1 ];
for (string Field1 : Fieldset ) {
a1.Field1 = String.valueof(a2.Field1); // Error : Invalid Field1 for SObject Account
//update a1;
}
don't work in a loop
for(String field :fields)
{
accs[0].get(field) = accs[1].get(field); //Expression cannot be assigned (the get() Arguments should be Integer i)
accs[0].field = accs[1].field; //Invalid field field for SObject Account
}and don't work just as a parameter
list<String> fields2 = new list<String>{'Fax','Website'};
accs[0].fields2[0] = accs[1].fields2[0]; //Invalid field fields2 for SObject Account
can work when set the exact Field Name
accs[0].Fax = accs[1].Fax; //List SObject Type
accs.Fax = accs.Fax; //SObject Type
Regards
Solved! Go to Solution.
Re: How to use SObject columns as parameter?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-10-2012 12:59 AM
My Blog
Kiran
Re: How to use SObject columns as parameter?
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-10-2012 02:10 AM - edited 12-10-2012 03:18 AM
Still don't work , you can test the code below:
Set<String> Fieldset = new Set<String>{'Fax','Website'};
account a1 = [select id,Fax,Website from account limit 1];
account a2 = [select id,Fax,Website from account limit 1 offset 1];
for (string Field1 : Fieldset ) {
a1.Field1 = String.valueof(a2.Field1) ; // Error : Invalid Field1 for SObject Account
//update a1;
}
Re: How to use SObject Fields as parameter?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-10-2012 06:29 AM
Hi IceEngine
this has bad practice written all over it, but the following code will do what you wanted:
Set<String> fields = new Set<String>{'Fax','Website'};
String q = 'Select ';
for(String field : fields)
{
q += field + ',';
}
q = q.subString(0.q.length()-1);
q += ' From Account limit 2';
List<Account> accs = Database.query(q);
for(String field :fields)
{
accs[0].get(field) = accs[1].get(field);
}
update accs[0];
Re: How to use SObject Fields as parameter?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-12-2012 01:22 AM
Thank you Noam.dgani,
accs[0].get(field) = accs[1].get(field); // Will turnout an Error : Expression cannot be assigned
for sObject, This is correct :
a1.put ( field, a2.get (field) );
Re: How to use SObject Fields as parameter?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-12-2012 03:56 AM
yep.
right about the .put part.
glad to help

