You need to sign in to do that
Don't have an account?
Mahesh 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.
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.
All Answers
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
quote.OwnerId = oppMap.get(quote.OpportunityId).CreatedById;
oppMap has key of opportunity Id not quote.createdById.