You need to sign in to do that
Don't have an account?
Kalpana Dhanajayaraj
webservice static String CreateBridgeRenewal(Id opportunityId, Integer ContractTerms)
{
String retString = '';
Savepoint sp = Database.setSavepoint();
try
{
Opportunity originOpportunity = [select Name,Business_Unit__c,Contract_Start_Date__c, Originating_Opportunity__r.id, Account.Name,LCSA_ID__c,Pricebook2Id,
CurrencyIsoCode,Payment_Method__c,Payment_Period__c, Renew_Target_Close_Date__c,
(select id,description,UnitPrice,PricebookEntryid,Quantity,
PricebookEntry.IsActive,PricebookEntry.Name from OpportunityLineItems order by SortOrder)
from Opportunity
where id=:opportunityId limit 1];
String recordTypeId = (originOpportunity.Business_Unit__c == 'LSS') ? Label.RT_Sales_Solutions : Label.RT_CorpSolutions;
String type = (originOpportunity.Business_Unit__c == 'LSS') ? 'Adjustment' : 'Bridge Renewal';
String stage = (originOpportunity.Business_Unit__c == 'LSS') ? 'Introduction' : 'Engaged';
String subtype = (originOpportunity.Business_Unit__c == 'LSS') ? 'Bridge Renewal' : '';
Opportunity BridgeRenewal = new Opportunity(Type = type,
RecordTypeId = recordTypeId,
StageName = stage,
Contract_Term_Months__c = ContractTerms,
Sales_Channel__c = 'Direct',
OwnerId = UserInfo.getUserId(),
ForecastCategoryName = 'Omitted',
closeDate= originOpportunity.Renew_Target_Close_Date__c, //Case-01845278: Set Close Date of the Bridge renewal to be the Renewal Target Close Date of Originating Opportunity
AccountId = originOpportunity.AccountId,
//Per Case 03372446: Change Originating_Opportunity__c of Bridge Renewal
//Originating_Opportunity__c = opportunityId,
Originating_Opportunity__c = originOpportunity.Originating_Opportunity__r.id,
Renewal_Opportunity__c = opportunityId,
Contract_Start_Date__c = originOpportunity.Contract_Start_Date__c,
LCSA_ID__c = originOpportunity.LCSA_ID__c,
Pricebook2Id = originOpportunity.Pricebook2Id,
CurrencyIsoCode = originOpportunity.CurrencyIsoCode,
Description = 'Bridge Renewal Opportunity',
Approval_Status__c = 'Renewal Bridge - New',
Sub_Type__c = subtype,
nextstep = 'Bridge Renewal Opportunity Approval',
Renew_Target_Close_Date__c= originOpportunity.Renew_Target_Close_Date__c,
Name = originOpportunity.Account.Name + '- BridgeRenewal -Original RTCD ' + datetime.newInstance(originOpportunity.Contract_Start_Date__c, time.newInstance(0, 0, 0, 0)).format('MM/dd/yyyy'),
Exclude_Xactly__c = true);
BridgeRenewal.Name = originOpportunity.Account.Name + '- BridgeRenewal';
if(originOpportunity.Contract_Start_Date__c!=null || originOpportunity.Renew_Target_Close_Date__c!=null)
{
BridgeRenewal.Name += ' -Original RTCD ' + datetime.newInstance(originOpportunity.Contract_Start_Date__c!=null?originOpportunity.Contract_Start_Date__c:originOpportunity.Renew_Target_Close_Date__c, time.newInstance(0, 0, 0, 0)).format('MM/dd/yyyy');
BridgeRenewal.Contract_End_Date__c = (originOpportunity.Contract_Start_Date__c!=null?originOpportunity.Contract_Start_Date__c:originOpportunity.Renew_Target_Close_Date__c).addMonths(ContractTerms).addDays(-1);
}
if(originOpportunity.Business_Unit__c == 'LSS')
{
BridgeRenewal.Payment_Method__c = originOpportunity.Payment_Method__c;
BridgeRenewal.Payment_Period__c = originOpportunity.Payment_Period__c;
}
Integer thisYear = System.today().year() ;
if(BridgeRenewal.Contract_Start_Date__c != null && Integer.valueOf(BridgeRenewal.Contract_Start_Date__c.year()) == thisYear
&& BridgeRenewal.Contract_End_Date__c != null && Integer.valueOf(BridgeRenewal.Contract_End_Date__c.year()) > thisYear){
BridgeRenewal.Exclude_Xactly__c = false;
}else{
BridgeRenewal.Exclude_Xactly__c = true;
}
if(BridgeRenewal.closeDate == null) BridgeRenewal.closeDate = system.today();
insert BridgeRenewal;
retString = BridgeRenewal.id;
/*Create Opportunity line items START*/
List<OpportunityLineItem> items = new List<OpportunityLineItem>();
for (OpportunityLineItem oli : originOpportunity.OpportunityLineItems)
{
if(oli.PricebookEntry.IsActive)
{
OpportunityLineItem newOLI = oli.clone(false);//new OpportunityLineItem();
newOLI.OpportunityId = BridgeRenewal.Id;
items.add(newOLI);
}
}
String[] insertErrors = GeneralUtil.insertRecords(items, 'OpportunityUtil', 'CreateBridgeRenewal-' + opportunityId, 'High');
} catch (Exception e){
Database.rollback(sp);
ExceptionLogUtil.logException(UserInfo.getUserId(), 'OpportunityUtil', 'CreateBridgeRenewal', new List<String>{''+e+' - ' + opportunityId + ContractTerms}, 'High');
retString = 'There was an error creating BridgeRenewal. Please contact System Administrator. ERROR: ' + e;
}
System.debug(LoggingLevel.Error,'CreateBridgeRenewal() retString =>' + retString);
return retString;
}
Getting error in th eline : Name = originOpportunity.Account.Name + '- BridgeRenewal -Original RTCD ' + datetime.newInstance(originOpportunity.Contract_Start_Date__c, time.newInstance(0, 0, 0, 0)).format('MM/dd/yyyy'),
Name is the API name.
how to get rid of this error
Invalif Foreign Key Relationship Error
webservice static String CreateBridgeRenewal(Id opportunityId, Integer ContractTerms)
{
String retString = '';
Savepoint sp = Database.setSavepoint();
try
{
Opportunity originOpportunity = [select Name,Business_Unit__c,Contract_Start_Date__c, Originating_Opportunity__r.id, Account.Name,LCSA_ID__c,Pricebook2Id,
CurrencyIsoCode,Payment_Method__c,Payment_Period__c, Renew_Target_Close_Date__c,
(select id,description,UnitPrice,PricebookEntryid,Quantity,
PricebookEntry.IsActive,PricebookEntry.Name from OpportunityLineItems order by SortOrder)
from Opportunity
where id=:opportunityId limit 1];
String recordTypeId = (originOpportunity.Business_Unit__c == 'LSS') ? Label.RT_Sales_Solutions : Label.RT_CorpSolutions;
String type = (originOpportunity.Business_Unit__c == 'LSS') ? 'Adjustment' : 'Bridge Renewal';
String stage = (originOpportunity.Business_Unit__c == 'LSS') ? 'Introduction' : 'Engaged';
String subtype = (originOpportunity.Business_Unit__c == 'LSS') ? 'Bridge Renewal' : '';
Opportunity BridgeRenewal = new Opportunity(Type = type,
RecordTypeId = recordTypeId,
StageName = stage,
Contract_Term_Months__c = ContractTerms,
Sales_Channel__c = 'Direct',
OwnerId = UserInfo.getUserId(),
ForecastCategoryName = 'Omitted',
closeDate= originOpportunity.Renew_Target_Close_Date__c, //Case-01845278: Set Close Date of the Bridge renewal to be the Renewal Target Close Date of Originating Opportunity
AccountId = originOpportunity.AccountId,
//Per Case 03372446: Change Originating_Opportunity__c of Bridge Renewal
//Originating_Opportunity__c = opportunityId,
Originating_Opportunity__c = originOpportunity.Originating_Opportunity__r.id,
Renewal_Opportunity__c = opportunityId,
Contract_Start_Date__c = originOpportunity.Contract_Start_Date__c,
LCSA_ID__c = originOpportunity.LCSA_ID__c,
Pricebook2Id = originOpportunity.Pricebook2Id,
CurrencyIsoCode = originOpportunity.CurrencyIsoCode,
Description = 'Bridge Renewal Opportunity',
Approval_Status__c = 'Renewal Bridge - New',
Sub_Type__c = subtype,
nextstep = 'Bridge Renewal Opportunity Approval',
Renew_Target_Close_Date__c= originOpportunity.Renew_Target_Close_Date__c,
Name = originOpportunity.Account.Name + '- BridgeRenewal -Original RTCD ' + datetime.newInstance(originOpportunity.Contract_Start_Date__c, time.newInstance(0, 0, 0, 0)).format('MM/dd/yyyy'),
Exclude_Xactly__c = true);
BridgeRenewal.Name = originOpportunity.Account.Name + '- BridgeRenewal';
if(originOpportunity.Contract_Start_Date__c!=null || originOpportunity.Renew_Target_Close_Date__c!=null)
{
BridgeRenewal.Name += ' -Original RTCD ' + datetime.newInstance(originOpportunity.Contract_Start_Date__c!=null?originOpportunity.Contract_Start_Date__c:originOpportunity.Renew_Target_Close_Date__c, time.newInstance(0, 0, 0, 0)).format('MM/dd/yyyy');
BridgeRenewal.Contract_End_Date__c = (originOpportunity.Contract_Start_Date__c!=null?originOpportunity.Contract_Start_Date__c:originOpportunity.Renew_Target_Close_Date__c).addMonths(ContractTerms).addDays(-1);
}
if(originOpportunity.Business_Unit__c == 'LSS')
{
BridgeRenewal.Payment_Method__c = originOpportunity.Payment_Method__c;
BridgeRenewal.Payment_Period__c = originOpportunity.Payment_Period__c;
}
Integer thisYear = System.today().year() ;
if(BridgeRenewal.Contract_Start_Date__c != null && Integer.valueOf(BridgeRenewal.Contract_Start_Date__c.year()) == thisYear
&& BridgeRenewal.Contract_End_Date__c != null && Integer.valueOf(BridgeRenewal.Contract_End_Date__c.year()) > thisYear){
BridgeRenewal.Exclude_Xactly__c = false;
}else{
BridgeRenewal.Exclude_Xactly__c = true;
}
if(BridgeRenewal.closeDate == null) BridgeRenewal.closeDate = system.today();
insert BridgeRenewal;
retString = BridgeRenewal.id;
/*Create Opportunity line items START*/
List<OpportunityLineItem> items = new List<OpportunityLineItem>();
for (OpportunityLineItem oli : originOpportunity.OpportunityLineItems)
{
if(oli.PricebookEntry.IsActive)
{
OpportunityLineItem newOLI = oli.clone(false);//new OpportunityLineItem();
newOLI.OpportunityId = BridgeRenewal.Id;
items.add(newOLI);
}
}
String[] insertErrors = GeneralUtil.insertRecords(items, 'OpportunityUtil', 'CreateBridgeRenewal-' + opportunityId, 'High');
} catch (Exception e){
Database.rollback(sp);
ExceptionLogUtil.logException(UserInfo.getUserId(), 'OpportunityUtil', 'CreateBridgeRenewal', new List<String>{''+e+' - ' + opportunityId + ContractTerms}, 'High');
retString = 'There was an error creating BridgeRenewal. Please contact System Administrator. ERROR: ' + e;
}
System.debug(LoggingLevel.Error,'CreateBridgeRenewal() retString =>' + retString);
return retString;
}
Getting error in th eline : Name = originOpportunity.Account.Name + '- BridgeRenewal -Original RTCD ' + datetime.newInstance(originOpportunity.Contract_Start_Date__c, time.newInstance(0, 0, 0, 0)).format('MM/dd/yyyy'),
Name is the API name.
how to get rid of this error
I dont think issue is in line. Normally this issue ome when you acceess wrong relationship or assign wrong Id to a lookup field. Like assing a contact Id to account.
Name = originOpportunity.Account.Name + '- BridgeRenewal -Original RTCD ' + datetime.newInstance(originOpportunity.Contract_Start_Date__c, time.newInstance(0, 0, 0, 0)).format('MM/dd/yyyy'),
I would recommend to use ethis method for exception instance:
getLineNumberInteger : Returns the line number from where the exception was thrown.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_exception_methods.htm
Thanks
Shashikant