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
Manjunath reddy 25Manjunath reddy 25 

Getting null for account name.

trigger abcAccount on Opportunity (before update) {
    list<id> oppIds = new list<id>();
    //if(trigger.isInsert || trigger.isUpdate){
        for(opportunity opp : trigger.new){
            system.debug('opp'+opp);
            system.debug('opp.Account.name'+opp.Account.name);
            if(opp.Account.name=='Axis'){
             system.debug('opp'+opp);
                opp.amount = opp.amount*10/100;
                //oppIds.add(opp.id);
            }
        }
    //}

}

Hi I am getting null for opp.Account.name when ued system.debug('opp.Account.name'+opp.Account.name);
 Actually iam trying to disount on amount when related account of the opportunity is changed to Axis.
Can some one help me on this?
 
Best Answer chosen by Manjunath reddy 25
SandhyaSandhya (Salesforce Developers) 
Hi Manjunath Reddy,


You need to query for those values since they don't come down natively in the trigger. Anything that is related will be null (like op.Account.Name)

Please see below code.
trigger abcAccount on Opportunity (before update) {
    list<id> oppIds = new list<id>();
    Set<Id> AccountIds = new Set<Id>();

    //if(trigger.isInsert || trigger.isUpdate){
        for(opportunity opp : trigger.new){
        accountIds.add(opp.AccountId);
        }
            Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :accountIds]);
String sName;
for (Opportunity op : Trigger.new) {

  // add the account name
  sName = accountMap.get(op.AccountId).Name;
  System.debug('Account name'+sName);
  
            if(sName=='Axis'){
            // system.debug('opp'+opp);
                op.amount = op.amount*10/100;
                //oppIds.add(op.id);
            }
        
    }

}

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi Manjunath Reddy,


You need to query for those values since they don't come down natively in the trigger. Anything that is related will be null (like op.Account.Name)

Please see below code.
trigger abcAccount on Opportunity (before update) {
    list<id> oppIds = new list<id>();
    Set<Id> AccountIds = new Set<Id>();

    //if(trigger.isInsert || trigger.isUpdate){
        for(opportunity opp : trigger.new){
        accountIds.add(opp.AccountId);
        }
            Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :accountIds]);
String sName;
for (Opportunity op : Trigger.new) {

  // add the account name
  sName = accountMap.get(op.AccountId).Name;
  System.debug('Account name'+sName);
  
            if(sName=='Axis'){
            // system.debug('opp'+opp);
                op.amount = op.amount*10/100;
                //oppIds.add(op.id);
            }
        
    }

}

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
This was selected as the best answer
Rohit K SethiRohit K Sethi
Hi,

In above code there are two problems :

1. First you are accessing the opp.account.name field with the reference to trigger.new which is wrong because we can only use the opp.accountId field.
2. This trigger will run every time if the account is updated or not. So whenever we write code for update we first check the old and new values.

Use below code for your problem :
trigger abcAccount on Opportunity (before update) {
    list<id> oppIds = new list<id>();
    if(trigger.isUpdate){
        // This set contains the all account ids which are changed.
        Set<Id> accountIds = new Set<Id>();
        for(opportunity opp : trigger.new){
            if(trigger.oldMap.get(opp.id).AccountId != opp.accountId && opp.accountId != null){
                accountIds.add(opp.AccountId);
            }
        }
        Map<Id,Account> accountMap = new Map<Id,Account>([select id,name from account where name = 'Axis' and id = : accountIds]);
        for(Opportunity opp : trigger.new){
            if(accountMap.containsKey(opp.accountId)){
                 opp.amount = opp.amount*10/100;
            }
        }
    }
}


Thanks.
Manjunath reddy 25Manjunath reddy 25
Hi sandhya I have tried your code I am getting the error  while saving the record, error is 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger abcAccount caused an unexpected exception, contact your administrator: abcAccount: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.abcAccount: line 15, column 1
trigger abcAccount on Opportunity (before update) {
    list<id> oppIds = new list<id>();
        //if(trigger.isBefore)
        //if(trigger.isInsert || trigger.isUpdate){
            for(opportunity opp : trigger.new){
                if(trigger.oldMap.get(opp.id).AccountId != opp.accountId && opp.accountId != null){
                    oppIds .add(opp.id);   
                }
            }
            map<id,account> accountMap = new map<id,account>([select id,name from account where name = 'Axis'and id IN : oppIds]);
            for(opportunity opp : trigger.new){
                string accName;
                accName = opp.AccountId;
                system.debug('accName '+accName );
                accName = accountMap.get(opp.AccountId).name;
                system.debug('accName '+accName );
                if(accName=='Axis'){
                    opp.amount = opp.amount*10/100;
            }

            }
        //}

}

debug logs are as follows

20:46:01.0 (660299)|CODE_UNIT_STARTED|[EXTERNAL]|01q28000000RHCN|abcAccount on Opportunity trigger event BeforeUpdate for [0062800000BIdmd] 20:46:01.0 (2479732)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|SELECT id, name FROM account WHERE (name = 'Axis' AND id IN :tmpVar1) 20:46:01.0 (5403866)|SOQL_EXECUTE_END|[10]|Rows:0 20:46:01.0 (5673945)|USER_DEBUG|[14]|DEBUG|accName 0012800000liw4yAAA 20:46:01.0 (5773133)|EXCEPTION_THROWN|[15]|System.NullPointerException: Attempt to de-reference a null object 20:46:01.0 (6009406)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object Trigger.abcAccount: line 15, column 1 20:46:01.0 (6023980)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object Trigger.abcAccount: line 15, column 1 20:46:01.6 (6080968)|CUMULATIVE_LIMIT_USAGE 20:46:01.6 (6080968)|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 1 out of 100
Manjunath reddy 25Manjunath reddy 25
Hi Sandhya ,

Now it is working  than you
trigger abcAccount on Opportunity (before update) {
    list<id> oppIds = new list<id>();
    Set<Id> AccountIds = new Set<Id>();

        //if(trigger.isBefore)
        //if(trigger.isInsert || trigger.isUpdate){
            for(opportunity opp : trigger.new){
                    AccountIds .add(opp.accountId);   
            }
            map<id,account> accountMap = new map<id,account>([select id,name from account where id IN : AccountIds ]);
            for(opportunity opp : trigger.new){
                string accName;
                accName = opp.AccountId;
                system.debug('accName '+accName );
                accName = accountMap.get(opp.AccountId).name;
                system.debug('accName '+accName );
                if(accName=='Axis'){
                    opp.amount = opp.amount*10/100;
            }

            }
        //}

}