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
CruzyCruzy 

Having trouble creating trigger to create Opportunities from expiring contract

Hi, I have recently started using Apex triggers with little knowledge of Java before hand and brief history of using visual basic to a moderate level.

 

I am trying to create an Apex trigger to fire when a contract is coming up to expire (I have created a workflow that sets a custom checkbox on contracts to flag it as expiring and am planning to fire it from this). So far I have come up with this, any help would be most appreciated:

 

 

trigger CreateNewOpp on Contract (after update, after insert) {


If (Contract.Contract_Renewal__c = true) {
List <Opportunity> OpportunityToInsert = new List <opportunity> ;
If (Contract.Contract_Renewal__c = true) {
For (Contract C : Trigger.new) {
Opportunity O = new Opportunity () ;
o.accountID = c.accountID ;
o.name = c.name && ' - Renewal' ;
o.ownerid = c.ownerid ;
o.stagename = 'Prospecting' ;
o.closedate = c.Expiration_Of_Contract__c + '365' ;
}
OpportunityToInstert.add (o) ;
}

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

Suresh RaghuramSuresh Raghuram

 

you missed == in if condition, change the variable names to small case example O, C. 

then execute it. Even if you have question post them, If this solves your problem make this as a solution.

trigger CreateNewOpp on Contract (after update, after insert) {


If (Contract.Contract_Renewal__c == true) {
List <Opportunity> OpportunityToInsert = new List <opportunity> ;
If (Contract.Contract_Renewal__c == true) {
For (Contract C : Trigger.new) {
Opportunity O = new Opportunity () ;
o.accountID = c.accountID ;

o.name = c.name &&' - Renewal' ;
o.ownerid = c.ownerid ;
o.stagename = 'Prospecting' ;
o.closedate = c.Expiration_Of_Contract__c + '365' ;
}
OpportunityToInstert.add (o) ;
}

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