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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Please help me with the assignment of field by a t trigger-Urgetnt!!!

Hi all,

     I have this trigger written here. Now i wanna calculate the value of field Age_Of_ Insurance and assign it to the record that is entered.See the bold Line.

trigger Calculation  on Contact (before insert,before update)
{
   String  a='';
   String  month='';
   String  yr='';
   String  x='';
   String  dat='';
   integer m=0;
   integer d=0;
   integer z=0;
   integer i=0;
   integer yar=0;
   integer year=0;
   boolean variable=false;
   for (Contact c: trigger.new)
     {
        If(c.RecordTypeId  == '012O00000000Au3')                          //to make sure that trigger fires only in case of corporate contact record types.
        {
          a=c.SSN_for_Customer__c;                                       //assign ssid__c to a string variable 'a'. 
          if (a.length() != 8)
          {
           c.addError('Please enter SSID in yymmdd-x format');

          }
        else
        {
        month=a.substring(2,4);                                          //fetch string representing month in 'a'.
        dat=a.substring(4,6);                                            //fetch string representing date in 'a'.
        yr=a.substring(0,2);                                             //fetch string representing year in 'a'
        m=integer.valueof(month);                                        //CALCULATE INTEGER VALUE OF MONTH IN SSID
        yar=integer.valueof(yr);                                         //calculate integer value of yy in ssid.
        d=integer.valueof(dat);                                          //CALCULATE INTEGER VALUE OF DATE IN SSID
        x=a.substring(7,8);
        z=integer.valueof(x);                                            //CALCULATE INTEGER VALUE OF X (century).
            if(z==1 || z==2 || z==5 || z==6)
            {
               i=1900;                                                   // calculate century.
            }
            else
            {
               i=2000;
            } 
        year=i+yar;                                                      // to get value of year in YYYY format.
        Date myDate =date.newinstance(year, m, d);                       //create date outh of given integer values.
        Date today=System.today();                                       //fetch todays date from the system.
        integer numbermonths=myDate.monthsBetween(today);                //calculate number of months between two dates
                    if(numbermonths<0)
                    {
                     c.addError('Please enter SSID date lesser than current date in yymmdd-x format');

                    }
                    else
                    {

                      integer addmonth=numbermonths+6;                                 //add 6 to total number of months
                      double y=addmonth/12;                                            //get total number of years in between two dates
                      c.Age_Of_Insurance__c=y;                                         //assign this value to Age_Of _Insurance__c
                      break;
     }
     }
     }
     else
     {
     variable=True;
     }
   }
}

Now I wanna achieve this by writing the trigger after Insert and After Update.

How Can this be done?

Using the same code if I change The actions to after Insert it gives error.

Says

c.Age_Of_Insurance__c iis a read only field.

Whereas at back end its a number field.

ngabraningabrani

You can not modify the fields of contact on which the trigger is being executed, in the after trigger. You will need to set these fields in before trigger only. Why do you want to modify the field in after trigger.