function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ember CampbellEmber Campbell 

Help with Triggers - Create a new Opportunity off of a Contract update

Hello!

I've just started to try to use triggers and need some help.  I thought this would be a pretty quick and easy thing to do, but I've been working on this same issue for a while now and I'm at a loss.  I'm including my draft code but it maybe beyond saving.  FYI - I did talk to SF support and found out that they can't help with detailed coding questions like this....  I hoping he community can help me :)

Here is the need: 

Create a new Opportunity when a Contract record is updated, specifically the field named 'Current Term Expiration Date' is updated.

On the new opportunity, I want to prepopulate data from the contract record
  • Record Type = ‘Existing Business’  (01280000000AT8P)
  • Service Offering = Contract field ‘Service_Lines__c’
  • Opportunity name = Create a new opportunity name.
    • Correct format: Dept # – Account Name – Service Line – Renewal (CTE Year)
    • Fields: ‘Department__c’ – ‘Account’ – ‘Service_Lines__c’ – Renewal (Year(‘Current_Term_Expiration_Date__c’)
      • Example for demonstration purposes only   4444 – Houston Methodist Hospital – IONM – Renewal (2014)
  • Stage ‘StageName’= ‘Renewal Notification’
  • Model Type  ‘Model_Type__c’= ‘Renewal’

Here is the code that I commented out – (otherwise I couldn’t save)

// Create the Opportunity record only when CTE is Updated

trigger CreateOpportunity on Contract (after update) {
//List<Opportunity> newOpps = new List <Opportunity>();
//for (Opportunity opp : Trigger.new);
//if (contract.Current_Term_Expiration_Date__c ==isChanged) {
//Opportunity Opp = new Opportunity();   
//opp.Opportunity_Name__c = ‘Department__c’ – ‘Account’ – ‘Service_Lines__c’ – Renewal (Year(‘Current_Term_Expiration_Date__c’)
//opp. RecordType =‘Existing Business’  (01280000000AT8P)
//opp. Service_Line__c=Contract. ‘Service_Lines__c’
//opp. StageName= ‘Renewal Notification’
//opp. Model_Type__c’= ‘Renewal’

}
    // Inserting the New Opportunity Record.

//try {
     //   insert opptoinsert;
  //  } catch (system.Dmlexception e) {
     //   system.debug (e);
   }

Any help would be greatly appreciated
Raghavendra ARaghavendra A
Hi Ember,

First of all the entire code is commented, just to bring to your notification. And in the entire code if we ignore comments the following lines have missing semi-colons at the end:

//opp. RecordType =‘Existing Business’  (01280000000AT8P)
//opp. Service_Line__c=Contract. ‘Service_Lines__c’
//opp. StageName= ‘Renewal Notification’
//opp. Model_Type__c’= ‘Renewal’

For the statement recordtype you should fetch the record type id from the system and assign it to opp.recordtypeid. For ex: opp. RecordType =01280000000AT8P;  

Try the above code first, if that works you can dynamically fetch the Recordtype if for Existing Business. 

Let me know if this helps.

Regards,
Raghu

Ember CampbellEmber Campbell
Hi Raghu,

I did comment out the code because I couldn't save without commenting out the code.  I've lost two or three versions I was working on before I learned about comments. 

Thanks for the suggestion on record type. I can remove the reference to ‘Existing Business’ if the record id would work.  Do you have any other suggestions since I’m very new?  I’ve seen folks talk about references to maps and loops.  I’m not sure about those or the debugging code on the end.  I borrowed that from another user in the community.

Regards,
Ember