You need to sign in to do that
Don't have an account?
CBradley1985
Setting Account Owner as the Opportunity Owner in a Class
Hi All,
I am having a bit of trouble that I'm hoping you can help me with...I have a Class that clones our opportunities and one of the things I'd like to add to it is have it set the Account Owner as the Opportunity Owner during cloning.
Attached is my Class that I currently have...any help would be appreciated!
Thanks!
Chris
I am having a bit of trouble that I'm hoping you can help me with...I have a Class that clones our opportunities and one of the things I'd like to add to it is have it set the Account Owner as the Opportunity Owner during cloning.
Attached is my Class that I currently have...any help would be appreciated!
Thanks!
Chris
global with sharing class CloneOpportunity { public static final String PRICEBOOK_NAME = 'CTM Price Book'; WebService static String clone(String oppId) { final String ACTIVITY_HISTORY_SUBJECT = 'Products Missing from ' + PRICEBOOK_NAME; final String COMMONS = 'The following products are not associated with the ' + PRICEBOOK_NAME; // Clone opportunity String pricebook2Id = [select Id from Pricebook2 where Name = :PRICEBOOK_NAME limit 1].Id; RecordType recordType = [select Id from RecordType where SobjectType = 'Opportunity' and Name = 'Renewal Opportunity' limit 1]; String oppQuery = getOpportunityQuery(oppId); Opportunity sourceOpp = (Opportunity)Database.query(oppQuery); Opportunity newOpportunity = sourceOpp.clone(false, true, false, false); newOpportunity.RecordTypeId = recordType.Id; newOpportunity.Pricebook2Id = pricebook2Id; newOpportunity.Contract_Number__c = ''; newOpportunity.Record_Type__c = 'Quote'; newOpportunity.StageName = 'Needs Analysis'; newOpportunity.Thank_You_Card_Email__c = ''; newOpportunity.Thank_You_Card_Contact__c = null; newOpportunity.Purchase_Order__c = '0' ; newOpportunity.GreatPlainsCustomerID__c =''; newOpportunity.Reason_for_Lost_Opportunity__c = null; newOpportunity.Smooth_Billing__c = FALSE; newOpportunity.Smooth_Revenue__c = FALSE; newOpportunity.Thank_You_Card_Template__c = null; newOpportunity.Type = 'Renewal'; newOpportunity.OwnerId = 'Account.OwnerId'; if(newOpportunity.Renewal_Cycle_Follow_Up_Date__c != null) { newOpportunity.Renewal_Cycle_Follow_Up_Date__c = newOpportunity.Renewal_Cycle_Follow_Up_Date__c.addYears(1); } if(newOpportunity.Contract_Start_Date__c != null) { newOpportunity.Contract_Start_Date__c = newOpportunity.Contract_Start_Date__c.addYears(1); } if(newOpportunity.Contract_End_Date__c != null) { newOpportunity.Contract_End_Date__c = newOpportunity.Contract_End_Date__c.addYears(1); } if(newOpportunity.Distribution_Dates_Start_Date__c != null) { newOpportunity.Distribution_Dates_Start_Date__c = newOpportunity.Distribution_Dates_Start_Date__c.addYears(1); } if(newOpportunity.Distribution_Dates_End_Date__c != null) { newOpportunity.Distribution_Dates_End_Date__c = newOpportunity.Distribution_Dates_End_Date__c.addYears(1); } if(newOpportunity.Bill_Dates_Start_Date__c != null) { newOpportunity.Bill_Dates_Start_Date__c = newOpportunity.Bill_Dates_Start_Date__c.addYears(1); } if(newOpportunity.Bill_Dates_End_Date__c != null) { newOpportunity.Bill_Dates_End_Date__c = newOpportunity.Bill_Dates_End_Date__c.addYears(1); } if(newOpportunity.CloseDate != null) { newOpportunity.CloseDate = newOpportunity.CloseDate.addYears(1); } insert newOpportunity; // Clone opportunity products Set<String> productIds = getProductIdsFromLineItem(oppId); Map<String, String> ctmProductIds = new Map<String, String>(); for(PricebookEntry entry : [select Id, Product2Id from PricebookEntry where Pricebook2Id = :pricebook2Id and Product2Id in :productIds and CurrencyIsoCode = :newOpportunity.CurrencyIsoCode]) { ctmProductIds.put(entry.Product2Id, entry.Id); } String lineItemQuery = getOpportunityLineItemQuery(oppId); OpportunityLineItem[] sourceLineItems = (List<OpportunityLineItem>)Database.query(lineItemQuery); List<OpportunityLineItem> newLineItems = new List<OpportunityLineItem>(); List<String> lineItemsInActivity = new List<String>(); String productNames = ''; for(OpportunityLineItem item : sourceLineItems) { if(ctmProductIds.keySet().contains(item.PricebookEntry.Product2Id)) { OpportunityLineItem newLineItem = item.clone(false, true, false, false); newLineItem.OpportunityId = newOpportunity.Id; newLineItem.PricebookEntryId = ctmProductIds.get(item.PricebookEntry.Product2Id); newLineItem.Quantity = (1); if(newLineItem.Billing_Start_Date__c != null) { newLineItem.Billing_Start_Date__c = newLineItem.Billing_Start_Date__c.addYears(1); } if(newLineItem.Billing_End_Date__c != null) { newLineItem.Billing_End_Date__c = newLineItem.Billing_End_Date__c.addYears(1); } if(newLineItem.Contract_Start_Date__c != null) { newLineItem.Contract_Start_Date__c = newLineItem.Contract_Start_Date__c.addYears(1); } if(newLineItem.Contract_End_Date__c != null) { newLineItem.Contract_End_Date__c = newLineItem.Contract_End_Date__c.addYears(1); } newLineItems.add(newLineItem); } else { String productName = ' • ' + item.PricebookEntry.Product2.Name + '\r\n'; lineItemsInActivity.add(productName); productNames += productName; } } if(newLineItems.size() > 0) { insert newLineItems; } if(lineItemsInActivity.size() > 0) { Task task = new Task(); task.WhatId = newOpportunity.Id; task.Subject = ACTIVITY_HISTORY_SUBJECT; task.ActivityDate = Date.today().addDays(-1); task.Status = 'Completed'; task.Type= 'SF Message'; task.Description = COMMONS + ':\r\n'; for(String lineItemName : lineItemsInActivity) { task.Description += lineItemName; } insert task; } return productNames + '^o^' + newOpportunity.Id; } /** * Get the opportunity query. */ private static String getOpportunityQuery(String oppId) { String result = 'select '; for(String fieldApiName : Schema.SObjectType.Opportunity.fields.getMap().keySet()) { result += fieldApiName + ','; } result = result.substring(0, result.length() - 1) + ' from Opportunity where Id = \'' + oppId + '\''; return result; } /** * Get the opportunity product query. */ private static String getOpportunityLineItemQuery(String oppId) { String result = 'select '; for(String fieldApiName : Schema.SObjectType.OpportunityLineItem.fields.getMap().keySet()) { result += fieldApiName + ','; } result = result.replace('totalprice,', ''); result = result + 'PricebookEntry.Product2Id, PricebookEntry.Product2.Name from OpportunityLineItem where OpportunityId = \'' + oppId + '\''; return result; } /** * Get the product IDs in opportunity. */ private static Set<String> getProductIdsFromLineItem(String oppId) { Set<String> result = new Set<String>(); for(OpportunityLineItem item : [select PricebookEntry.Product2Id from OpportunityLineItem where OpportunityId = :oppId]) { result.add(item.PricebookEntry.Product2Id); } return result; }
1) Change Opp query to get the account owner in getOpportunityLineItemQuery method
>>result = result.substring(0, result.length() - 1) + ' from Opportunity where Id = \''+ oppId + '\'';
result += ' account.ownerid from Opportunity where Id = \''+ oppId + '\'';
2) Set the ownerid to Opportunity clone method
newOpportunity.OwnerId = sourceOpp.Account.OwnerId;
All Answers
1) Change Opp query to get the account owner in getOpportunityLineItemQuery method
>>result = result.substring(0, result.length() - 1) + ' from Opportunity where Id = \''+ oppId + '\'';
result += ' account.ownerid from Opportunity where Id = \''+ oppId + '\'';
2) Set the ownerid to Opportunity clone method
newOpportunity.OwnerId = sourceOpp.Account.OwnerId;
Can you tell me what line I need to insert
"result = result.substring(0, result.length() - 1) + ' from Opportunity where Id = \''+ oppId + '\'';
result += ' account.ownerid from Opportunity where Id = \''+ oppId + '\'';"
and what line I should insert
"newOpportunity.OwnerId = sourceOpp.Account.OwnerId;" ( I think I can just put it right below row #30 once step one is done...)
Again, thank you for any help...really want to learn!
Chris
instead of that line you need to have result += ' account.ownerid from Opportunity where Id = \''+ oppId + '\''; this line queies the account owner of the opportunity when it queries the opp details.
then where ever you have newOpportunity.OwnerId = 'Account.OwnerId'; (line 30) replace that line with
newOpportunity.OwnerId = sourceOpp.Account.OwnerId;
That worked perfectly...I am starting to see what it is doing now.
Chris