You need to sign in to do that
Don't have an account?

Modify the existing Apex class - be able to clone Contact Roles when custom clone/renewal button is used.
Hi, I wonder if you can help here> we have a custom renewal button that copies opportunity with certain data autopopulated along with products. Now, we want to clone Contact Roles (primary as well) when renewing/cloning opportunity. here is the code. would you be able how to modify it? Thank you in advance. Kasia
public class OpportunityRenewalController
{
private ApexPages.StandardController controller {get; set;}
private Opportunity opp {get;set;}
public ID newRecordId {get;set;}
public decimal num {get;set;}
public date startDate {get;set;}
public date endDate {get;set;}
public OpportunityRenewalController(ApexPages.StandardController controller)
{
this.controller = controller;
opp = (Opportunity)controller.getRecord();
}
public PageReference preCancel() {
PageReference originalOpp = new ApexPages.StandardController(opp).view();
originalOpp.setRedirect(true);
return originalOpp;
}
public PageReference RenewalWithItems()
{
Savepoint sp = Database.setSavepoint();
num = opp.Number_of_positions__c;
//startDate = Opp.Contract_Start_Date__c;
//endDate = Opp.Contract_End_Date__c;
Opportunity newOpp;
try
{
opp = [SELECT Id, AccountId, Account.Name, Name, Contract_Type__c, StageName, MarketRegion__c,
Service_Offering__c, Market__c, Forecast_Status__c,Sub_Practice__c,
CurrencyIsoCode, Billing_Type__c, Service_Line__c, CampaignId, Campaign.Name, Amount,
Notice_Period__c, Delivery_Location__c, LeadSource, NextStep, Description,
Insurance_Coverage__c, Unusual_Insurance_Requirements__c, Warranty_Clause__c,
Unusual_Damages__c, Risk_Do_Dont__c, Unusual_Risk__c, External_Counsel__c,
Payment_Terms__c, Gross_Margin__c, Authorized_Approver__c, Risk_Assessment_Comment__c,
Contract_Start_Date__c, Contract_End_Date__c, Contract_Duration__c,
CIBER_Opportunity_ID__c, Nearshore__c, Offshore__c, Onshore__c,
Delivery_Location_Type_Poland_GSC__c,Solution_Set__c FROM Opportunity
WHERE Id = :opp.Id];
if (opp.StageName != 'Pre-Qualified')
{
opp.StageName = 'Qualified';
opp.Forecast_Status__c = 'Probable';
opp.Booking_Type__c = 'Existing Client - Renewals/Extension';
if ((opp.Contract_Start_Date__c != Null) && (opp.Contract_End_Date__c != Null))
{
opp.Contract_Start_Date__c = opp.Contract_End_Date__c.addDays(1);
integer iDuration = integer.valueOf(opp.Contract_Duration__c);
opp.Contract_End_Date__c = opp.Contract_Start_Date__c.addMonths(iDuration)-1;
opp.CloseDate = opp.Contract_Start_Date__c;
//opp.Number_of_Positions__c =opp.Number_of_Positions__c=5;
opp.Probability = 75;
}
else
{
opp.CloseDate = Date.today().addMonths(1);
//opp.Contract_Start_Date__c = Null;
//opp.Contract_End_Date__c = Null;
integer iDuration = 0;
opp.Probability = 75;
}
newOpp = opp.Clone(false);
newOpp.Contract_Start_Date__c = startDate;
newOpp.Contract_End_Date__c = endDate;
if (opp.Contract_Type__c == 'Staffing') {
newOpp.Number_of_Positions__c = num;
}
insert newOpp;
NewRecordId = newOpp.id;
List<OpportunityLineItem> items = new List<OpportunityLineItem>();
for (OpportunityLineItem li : [Select l.Id, l.Quantity, l.Description, l.ServiceDate,l.UnitPrice, l.CurrencyIsoCode, l.PricebookEntryId, l.SortOrder, l.Cost_Price__c From OpportunityLineItem l where OpportunityId = :opp.id])
{
OpportunityLineItem newLI = li.clone(false);
newLI.OpportunityId = newOpp.id;
items.add(newLI);
}
insert items;
Note myNote = new Note (ParentId = newRecordId, Title = 'This is the renewal of CIBER Opportunity ID ' + opp.CIBER_Opportunity_ID__c);
insert myNote;
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Renewal not possible for opportunities with phase line Pre-Qualified'));
return null;
}
}
catch (Exception e)
{
Database.rollback(sp);
ApexPages.addMessages(e);
return null;
}
//PageReference oppPage = new ApexPages.standardcontroller(newOpp).view();
PageReference oppPage = new ApexPages.StandardController(newOpp).edit();
oppPage.setRedirect(true);
if(newOpp != null)
{
return new PageReference ('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
}
else
{
return null;
}
}
}
public class OpportunityRenewalController
{
private ApexPages.StandardController controller {get; set;}
private Opportunity opp {get;set;}
public ID newRecordId {get;set;}
public decimal num {get;set;}
public date startDate {get;set;}
public date endDate {get;set;}
public OpportunityRenewalController(ApexPages.StandardController controller)
{
this.controller = controller;
opp = (Opportunity)controller.getRecord();
}
public PageReference preCancel() {
PageReference originalOpp = new ApexPages.StandardController(opp).view();
originalOpp.setRedirect(true);
return originalOpp;
}
public PageReference RenewalWithItems()
{
Savepoint sp = Database.setSavepoint();
num = opp.Number_of_positions__c;
//startDate = Opp.Contract_Start_Date__c;
//endDate = Opp.Contract_End_Date__c;
Opportunity newOpp;
try
{
opp = [SELECT Id, AccountId, Account.Name, Name, Contract_Type__c, StageName, MarketRegion__c,
Service_Offering__c, Market__c, Forecast_Status__c,Sub_Practice__c,
CurrencyIsoCode, Billing_Type__c, Service_Line__c, CampaignId, Campaign.Name, Amount,
Notice_Period__c, Delivery_Location__c, LeadSource, NextStep, Description,
Insurance_Coverage__c, Unusual_Insurance_Requirements__c, Warranty_Clause__c,
Unusual_Damages__c, Risk_Do_Dont__c, Unusual_Risk__c, External_Counsel__c,
Payment_Terms__c, Gross_Margin__c, Authorized_Approver__c, Risk_Assessment_Comment__c,
Contract_Start_Date__c, Contract_End_Date__c, Contract_Duration__c,
CIBER_Opportunity_ID__c, Nearshore__c, Offshore__c, Onshore__c,
Delivery_Location_Type_Poland_GSC__c,Solution_Set__c FROM Opportunity
WHERE Id = :opp.Id];
if (opp.StageName != 'Pre-Qualified')
{
opp.StageName = 'Qualified';
opp.Forecast_Status__c = 'Probable';
opp.Booking_Type__c = 'Existing Client - Renewals/Extension';
if ((opp.Contract_Start_Date__c != Null) && (opp.Contract_End_Date__c != Null))
{
opp.Contract_Start_Date__c = opp.Contract_End_Date__c.addDays(1);
integer iDuration = integer.valueOf(opp.Contract_Duration__c);
opp.Contract_End_Date__c = opp.Contract_Start_Date__c.addMonths(iDuration)-1;
opp.CloseDate = opp.Contract_Start_Date__c;
//opp.Number_of_Positions__c =opp.Number_of_Positions__c=5;
opp.Probability = 75;
}
else
{
opp.CloseDate = Date.today().addMonths(1);
//opp.Contract_Start_Date__c = Null;
//opp.Contract_End_Date__c = Null;
integer iDuration = 0;
opp.Probability = 75;
}
newOpp = opp.Clone(false);
newOpp.Contract_Start_Date__c = startDate;
newOpp.Contract_End_Date__c = endDate;
if (opp.Contract_Type__c == 'Staffing') {
newOpp.Number_of_Positions__c = num;
}
insert newOpp;
NewRecordId = newOpp.id;
List<OpportunityLineItem> items = new List<OpportunityLineItem>();
for (OpportunityLineItem li : [Select l.Id, l.Quantity, l.Description, l.ServiceDate,l.UnitPrice, l.CurrencyIsoCode, l.PricebookEntryId, l.SortOrder, l.Cost_Price__c From OpportunityLineItem l where OpportunityId = :opp.id])
{
OpportunityLineItem newLI = li.clone(false);
newLI.OpportunityId = newOpp.id;
items.add(newLI);
}
insert items;
Note myNote = new Note (ParentId = newRecordId, Title = 'This is the renewal of CIBER Opportunity ID ' + opp.CIBER_Opportunity_ID__c);
insert myNote;
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Renewal not possible for opportunities with phase line Pre-Qualified'));
return null;
}
}
catch (Exception e)
{
Database.rollback(sp);
ApexPages.addMessages(e);
return null;
}
//PageReference oppPage = new ApexPages.standardcontroller(newOpp).view();
PageReference oppPage = new ApexPages.StandardController(newOpp).edit();
oppPage.setRedirect(true);
if(newOpp != null)
{
return new PageReference ('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
}
else
{
return null;
}
}
}