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
AzusfdcAzusfdc 

Getting error System.NullPointerException: Attempt to de-reference a null object: how to resolve this below is the code ?

public class Helperclass
{
    public static void user_coninsertupdated()
    {       
    }
    
    public static void oppnameupdatewithacnamelastmodifyclsdate(List<Opportunity> opp)
    {
    set<id> accountid=new set<id>();
    set<id> ownerid=new set<id>();
    string opname;
    for(opportunity opp1:opp)
    {
    accountid.add(opp1.accountid);
    ownerid.add(opp1.ownerid);
    }
    map<id,account> accmap=new map<id,account>
    ([select id,name from account where id in:accountid]);
    map<id,user> usermap=new map<id,user>
    ([select id,lastmodifiedby.name from user where id in:ownerid]);
    
    for(opportunity opp2:opp)
    {   
    opname=accmap.get(opp2.accountid).name+' '+opp2.closedate+' '+usermap.get(opp2.ownerid).lastmodifiedby.name;
    if(opp2!=null)
    opp2.name=opname;
    }
    
    }
}
 
trigger accnameclsdatemodifydate  on Opportunity (before insert,before update) {

    if(trigger.isbefore && trigger.isinsert)
    {
    Helperclass.oppnameupdatewithacnamelastmodifyclsdate(trigger.new);
    }
    
    if(trigger.isbefore && trigger.isupdate)
    {
    Helperclass.oppnameupdatewithacnamelastmodifyclsdate(trigger.new);
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
public class Helperclass
{
    public static void user_coninsertupdated()
    {       
    }
    
    public static void oppnameupdatewithacnamelastmodifyclsdate(List<Opportunity> opp)
    {
		set<id> accountid=new set<id>();
		set<id> ownerid=new set<id>();
		string opname;
		
		for(opportunity opp1:opp)
		{
			if(opp1.accountid != null)
			{
				accountid.add(opp1.accountid);
			}
			if(opp1.ownerid != null)
			{
				ownerid.add(opp1.ownerid);
			}	
		}
		
		map<id,account> accmap=new map<id,account> ([select id,name from account where id in:accountid]);
		map<id,user> usermap=new map<id,user>    ([select id,lastmodifiedby.name from user where id in:ownerid]);
    
		for(opportunity opp2:opp)
		{   
			if(accmap.containsKey(opp2.accountid) && usermap.containsKey(opp2.ownerid) && opp2.closedate != null )
			{
				opname=accmap.get(opp2.accountid).name+' '+opp2.closedate+' '+usermap.get(opp2.ownerid).lastmodifiedby.name;
			}
			else if ( accmap.containsKey(opp2.accountid) && opp2.closedate != null )
			{
				opname=accmap.get(opp2.accountid).name+' '+opp2.closedate ;
			}
			
			if(opname!=null && opname!='')
				opp2.name=opname;
		}
    }
}

Let us know if this will help you
 
Mahesh DMahesh D
Hi

Please find the below modified code:

Here I modified / followed:
(1) Naming Convention.
(2) Alignment.
(3) Bulkified the code.
(4) Corrected the logic
(5) Also tested the code in my DE environment and everything looks good.


Apex Class:
public class OpportunityHelper {
    public static void user_coninsertupdated()
    {       
    }
    
    public static void oppnameupdatewithacnamelastmodifyclsdate(List<Opportunity> oppList) {
        Set<Id> accIdSet = new Set<Id>();
        Set<Id> ownerIdSet = new Set<Id>();
        
        for(Opportunity opp: oppList) {
            if(opp.AccountId != null)
                accIdSet.add(opp.AccountId);
            ownerIdSet.add(opp.OwnerId);
        }
        
        Map<Id, Account> accMap = new Map<Id, Account>();
        if(!accIdSet.isEmpty()) {
            accMap = new Map<Id, Account>([Select Id, Name from Account where Id IN: accIdSet]);
        }
        Map<Id, User> userMap = new Map<Id, User>([Select Id, LastModifiedBy.Name from User where Id IN: ownerIdSet]);
        
        for(opportunity opp:oppList) {   
            
            String oppName = (opp.AccountId != null ? accMap.get(opp.AccountId).Name: '')+' '+opp.closeDate+' '+userMap.get(opp.OwnerId).LastModifiedBy.Name;
            opp.Name = oppName;
        }    
    }
}

Trigger:
 
trigger OpportunityTrigger on Opportunity (before insert, before update) {

    if(trigger.isbefore && trigger.isinsert) {
        OpportunityHelper.oppnameupdatewithacnamelastmodifyclsdate(trigger.new);
    }
    
    if(trigger.isbefore && trigger.isupdate) {
        OpportunityHelper.oppnameupdatewithacnamelastmodifyclsdate(trigger.new);
    }
}

Also go through the below links for understanding the Apex Code:

https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex1_2.htm

https://developer.salesforce.com/page/Apex_Code:_The_World's_First_On-Demand_Programming_Language

https://www.quora.com/What-is-the-best-method-to-learn-Salesforce-com-product-s-Apex-code

http://www.slideshare.net/EdwinOstos/introduction-to-apex-code

https://www.youtube.com/watch?v=Azb31kE31ZA


Please do let me know if it helps you.

Regards,
Mahesh