+ Start a Discussion
Shannon Andreas 1Shannon Andreas 1 

Need help with trigger that creates a new contract

So...I created my almost very first trigger...it's not firing. Please help. I pieced together code from different sources and thought I was on the right track...hmmmm

Trying to create a contract when the checkbox "Ready for Contract" is checked. Do I need the opportunity ID as well? Not sure why this is not working. Thanks in advance for help. Shannon

trigger CreateContract on Opportunity (before insert) {
 private List<Contract> ctr = new List<Contract>();
  
  for(Opportunity o : Trigger.new) {
    if(o.Ready_for_Contract__c = true) {
      Contract c = new Contract(Id = o.Account.Id,
                                Name = o.Name,
                                Status = 'Activated',
                                Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
                                StartDate = o.Contract_Start_Date__c,
                                Payment_Status__c = 'Ready to Be Invoiced');
      ctr.add(c);
      }
      insert ctr;
      }
      }
William TranWilliam Tran
If you are inserting, you do not need to set the id.  

You probably want to set the accountID.

Try code below (not sure, but you might have other errrors, we'll see :-)
 
for(Opportunity o : Trigger.new) {
    if(o.Ready_for_Contract__c = true) {
      Contract c = new Contract(AccountId = o.Account.Id,
                                Name = o.Name,
                                Status = 'Activated',
                                Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
                                StartDate = o.Contract_Start_Date__c,
                                Payment_Status__c = 'Ready to Be Invoiced');
      ctr.add(c);
      }
      insert ctr;
      }
      }

 
hitesh90hitesh90
Hello Shannon,

There are few mistakes in your code.
1. In If condition use "==" instead of "=".
2. DML statement (insert ctr) written inside for loop.
3. trigger should be on "after insert" event.

Try to use following code.

Apex Trigger:
trigger CreateContract on Opportunity (after insert) {
    private List<Contract> ctr = new List<Contract>();
    for(Opportunity o : Trigger.new) {
        if(o.Ready_for_Contract__c == true) {
            Contract c = new Contract(AccountId = o.AccountId,
            Name = o.Name,
            Status = 'Draft',
            Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
            StartDate = o.Contract_Start_Date__c,
            Payment_Status__c = 'Ready to Be Invoiced');
            ctr.add(c);
        }        
    }
    if(!ctr.isEmpty()){
        insert ctr;
    }
}

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
Shannon Andreas 1Shannon Andreas 1
Ughh...still not working. Can you help me with a test class? Maybe I can troubleshoot that way? I seem to recall being able to debug code in the Dev Console?
hitesh90hitesh90
Hello Shannon,

Which issue is coming now? can you please post the error message here?

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
Shannon Andreas 1Shannon Andreas 1
There is no error. It's not firing. It's seems so simple! I know that it was doing something earlier. Not sure what has changed?
Shannon Andreas 1Shannon Andreas 1
It is not creating the contract.
Andrei KuznetsovAndrei Kuznetsov
One thing I noticed you are assigning Account Id on Oppt record to Contract Id (which is primery key and should be generated automatically):
Contract c = new Contract(Id = o.Account.Id
 
Andrei KuznetsovAndrei Kuznetsov
I did some changes to the trigger in my system and it's working:

trigger AndreiTest on Opportunity (before insert) {
 private List<Contract> ctr = new List<Contract>();
 
  for(Opportunity o : Trigger.new) {
    if(o.Ready_for_Contract__c = true) {
      Contract c = new Contract(Name = o.Name,
                                Status = 'Draft',
                                Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
                                StartDate = o.Contract_Start_Date__c,
                                Payment_Status__c = 'Ready to Be Invoiced'
                                ,AccountId = o.AccountId);
        ctr.add(c);
      }
      insert ctr;
     }    
}
William TranWilliam Tran
Andrei,

it looked like you pretty much went in full circle, your final code looked like what I proposed :-)

Thx

Yours:
 
for(Opportunity o : Trigger.new) {
    if(o.Ready_for_Contract__c = true) {
      Contract c = new Contract(Name = o.Name,
                                Status = 'Draft',
                                Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
                                StartDate = o.Contract_Start_Date__c,
                                Payment_Status__c = 'Ready to Be Invoiced'
                                ,AccountId = o.AccountId);
        ctr.add(c);
      }
      insert ctr;
     }

Mine:

If you are inserting, you do not need to set the id.  

You probably want to set the accountID.
 
for(Opportunity o : Trigger.new) {
    if(o.Ready_for_Contract__c = true) {
      Contract c = new Contract(AccountId = o.Account.Id,
                                Name = o.Name,
                                Status = 'Activated',
                                Total_Contract_Value__c = o.Total_Sales_Price_of_Products__c,
                                StartDate = o.Contract_Start_Date__c,
                                Payment_Status__c = 'Ready to Be Invoiced');
      ctr.add(c);
      }
      insert ctr;
      }

 
William TranWilliam Tran
Ohh, the original ask is from Shannon :-)  Too funny.

Thx
Shannon Andreas 1Shannon Andreas 1
Okay guys...still not creating a contract. Just want to clarify. The oportunity is already created. The docusign logic states that when an envelope is completed, it will check the box on the oppty for Ready for Contract. Therefore, would the trigger still be a before insert? Or would it be something else? Before insert means we have a new opportunity and if the checkbox is checked, the trigger should fire? Or does it mean before we insert a new contract, check to see that the checkbox is checked? 

 
Vijay NagarathinamVijay Nagarathinam
​Hi Shannon,

Try the following code, It will be helpful to you.
 
trigger createContract on Opportunity (before insert) {
    List<Contract> contractList = new List<Contract>();
    for(Opportunity Opp : Trigger.new){
        if(opp.Ready_for_Contract__c == True){
           Contract newContract = new Contract();
           newContract.AccountId = Opp.AccountId;
           newContract.StartDate = System.Today();
           newContract.ContractTerm = 10;
           newContract.SpecialTerms = 'Vijay Test Contract';
           contractList.add(newContract);
       }
    }
    if(contractList.size() > 0){
        insert contractList;
    }
}




 
Shannon Andreas 1Shannon Andreas 1
Thanks Vijay. I did find a solution...here is the code:

trigger CreateContract on Opportunity (before update)
{
    List<Contract> ctr = new List<Contract>();
    
      for(Opportunity o : Trigger.new) 
      {
        if(o.Ready_for_Contract__c == true) 
        {
             Contract c = new Contract(Name = o.Name,
             Status = 'Draft',
             Total_Contract_Value__c = o.Amount,
             StartDate = o.Contract_Start_Date__c,
             Payment_Status__c = 'Ready to be Invoiced',
             AccountId = o.AccountId,
             Opportunity_Name__c = o.Id);
             ctr.add(c);
         }
      }
      if(ctr.size() > 0)
      {
            System.debug('-ctr------->'+ctr.size());
            insert ctr;
      }     
}

Now I have other issues! I would be greatful for your help with this post:
 
Shannon Andreas 1Shannon Andreas 1
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BU10IAG