+ Start a Discussion
Leonardo Girgenti 5Leonardo Girgenti 5 

Help with a simple trigger

Dear all,
I have 2 custom fields on the Contract object. 
Contract Status and Contract Validity

I need to ensure that everytime the Contract record is edited and saved, the Contract Status and Contract Validity fields are updated in the corresponding custom fields on the Account Object. 
I need trigger and test class!!
Rajiv Bhatt 16Rajiv Bhatt 16
What have you already tried? What is the issue that you are facing? I dont think anyone will write the whole code for you.
Hi Leonardo,
Rajiv is right ,You shuold ask to write code in forum .
But next time onwards try with some approch and let us know if you are facing any issue .If we will write code then it will be difficult to manage forum for future .I know you are NewBee still so many blogs and materials are there to follow .
Please try with below code it will help you ,
trigger UpdateFromAccount on Contract (before update) {
    Set<id> accountIdSet=new Set<Id>();
    for(Contract  cont : Trigger.new){
    Map<id,Account> accountIdMap=new Map<Id,Account>([SELECT id,Account_Status__c,Account_Validity__c FROM Account WHERE Id IN : accountIdSet]);
    for(Contract  cont : Trigger.new){
        if(accountIdMap.get(cont.AccountId).!= null)
        if(accountIdMap.get(cont.AccountId).Account_Validity__c!= null)
public class UpdateFromAccountTest{
public static testMethod void unitTest(){
   Account acc=new Account();
	  acc.Account_Status__c='testStatus';//If picklist add the picklist value 
	  Insert acc;
	Contract cont=new Contract();
		insert cont;
		update cont;

Please follow below links which help you to write trigger and test class.