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
MDXHikerMDXHiker 

Update issue on standard obj

I have a apex class which is called by a trigger on a custom object.
i.e a condition is met on a custom object which fires the trigger which in turn calls the class.

The class is trying to update the custom amount fields on an Oppty obj

Code:
public class AmountsUpdate {
public void create(String oppId,Decimal amount_To_Adjust,String reason)
{try{
Opportunity Oppty=new Opportunity(Id=oppId);
if((reason=='X')||(reason=='Y'))
Oppty.Base_Amount__c=Oppty.Base_Amount__c-amount_To_Adjust;
else
Oppty.Base_Amount__c=Oppty.Base_Amount__c+amount_To_Adjust;
update Oppty;
} code continues

 I get a null pointer expection when I am trying to add the amount to the base amount.
I am trying to get the Oppty whose Id =OppId in the creation of the object

Is this not a valid in Apex?
 
Any help would be appreciated. Thanks
philbophilbo
Hey,

I think you're trying to update fields in the Opportunity record identified by 'oppId', right?

The line

Opportunity Oppty=new Opportunity(Id=oppId);

 does not pull up that Opportunity; instead it creates a brand new one.  Instead, you need to look up the Opportunity in the database by that ID, and then operate on its fields, then update it.   Remember that your trigger in general will be handling multiple objects at once, so your code needs to account for that (i.e. it needs to look up all related Opportunities once, via one single SOQL query).

-philbo

MDXHikerMDXHiker
OppId is the Opportunity ID which is unique.
How do I pull that record in the code? Code example would help
Thanks philbo
philbophilbo
Single-record lookup:

Opportunity o = [select <flds> from Opportunity where Id = :oppId];

or, if you've got a bunch of Ids (say in oppIdSet) and you want to keep track of which Opportunity belongs to which Id,

Map<Id , Opportunity> oppById = new Map<Id , Opportunity> (
    [select <flds> from Opportunity where Id in :oppIdSet]
);

Grab the Apex dev guide from the SF Help site if you haven't already; it's full of useful stuff like this.

-philbo
 

 

MDXHikerMDXHiker
thanks philbo. That works fine

Now are referring to the cookboox? or is there something else?
philbophilbo
It's this:

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

-philbo