+ Start a Discussion
Bob.390672021994282E12Bob.390672021994282E12 

How to use public static Integer valueOf(String toInteger)

I was trying to figure out how to use value of in Salesforce. I want to grab the number of string on a field. 

parent = [select id, Name, version_number__c, Text_Field__c from Parent__c where id = :parent.id];

         newParent = parent.clone(false, true);

newparent.Text_Field__c =  valueOf(parent); // doesnt work.I would think that it would bring back the number of chars. 

                 insert newParent;


public static Integer valueOf(String toInteger){
Integer myInt = Integer.valueOf('123');
return myInt;
}


Shingo YamazakiShingo Yamazaki
Dear bryanrevelant1.390672021994282E12

I'm Shingo.

Did you define your original static method named "valueOf", which takes String type argument?

It looks you're passing the sObject "parent" to this method, not String.
shiv@SFDCshiv@SFDC
Integer.valueOf(String numericeString)

above method works when you try to converat a numeric string to a integer number.

Ex : 
String mathMarks = '90' ;
String englishMarks = '80' ;
Integer totalMarks = mathMarks + englishMarks; // This is wrong because we can not add string values
Integer totalMarks = Integer.valueOf(mathMarks) + Integer.valueOf(englishMarks); // This is right because we have done the convesion

Bob.390672021994282E12Bob.390672021994282E12
That makes sense, How then do you call the method too assign it to a field if this is the case?

newparent.Text_Field__c =  valueOf(totalMarks);
shiv@SFDCshiv@SFDC
newparent.Text_Field__c =  String.valueOf(totalMarks);
Bob.390672021994282E12Bob.390672021994282E12
I want to call the method and assign it into a different method. When I try I get the below error Compilation error: Variable does not exist: totalMarks public PageReference ParentChildRecords() { Parent__c newParent; parent = [select id, Name, version_number__c, Text_Field__c from Parent__c where id = :parent.id]; newParent = parent.clone(false, true); newparent.name = countstring(parent); // should return a 10 character random string newparent.Text_Field__c = String.valueOf(totalMarks); // insert newParent; } public static Integer valueOf(String toInteger){ String mathMarks = '90' ; String englishMarks = '80' ; Integer totalMarks = Integer.valueOf(mathMarks) + Integer.valueOf(englishMarks); return totalMarks; }
shiv@SFDCshiv@SFDC
Hi Bryan,

Your code doesn't right. this code having lot of mistakes. 
ParentChildRecords not returning any page reference .

please let me know your whole requirement...

Thanks