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
JocsanJocsan 

Accessing sObject Fields Problem

Hi,

I am using the following code and myAcct.Type is returning null. Why? I am able to update the Type field but I can't read it.

    for (opportunity o : Trigger.new) {
       
        String accountid = o.AccountId;
        Account myAcct = new Account(Id = accountid);
        System.debug(myAcct.Type);
       
        if ((o.StageName == 'Closed Won') && (myAcct.Type != 'Customer')) {           
            myAcct.Type = 'Customer';
            update myAcct;
        }   
    }

Any idea?

Thanks, Jocsan
SuperfellSuperfell
because you didn't set it,

Account myAcct = new Account(Id = accountid);
System.debug(myAcct.Type);

every field value on myAcct except Id (which you set in the constructor) is going to be null because you haven't given then any values.
JocsanJocsan
Thanks for the reply.

So, I can't use the condition, right?

if ((o.StageName == 'Closed Won') && (myAcct.Type != 'Customer')) {...

Is SOQL the only option I have to find out the currect Type value?

Jocsan
SuperfellSuperfell
Yes, if you want the value of any field on account from a trigger that's not for account, you'll need to use a query to get them.
JocsanJocsan
Thanks!