+ Start a Discussion
Mahesh Babu 187Mahesh Babu 187 

Change Quote Owner As Opportunity Owner for a Particular Profile

I have written an Apex class for changing the Quote owner as the Opportunity owner for a Profile named 'TriLink Sales & Service'. I have called this class in Trigger. But it is not updating anything.

APEX CODE:
public class changeOwnerClass{
public list<Opportunity> lstOpp = [Select id, name, ownerId from Opportunity] ;
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);
} }

I can see that the opportunity owner id is not coming here. But how to get opportunity owner id here.
Please help me to solve this error.
Best Answer chosen by Mahesh Babu 187
David Zhu 🔥David Zhu 🔥
You may use the code blow to implment the quote owner change.
trigger changeOwner on Quote (before insert) {
    if(trigger.isInsert && trigger.isBefore)
    { 
        changeOwnerClass.changeOwner(trigger.new);
    } 
}
 
public class changeOwnerClass{

    public static void changeOwner(list<Quote> lstQuote){
        List<Id> oppIds = new List<Id>();
        
        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;
            }
        }
	}
}


 

All Answers

David Zhu 🔥David Zhu 🔥
You may use the code blow to implment the quote owner change.
trigger changeOwner on Quote (before insert) {
    if(trigger.isInsert && trigger.isBefore)
    { 
        changeOwnerClass.changeOwner(trigger.new);
    } 
}
 
public class changeOwnerClass{

    public static void changeOwner(list<Quote> lstQuote){
        List<Id> oppIds = new List<Id>();
        
        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;
            }
        }
	}
}


 
This was selected as the best answer
Mahesh Babu 187Mahesh Babu 187

Hi David,

Can you tell me what to change if I need the Quote owner to be the Opportunity CreatedBy Name.
I have modified the above Apex code but it is throwing "NullPointerException: Attempt to de-reference a null object: Class.changeOwnerClass.changeOwner: line 14, column 1" error while creating a new Quote.

APEX:

public class changeOwnerClass{
    public static void changeOwner(list<Quote> lstQuote){
        List<Id> oppIds = new List<Id>();     
        for (Quote quote: lstQuote) {
            oppIds.add(quote.CreatedById);
        }        
        Map<Id,Opportunity> OppMap = new Map<Id,Opportunity>([Select id, ownerId, CreatedById 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.CreatedById).CreatedById;
            }
        }
    }
}

Thanks,
Mahesh
David Zhu 🔥David Zhu 🔥
You may need to change line 14 to :
quote.OwnerId = oppMap.get(quote.OpportunityId).CreatedById;

oppMap has key of opportunity Id not quote.createdById.
Mahesh Babu 187Mahesh Babu 187
Thanks David.