You need to sign in to do that
Don't have an account?
travis.truett
Need Account Name in Opportunity Trigger
I have a trigger that duplicates opportunities when we close them. Currently, the trigger just appends the word Annual and the current year to the end of the name of the duplicated opp for the name of the new opp. What I would like to do, however, is use the name of the opportunity's associated account instead. Is there an easy way to pull that info in and access the account's name? Here's my code, with the section I'm talking about bolded:
trigger Create_followup on Opportunity (before update, after insert) {
List<Pricebook2> standardBook = [SELECT Id FROM Pricebook2 WHERE Name = :'Ambition'];//Create an instance of the standard pricebook
if(Trigger.isUpdate){
List<Opportunity> listOppor = new List<Opportunity>();
for (Opportunity o: Trigger.new){
if (o.StageName == 'Closed Won' && o.Stage_Change__c == false){
Opportunity oppNew = o.clone();
oppNew.Name = oppNew.Name + ' - Annual ' + o.CloseDate.year();
if(o.Renewal_Date__c != null){
oppNew.Renewal_Date__c = o.Renewal_Date__c.addYears(1);
oppNew.CloseDate = o.Renewal_Date__c;}
oppNew.StageName = 'Discovery';
oppNew.Probability = 25;
oppNew.Parent_Opportunity__c = o.Id;
oppNew.Pricebook2Id = standardBook[0].Id;//associate the standard pricebook with this opportunity
oppNew.Is_Clone__c = true;
listOppor.add(oppNew);
o.Stage_Change__c = true;
}
}//end of for loop
if(listOppor.size() > 0){
insert listOppor;
List<OpportunityContactRole> ocrList = [SELECT OpportunityId, ContactId, Role FROM OpportunityContactRole WHERE OpportunityId IN :Trigger.New];
List<OpportunityContactRole> newOcrList = new List<OpportunityContactRole>();
if(!ocrList.isEmpty()) {
Map<Id, Id> oldOpNewOpIdMap = new Map<Id, Id>();
for(Opportunity opNew : listOppor) {
oldOpNewOpIdMap.put(opNew.Parent_Opportunity__c, opNew.Id);
}
for(OpportunityContactRole ocr : ocrList) {
OpportunityContactRole newOcr = new OpportunityContactRole();
newOcr.ContactId = ocr.ContactId;
newOcr.Role = ocr.Role;
newOcr.OpportunityId = oldOpNewOpIdMap.get(ocr.OpportunityId);
newOcrList.add(newOcr);
}
insert newOcrList;
}
List<OpportunityLineItem> oliList = [SELECT OpportunityId, PricebookEntryId, UnitPrice, Quantity, Duration__c, Payment_Terms__c, Discount FROM OpportunityLineItem WHERE OpportunityId IN :Trigger.New];
List<OpportunityLineItem> newoliList = new List<OpportunityLineItem>();
if(!oliList.isEmpty()) {
Map<Id, Id> oldOpNewOpIdMap2 = new Map<Id, Id>();
for(Opportunity opNew : listOppor) {
oldOpNewOpIdMap2.put(opNew.Parent_Opportunity__c, opNew.Id);
}
for(OpportunityLineItem oli : oliList) {
OpportunityLineItem newOli = new OpportunityLineItem();
newOli.UnitPrice = oli.UnitPrice;
newOli.PricebookEntryId = oli.PricebookEntryId;
newOli.Quantity = oli.Quantity;
newOli.Duration__c = 12;
newOli.Payment_Terms__c = oli.Payment_Terms__c;
newOli.Discount = oli.Discount;
newOli.OpportunityId = oldOpNewOpIdMap2.get(oli.OpportunityId);
newoliList.add(newOli);
}
insert newoliList;
}
}
}
if(trigger.isInsert){
try{
//OpportunityLineItem[] lines = new OpportunityLineItem[0];
//PricebookEntry entry = [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :standardBook.Id AND Product2.ProductCode = 'ENTERPRISE_ANNUAL_UPFRONT'];
List<Event> eventList = new List<Event>();
//List<Note> noteList = new List<Note>();
for(Opportunity o: Trigger.new){
if(o.Is_Clone__c == true){
//noteList.add(new Note(ParentId=o.id,Title='Matt is the Apex_God',Body='Matt is the Apex_God',isPrivate=false));
//lines.add(new OpportunityLineItem(PricebookEntryId=entry.Id, OpportunityId=o.Id, UnitPrice=entry.UnitPrice, Quantity=1));
if(o.Renewal_Date__c != null){
DateTime myDateTime = o.Renewal_Date__c.addMonths(-10);
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime,subject='Account Management Follow-Up', EndDateTime=myDateTime, IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(2),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(2), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(4),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(4), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(6),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(6), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(8),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(8), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(10),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(10), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(10),subject='Renewal',EndDateTime=myDateTime.addMonths(10), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(9),subject='Sales Contract Follow-Up',EndDateTime=myDateTime.addMonths(9), IsAllDayEvent=true));
}//end of if
}
}
//insert lines;
insert eventList;
//insert noteList;
}
catch(Exception e){
}
}
}
trigger Create_followup on Opportunity (before update, after insert) {
List<Pricebook2> standardBook = [SELECT Id FROM Pricebook2 WHERE Name = :'Ambition'];//Create an instance of the standard pricebook
if(Trigger.isUpdate){
List<Opportunity> listOppor = new List<Opportunity>();
for (Opportunity o: Trigger.new){
if (o.StageName == 'Closed Won' && o.Stage_Change__c == false){
Opportunity oppNew = o.clone();
oppNew.Name = oppNew.Name + ' - Annual ' + o.CloseDate.year();
if(o.Renewal_Date__c != null){
oppNew.Renewal_Date__c = o.Renewal_Date__c.addYears(1);
oppNew.CloseDate = o.Renewal_Date__c;}
oppNew.StageName = 'Discovery';
oppNew.Probability = 25;
oppNew.Parent_Opportunity__c = o.Id;
oppNew.Pricebook2Id = standardBook[0].Id;//associate the standard pricebook with this opportunity
oppNew.Is_Clone__c = true;
listOppor.add(oppNew);
o.Stage_Change__c = true;
}
}//end of for loop
if(listOppor.size() > 0){
insert listOppor;
List<OpportunityContactRole> ocrList = [SELECT OpportunityId, ContactId, Role FROM OpportunityContactRole WHERE OpportunityId IN :Trigger.New];
List<OpportunityContactRole> newOcrList = new List<OpportunityContactRole>();
if(!ocrList.isEmpty()) {
Map<Id, Id> oldOpNewOpIdMap = new Map<Id, Id>();
for(Opportunity opNew : listOppor) {
oldOpNewOpIdMap.put(opNew.Parent_Opportunity__c, opNew.Id);
}
for(OpportunityContactRole ocr : ocrList) {
OpportunityContactRole newOcr = new OpportunityContactRole();
newOcr.ContactId = ocr.ContactId;
newOcr.Role = ocr.Role;
newOcr.OpportunityId = oldOpNewOpIdMap.get(ocr.OpportunityId);
newOcrList.add(newOcr);
}
insert newOcrList;
}
List<OpportunityLineItem> oliList = [SELECT OpportunityId, PricebookEntryId, UnitPrice, Quantity, Duration__c, Payment_Terms__c, Discount FROM OpportunityLineItem WHERE OpportunityId IN :Trigger.New];
List<OpportunityLineItem> newoliList = new List<OpportunityLineItem>();
if(!oliList.isEmpty()) {
Map<Id, Id> oldOpNewOpIdMap2 = new Map<Id, Id>();
for(Opportunity opNew : listOppor) {
oldOpNewOpIdMap2.put(opNew.Parent_Opportunity__c, opNew.Id);
}
for(OpportunityLineItem oli : oliList) {
OpportunityLineItem newOli = new OpportunityLineItem();
newOli.UnitPrice = oli.UnitPrice;
newOli.PricebookEntryId = oli.PricebookEntryId;
newOli.Quantity = oli.Quantity;
newOli.Duration__c = 12;
newOli.Payment_Terms__c = oli.Payment_Terms__c;
newOli.Discount = oli.Discount;
newOli.OpportunityId = oldOpNewOpIdMap2.get(oli.OpportunityId);
newoliList.add(newOli);
}
insert newoliList;
}
}
}
if(trigger.isInsert){
try{
//OpportunityLineItem[] lines = new OpportunityLineItem[0];
//PricebookEntry entry = [SELECT Id, UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :standardBook.Id AND Product2.ProductCode = 'ENTERPRISE_ANNUAL_UPFRONT'];
List<Event> eventList = new List<Event>();
//List<Note> noteList = new List<Note>();
for(Opportunity o: Trigger.new){
if(o.Is_Clone__c == true){
//noteList.add(new Note(ParentId=o.id,Title='Matt is the Apex_God',Body='Matt is the Apex_God',isPrivate=false));
//lines.add(new OpportunityLineItem(PricebookEntryId=entry.Id, OpportunityId=o.Id, UnitPrice=entry.UnitPrice, Quantity=1));
if(o.Renewal_Date__c != null){
DateTime myDateTime = o.Renewal_Date__c.addMonths(-10);
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime,subject='Account Management Follow-Up', EndDateTime=myDateTime, IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(2),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(2), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(4),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(4), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(6),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(6), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(8),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(8), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(10),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(10), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(10),subject='Renewal',EndDateTime=myDateTime.addMonths(10), IsAllDayEvent=true));
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(9),subject='Sales Contract Follow-Up',EndDateTime=myDateTime.addMonths(9), IsAllDayEvent=true));
}//end of if
}
}
//insert lines;
insert eventList;
//insert noteList;
}
catch(Exception e){
}
}
}
I have not uderstood your requirement cleary but we can Try below and let me know.
Best Regards,
-Vivek
Note:- if you want to access any lookup field values in trigger then you need to execute soql like above
Please let us know if this will help u
Thanks,
Amit Chaudhary
So Vivek, when I tried your code, I got null - Annual 2015
and Amit, when I tried yours, I got Name - Annual 2015
So for some reason it's not actually grabbing the name of the account...not sure why.