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
Vidya RaniVidya Rani 

For new records: Copying value from previous record in trigger.

In case of record creation,am trying to copy value in field "Next call Notes" from a previous record (created by same user on same Account). Unbale to create new record saying : Record is read-only: Trigger.CopyCallNotes: line 26, column 1
Below is code piece. Any help will bereally helpful.

trigger CopyCallNotes on Call__c (after insert, after update) {

public String CurrAcc;
public Id CurrCreatedBy;


 for(Call__c c: Trigger.new){
        CurrAcc = c.Account__c;
        CurrCreatedBy = c.CreatedById;
        System.debug('CAccount' + c.Account__c);
        System.debug('CCreatedBy' + c.CreatedById);
        System.debug('CName' + c.Name);
        System.debug('CId' + c.Id);
                      } 

List<Call__c> listaToTrue = new List<Call__c>([select Next_Call_Notes__c from Call__c where Account__c = : CurrAcc and CreatedById = :CurrCreatedBy order by CreatedDate desc LIMIT 2]);
       System.debug('ListSize' + listaToTrue.size());
       System.debug('Listvalue' + listaToTrue.get(1)); 

   String NCN = string.valueOf(listaToTrue.get(1).Next_Call_Notes__c);
   System.debug('NCNvalue' + NCN);
        for(Call__c a: Trigger.new){
         a.Next_Call_Notes__c = NCN;   // line 26
         System.debug('CNCN' + a.Next_Call_Notes__c);
         }                   
                      

}
Raj VakatiRaj Vakati
Change it before events so it will work
 
trigger CopyCallNotes on Call__c (before insert, before update) {

public String CurrAcc;
public Id CurrCreatedBy;


 for(Call__c c: Trigger.new){
        CurrAcc = c.Account__c;
        CurrCreatedBy = c.CreatedById;
        System.debug('CAccount' + c.Account__c);
        System.debug('CCreatedBy' + c.CreatedById);
        System.debug('CName' + c.Name);
        System.debug('CId' + c.Id);
                      } 

List<Call__c> listaToTrue = new List<Call__c>([select Next_Call_Notes__c from Call__c where Account__c = : CurrAcc and CreatedById = :CurrCreatedBy order by CreatedDate desc LIMIT 2]);
       System.debug('ListSize' + listaToTrue.size());
       System.debug('Listvalue' + listaToTrue.get(1)); 

   String NCN = string.valueOf(listaToTrue.get(1).Next_Call_Notes__c);
   System.debug('NCNvalue' + NCN);
        for(Call__c a: Trigger.new){
         a.Next_Call_Notes__c = NCN;   // line 26
         System.debug('CNCN' + a.Next_Call_Notes__c);
         }                   
                      

}

 
Vidya RaniVidya Rani
I tried but now m getting error: List Index out of bound for line: System.debug('Listvalue' + listaToTrue.get(1));
Tried with :System.debug('Listvalue' + listaToTrue.get(0)); whereever applicable, still errors.
I think m also missing update command. 
Steven NsubugaSteven Nsubuga
I have failed to figure out some aspects of your code. As a result I have not been able to bulkify the trigger. Try this:
trigger CopyCallNotes on Call__c (before insert, before update) {

	public String CurrAcc;
	public Id CurrCreatedBy;

	for(Call__c c: Trigger.new){
		accountIds.add(c.Account__c);
		CreatedByIds.add(c.CreatedById;);
		CurrAcc = c.Account__c;
		CurrCreatedBy = c.CreatedById;
		System.debug('CAccount' + c.Account__c);
		System.debug('CCreatedBy' + c.CreatedById);
		System.debug('CName' + c.Name);
		System.debug('CId' + c.Id);

		List<Call__c> listaToTrue = new List<Call__c>([select Next_Call_Notes__c from Call__c where Account__c = : CurrAcc and CreatedById = :CurrCreatedBy order by CreatedDate desc LIMIT 2]);
		System.debug('ListSize' + listaToTrue.size());
		System.debug('Listvalue' + listaToTrue.get(0));
		String NCN = string.valueOf(listaToTrue.get(0).Next_Call_Notes__c);
		c.Next_Call_Notes__c = NCN;
		System.debug('CNCN' + c.Next_Call_Notes__c);
	}
}

 
Vidya RaniVidya Rani
It worked after adding "update" command at the end but I did more chnages to cover other senarios and now the update is not happening without error.
Line 40 is showing null values.Any suggestions.
  1. trigger CopyCallNotes on Call__c (after insert, after update) {
  2.  
  3. public String CurrAcc;
  4. public Id CurrCreatedBy;
  5.  
  6.  
  7.  for(Call__c c: Trigger.new){
  8.         CurrAcc = c.Account__c;
  9.         CurrCreatedBy = c.CreatedById;
  10.         System.debug('CAccount' + c.Account__c);
  11.         System.debug('CCreatedBy' + c.CreatedById);
  12.         System.debug('CName' + c.Name);
  13.         System.debug('CId' + c.Id);  } 
  14.  
  15. List<Call__c> listaToTrue = new List<Call__c>([select Next_Call_Notes__c from Call__c where Account__c = : CurrAcc and CreatedById = :CurrCreatedBy order by CreatedDate desc LIMIT 2]);
  16.  
  17.     System.debug('ListSize' + listaToTrue.size());
  18.     public String NCN;
  19.    if(listaToTrue.size() > 1 ) 
  20.   {    
  21.    String NCN = string.valueOf(listaToTrue.get(1).Next_Call_Notes__c);
  22.       System.debug('NCNvalueG' + NCN);
  23.    }
  24.    else
  25.    {
  26.    String NCN = string.valueOf(listaToTrue.get(0).Next_Call_Notes__c);
  27.       System.debug('NCNvalueL' + NCN);
  28.    } 
  29.         List<Id> CallIds = new List<Id>();
  30.         
  31.             for (Call__c oCall : trigger.new) {
  32.              if (oCall.Account__c != null) {
  33.                 CallIds.add(oCall.Id);
  34.                 System.debug('oCallValue' + CallIds);
  35.              }
  36.       }   
  37.         List<Call__c> listCalls = [SELECT id,Next_Call_Notes__c,Name from Call__c WHERE Id in :CallIds];
  38.         For (Call__c oCall : listCalls ) {
  39.             oCall.Call_Notes__c = NCN; 
  40.             System.debug('oCallNotes' + oCall.Call_Notes__c ); 
  41.             System.debug('ListCalls' + listCalls.get(0) );
  42.          }
  43.        if(trigger.isinsert)
  44.         {
  45.             update listCalls;
  46.         }