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
David Corns 16David Corns 16 

Looking for help with a simple trigger on the Contract object

We have a custom picklist field on the Contract object and we are looking for help with a trigger that during the new Contract creation process will upon selecting a Contract custom picklist field particular value autopopulate the standard Contract Term (months) number field with a particular value e.g. 12. All this prior to saving the record.
Best Answer chosen by David Corns 16
Maharajan CMaharajan C
Hi David,

You have to fill all required fields data in the UI while creating the record(Required field are shows with red mark) then if there is any validation rules in Contract object it will check your data entered in the screen so please check the system validations(Required fields, Data Type for data you entered) and custom validations(Validation rules created by you : setup --> Contract --> Validation Rules) in Contract object.

In Salesforce we have order of execution while you save the records. So first the Validations will fire in the background there after only the Trigger will fire.

so in contract object there is Some Standard fields are always madatory : Account Name,Contract Start Date,Contract Term (months).

so you must have to fill the some dummy value in Contract Term fields while save but it will changed as per your trigger value. So i think there is no use of trigger here to prepoulate the value.But you can keep the some static value here by using trigger. 

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj

All Answers

Rahul.MishraRahul.Mishra
Hi David,

Here is the sample code
 
trigger populateContract on Contract (before insert)  {

  for(Contract cntr : trigger.new ) { 
     if(cntr.apiNameOfYourField == 'Perticular Value') {
	 
	 cntr.ContractTerm = 12;
	 }  
  }
}

Update populateContract with api name of your custom field and 'Perticular Value'​ with the picklist value which you are checking in criteria

Mark answer as solved if it does help you.


 
David Corns 16David Corns 16
Thank you Rahul,
I updated as follows but received the error message below:

trigger Type_of_Contract__c on Contract (before insert)  {

  for(Contract cntr : trigger.new ) { 
     if(cntr.Type_of_Contract__c == 'NDA') {
     
     cntr.ContractTerm = 12;
     }  
  }
}

"Error: Compile Error: Invalid character in identifier: Type_of_Contract__c at line 1 column 9"

Any thoughts as to where I am tripping up?
Thanks again.
Maharajan CMaharajan C
Hi David,

change the Trigger Name:

trigger TypeofContract on Contract (before insert)  {

  for(Contract cntr : trigger.new ) { 
     if(cntr.Type_of_Contract__c == 'NDA') {
     
     cntr.ContractTerm = 12;
     }  
  }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj
David Corns 16David Corns 16
Thanks Raj, I made that change but when I tested it out in my Sandbox by creating a new Contract, I received the standard error message "Error: You must enter a value" on the mandatory standard Contract Term field. Any thoughts as to why the trigger is not updating that field to the value 12?
Rahul.MishraRahul.Mishra
Hi  David,

Since that field is the required field, initially you have to enter some values in it , after record save value will be updated with 12.
Reason of validation error is, system validation runs before trigger.

Thanks,
Rahul
Maharajan CMaharajan C
Hi David,

You have to fill all required fields data in the UI while creating the record(Required field are shows with red mark) then if there is any validation rules in Contract object it will check your data entered in the screen so please check the system validations(Required fields, Data Type for data you entered) and custom validations(Validation rules created by you : setup --> Contract --> Validation Rules) in Contract object.

In Salesforce we have order of execution while you save the records. So first the Validations will fire in the background there after only the Trigger will fire.

so in contract object there is Some Standard fields are always madatory : Account Name,Contract Start Date,Contract Term (months).

so you must have to fill the some dummy value in Contract Term fields while save but it will changed as per your trigger value. So i think there is no use of trigger here to prepoulate the value.But you can keep the some static value here by using trigger. 

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
This was selected as the best answer
David Corns 16David Corns 16
Thank you Raj and Rahul for clarifying. Much appreciated!