public class DemoSObject {
public void Demomethod(){
SObject obj;
obj = new Account(Name = 'Test', phone = '998877');
Account acc = new Account();
acc.Name = 'Test' ;
acc.phone = '889977';
system.debug('acc =>' +acc);
obj = new student__c(Name='Test',high_school_phone__c='998877');
//std.Name = 'Test';
//std.high_school_phone__c = '998877';
student__c std = (student__c)obj;
system.debug('std => '+std);
}
}
-
2The error is straightforward here. What part of it are you having trouble understanding? What have you tried to do to resolve the issue so far?– Derek FMar 11, 2021 at 13:07
-
obj = new student__c(Name='Test',high_school_phone__c='998877');– saiMar 11, 2021 at 13:11
-
I think i went wrong in the above statement. can you tell me is any other way to assign values?– saiMar 11, 2021 at 13:12
-
If high_school_phone__c is a Number field (and not a text field, like the standard Phone datatype), you will need to assign without single quotes (like high_school_phone__c=998877).– Menachem ShanowitzMar 11, 2021 at 13:20
-
yes i got it . Thank you– saiMar 11, 2021 at 13:22
1 Answer
To access a field on sObject (aside from Id), you need to use the get and put methods:
obj = new student__c(Name='Test',high_school_phone__c='998877');
std.put('Name','Test');
std.put('high_school_phone__c','998877');
Based on your description, it sounds like you made High School Phone a number field, not a Phone field, so you can't use quotes:
obj = new student__c(Name='Test',high_school_phone__c=998877);
std.put('Name','Test');
std.put('high_school_phone__c',998877);