You need to sign in to do that
Don't have an account?
AntonyWarc
Help tweak opp creation trigger
Hi all,
The below trigger was written for us to auto-create a renewal opportunity when a contract is saved. All is fine except we are now in the position of needing more than one product line item on an opp. When testing this, I get an error saying more than one product line item found (error is triggered by line 53). What do I need to change to get this code to select all product line items from the original opp and insert into the new opp?
Thanks in advance,
Antony
The below trigger was written for us to auto-create a renewal opportunity when a contract is saved. All is fine except we are now in the position of needing more than one product line item on an opp. When testing this, I get an error saying more than one product line item found (error is triggered by line 53). What do I need to change to get this code to select all product line items from the original opp and insert into the new opp?
Thanks in advance,
Antony
trigger ContractRenewalOpportunityCreate on Contract (after insert) { Contract contract = Trigger.new[0]; //if contract was created with'Create Contract' button on opportunity then create renewal opportunity if (contract.Create_Renewal_Opportunity__c == true) { Opportunity currentOpp = [SELECT Name, AccountId, Account.Name, Account.Warc_SubRef__c, Contact__r.Name, Contact__c, COF_Description__c, Description, Large_Agency__c, Budget_Assumption__c, Budgeted_Value_Local__c, Budgeted_Value_Assumption__c, Original_Budgeted_amount__c, Renewal_Forecast_Value__c, Original_Budgeted_local_amount__c, Last_Year_Local_value__c, Last_Year_GBP_value__c, Subscription_Type__c, Local_Client_Service_Manager__r.Name, Local_Client_Service_Manager__c, Sales_Manager__r.Name, Sales_Manager__c, Currency__c, CurrencyISOCode, CloseDate, LeadSource, Master_Password__c, Master_Username__c, Year_1__c, VAT_Code__c, Type, (SELECT OpportunityId, PricebookEntryId, Quantity, ServiceDate, Description, UnitPrice FROM OpportunityLineItems) FROM Opportunity WHERE Id=:contract.Opportunity__c]; OpportunityLineItem currentOppLineItem = currentOpp.OpportunityLineItems; //calc renewal opportunity close date Date renewalOppCloseDate; String RenewalOppYr1; //If Ren opp is to be created from New business opp if(currentOpp.Type == 'New Business'){ RenewalOppYr1 = 'Yes'; renewalOppCloseDate = currentOpp.CloseDate.addYears(1).addMonths(1); renewalOppCloseDate = Date.newInstance(renewalOppCloseDate.Year(), renewalOppCloseDate.Month(), 1); } else if(currentOpp.Type == 'Existing Business'){ RenewalOppYr1 = 'No'; renewalOppCloseDate = currentOpp.CloseDate.addYears(1); } //Build renewal opportunity name if(renewalOppCloseDate != null){ String renewalOppName = contract.id + '-Ren' + renewalOppCloseDate.year(); if (renewalOppCloseDate.month() < 10) { renewalOppName += '0' + renewalOppCloseDate.month(); } else { renewalOppName += renewalOppCloseDate.month(); } //Fathom edit (tmh): get record type ID for Renewal record type Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Renewal').getRecordTypeId(); //create renewal opportunity Opportunity renewalOpp = new Opportunity(Name=renewalOppName, Type='Existing Business', Subscription_Type__c=currentOpp.Subscription_Type__c, Description=currentOpp.Description, COF_Description__c=currentOpp.COF_Description__c, Large_Agency__c=currentOpp.Large_Agency__c, Budget_Assumption__c=currentOpp.Budget_Assumption__c, Budgeted_Value_Local__c=currentOpp.Budgeted_Value_Local__c, Budgeted_Value_Assumption__c=currentOpp.Budgeted_Value_Assumption__c, Original_Budgeted_amount__c=currentOpp.Original_Budgeted_amount__c, //Renewal_Forecast_Value__c=currentOpp.Renewal_Forecast_Value__c, Original_Budgeted_local_amount__c=currentOpp.Original_Budgeted_local_amount__c, Last_Year_Local_value__c=currentOpp.Last_Year_Local_value__c, Last_Year_GBP_value__c=currentOpp.Last_Year_GBP_value__c, Local_Client_Service_Manager__c=currentOpp.Local_Client_Service_Manager__c, Contact__c=currentOpp.Contact__c, Sales_Manager__c=currentOpp.Sales_Manager__c, Currency__c=currentOpp.Currency__c, CurrencyISOCode=currentOpp.CurrencyISOCode, AccountId=currentOpp.AccountId, CloseDate=renewalOppCloseDate, StageName='D - Interested', // Fathom Edit (tmh): Change from 'Renewal M/Y' LeadSource='Renewal', Year_1__c = RenewalOppYr1, //AR Additoon 300117 Master_Password__c=currentOpp.Master_Password__c, Master_Username__c=currentOpp.Master_Username__c, VAT_Code__c=currentOpp.VAT_Code__c, renewal_category__c = 'Anticipated Yes', // Fathom Edit (tmh): add the renewal category to the opp. recordTypeId = devRecordTypeId // Fathom Edit (tmh): add the record type ); insert renewalOpp; //create add product to renewal opportunity OpportunityLineItem renewalOppLineItem = new OpportunityLineItem(OpportunityId=renewalOpp.Id, PricebookEntryId=currentOppLineItem.PricebookEntryId, Quantity=currentOppLineItem.Quantity, ServiceDate=currentOppLineItem.ServiceDate, Description=currentOppLineItem.Description, UnitPrice=currentOppLineItem.UnitPrice); insert renewalOppLineItem; } } }
Apex trigger ContractRenewalOpportunityCreate caused an unexpected exception, contact your administrator: ContractRenewalOpportunityCreate: execution of AfterInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject: Trigger.ContractRenewalOpportunityCreate: line 48, column 1