Is it posible to convert a string who has a value of field name into a salesforce object field. For example I have a String strvalfieldname = 'Desription__c'; and i want it to convert into the Product custom field Description__c. is it possible ? .Because in my case the description__c field is a text area and we cannot filter and include a text area into a where clause.

HERE IS MY CODE BUT THIS IS AN ERROR..Method does not exist or incorrect signature: [Object].contains(String)

    String qryTA = 'Description__c';
    String qry = 'Select Id,'+qryTA+' FROM Product__c WHERE '+
    for(Product__c proj : Database.query(qry)){

        if((string) proj.get(qryTA).contains(textareaval)) {

  • Also, FYI you have a bug in your query string. You need a ',' and a space after Id
    – NSjonas
    Sep 4 '15 at 6:01

Your class doesn't compile because you need to case it to a string BEFORE calling contains().

 String myTextArea = (String) proj.get(qryTA);
 if(String.isNotEmpty(myTextArea) && myTextArea.contains(textareaval) ) { 
  • We cannot Include a text area field in where clause.. Try it yourself.. Sep 4 '15 at 6:09
  • the way your doing it should work fine then
    – NSjonas
    Sep 4 '15 at 6:16
  • getting me an error of this **Line: 114, Column: 1: Attempt to de-reference a null object ** Sep 4 '15 at 8:10
  • @DanrylTigolCarpio what line is 114?
    – NSjonas
    Sep 4 '15 at 18:07
  • @DanrylTigolCarpio I'm guessing it was the line in question. I updated my answer to check for null before calling contains.
    – NSjonas
    Sep 4 '15 at 18:10

I hope this helps,

String textareaval = 'matching value';
String fieldName = 'Name';
String qry = 'Select Id,'+fieldName+' FROM Account ';

for(Account acct : Database.query(qry)){
    String val = (string)acct.get(fieldName);
    if(val.contains(textareaval)) {
        system.debug(' DEBUG HERE : '+(string) acct.get(fieldName));
  • getting me this error **Line: 114, Column: 1 System.NullPointerException: Attempt to de-reference a null object ** Sep 4 '15 at 8:10
  • This is because you "(string) proj.get(qryTA)" return null and you cannot perform .contains() on a null object.
    – Hasantha
    Sep 7 '15 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.