function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
j_panchalj_panchal 

how to use string varibale to assign column name while updating records.

Hello,

 

i have a method updateRecords to update selected records from VF page on button click, below is the code

 

    public PageReference updateRecords()
    {
            List<StareAndCompare> selRecords = GetSelectedRecords();
            Contact updateContact;
            String strFieldName;
            
            if(selRecords.size() > 0)
            {
                updateContact = [SELECT c.FirstName, c.LastName, c.Title, c.Email, c.Phone, c.MailingStreet, c.MailingCity,   c.MailingState FROM Contact c WHERE c.Id = :selRecords[0].Id];
                

                for(Integer i=0 ; i<selRecords.size() ; i++)
                {

                     strFieldName = selRecords[i].SFFIeldName;

                     updateContact.strFieldName = selRecords[i].InfoValue;
                }
                
                update updateContact;
            }
            return null;
    }

 

i need to assign the column Name by using a varibale, and the problem is on lines inside the loop i.e. -- 

                     strFieldName = selRecords[i].SFFIeldName;

                     updateContact.strFieldName = selRecords[i].InfoValue;

 

need to assign fieldName from List variable "selRecords" to the string varibale and use it as the column name in next line . For eg. if strFiledName = 'FirstName' then on the line

"updateContact.strFieldName = selRecords[i].InfoValue;" the string value is not used, but it treats strFieldName as columnName.

Please help as i'm stuck i this issue.

 

Thanks in Advance.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
admintrmpadmintrmp

Use SObject instead. Here's a quick example I just wrote to show you how to do this:

 

String strFieldName  = 'FirstName';
String strFieldValue = 'Charlie';

SObject rs = [SELECT Id, FirstName FROM Contact WHERE Id=:recordId];

rs.put(strFieldName, strFieldValue);

update rs;

 

More information on SObject here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm?SearchType=Stem&Highlight=sObject|sObjects|SObject|SObjects|sobjects|sobject|Sobject

All Answers

admintrmpadmintrmp

Use SObject instead. Here's a quick example I just wrote to show you how to do this:

 

String strFieldName  = 'FirstName';
String strFieldValue = 'Charlie';

SObject rs = [SELECT Id, FirstName FROM Contact WHERE Id=:recordId];

rs.put(strFieldName, strFieldValue);

update rs;

 

More information on SObject here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm?SearchType=Stem&Highlight=sObject|sObjects|SObject|SObjects|sobjects|sobject|Sobject

This was selected as the best answer
j_panchalj_panchal

Thanks a lot, it worked :)