0

I have written an Apex class for changing the Quote owner as the Opportunity owner for Profile named 'TriLink Sales & Service'. I have called this class class in Trigger. But it is throwing 'Apex trigger changeOwner caused an unexpected exception, contact your administrator: changeOwner: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.changeOwnerClass.changeOwner: line 7, column 1' error.

APEX CODE:

public class changeOwnerClass{

public list<Opportunity> lstOpp ;
public void changeOwner(list<Quote> lstQuote){
    Profile p = [select id,Name from Profile where id=:Userinfo.getProfileid()];
    System.debug(p);
    for (Opportunity opp : lstOpp){
        for(Quote objQuote : lstQuote){
        if(p.Name == 'TriLink Sales & Service' || p.Name!= 'TriLink GMP'){
            objQuote.OwnerId = opp.OwnerId;
            System.debug(opp.OwnerId);
            System.debug(objQuote.OwnerId);
        }}
    }
}

}

TRIGGER:

trigger changeOwner on Quote (before insert) { changeOwnerClass clsChangeOwner = new changeOwnerClass ();

if(trigger.isInsert && trigger.isBefore){ clsChangeOwner.changeOwner(trigger.new); } }

Please help me to solve this error.

1
  • where do you set the lstOpp?
    – Liron C
    Commented Aug 7, 2020 at 12:45

2 Answers 2

0
public list<Opportunity> lstOpp ;
public void changeOwner(list<Quote> lstQuote){
    Profile[] p = [select id,Name from Profile where id=:Userinfo.getProfileid()];
    System.debug(p);
if(!lstOpp.isEmpty()){
    for (Opportunity opp : lstOpp){
        for(Quote objQuote : lstQuote){
      
        if(!p.isEmpty() && (p[0].Name == 'TriLink Sales & Service' || p[0].Name!= 'TriLink GMP')){
            objQuote.OwnerId = opp.OwnerId;
            System.debug(opp.OwnerId);
            System.debug(objQuote.OwnerId);
        }}
    }
}
}
0

I have modified the code and it is working now.

APEX CODE:

public class changeOwnerClass{ public static void changeOwner(list lstQuote){ List oppIds = new List();
for (Quote quote: lstQuote) { oppIds.add(quote.OpportunityId); }

    Map<Id,Opportunity> OppMap = new Map<Id,Opportunity>([Select id, ownerId from Opportunity where id in :oppIds]);
    Profile p = [select id,Name from Profile where id=:Userinfo.getProfileid()];
    for(Quote quote : lstQuote){
        if(p.Name == 'TriLink Sales & Service'){
            quote.OwnerId = oppMap.get(quote.OpportunityId).OwnerId;
        }
    }
}

}

TRIGGER:

trigger changeOwner on Quote (before insert) { if(trigger.isInsert && trigger.isBefore) { changeOwnerClass.changeOwner(trigger.new); } }

1
  • Can anyone tell me how to make the Quote Owner to be the Opportunity CreatedBy.Name as the owner?
    – Mahesh
    Commented Aug 11, 2020 at 8:25

You must log in to answer this question.

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