-1
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);
    }
}
5
  • 2
    The 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 F
    Mar 11, 2021 at 13:07
  • obj = new student__c(Name='Test',high_school_phone__c='998877');
    – sai
    Mar 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?
    – sai
    Mar 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). Mar 11, 2021 at 13:20
  • yes i got it . Thank you
    – sai
    Mar 11, 2021 at 13:22

1 Answer 1

0

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);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .