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
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!!
Thanks 
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.
ManojjenaManojjena
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){
        accountIdSet.add(cont.Id);
    }
    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)
            cont.Contract_Status__c=accountIdMap.get(cont.AccountId).Account_Status__c;
        if(accountIdMap.get(cont.AccountId).Account_Validity__c!= null)
            cont.Contract_Validity__c=accountIdMap.get(cont.AccountId).Account_Validity__c;
    }
}
//
@isTest
public class UpdateFromAccountTest{
public static testMethod void unitTest(){
   Account acc=new Account();
      acc.Name='TestAccount';
	  acc.Account_Status__c='testStatus';//If picklist add the picklist value 
	  acc.Account_Validity__c=10;
	  Insert acc;
	Contract cont=new Contract();
		cont.AccountId=acc.Id;
		insert cont;
		update cont;
	}
}

Please follow below links which help you to write trigger and test class.
http://manojjena20.blogspot.in/2013/03/manojs-trigger-blog.htm
http://www.salesforcetutorial.com/apex-trigger-create-simple-trigger/
http://www.sfdc99.com/2014/04/06/example-how-to-write-an-advanced-trigger/


https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BBAZIA4