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
SFDC_LearnerSFDC_Learner 

Updating same object field in trigger.new (before update) call

trigger AccDescriptionUpdate on Account (before update) {
set<Id> setIds = new set<Id>();
for(Account objA : trigger.new){
setIds.add(objA.id);
}
system.debug('----set Ids are --->'+setIds);

List<Account> lstA = [select id,name,(select id,name from Contacts) from Account where id in : setIds];
system.debug('---lstA is --->'+lstA);
String sDesc ='';

for(Account objA : lstA){
Integer i= objA.contacts.size();
integer j=1;
for(Contact objC : objA.contacts){
if(j <= i-1){
sDesc = sDesc + objC.name + ',';
j++;
}
else{
sDesc = sDesc + objC.name;
j++;
}



}
system.debug('---sDesc is --->'+sDesc);
objA.description = sDesc; // In before insert why its not updating to my account.
system.debug('---objA.description is --->'+objA.description);
}

}

 

how can i update the description file in before update event.

Laxman RaoLaxman Rao

I have modified the code, try this :

 

trigger AccDescriptionUpdate on Account (before update) {
set<Id> setIds = new set<Id>();
for(Account objA : trigger.new){
setIds.add(objA.id);
}
system.debug('----set Ids are --->'+setIds);

List<Account> lstA = [select id,name,(select id,name from Contacts) from Account where id in : setIds];
system.debug('---lstA is --->'+lstA);
String sDesc ='';

for(Account objA : lstA){
Integer i= objA.contacts.size();
integer j=1;
for(Contact objC : objA.contacts){
if(j <= i-1){
sDesc = sDesc + objC.name + ',';
j++;
}
else{
sDesc = sDesc + objC.name;
j++;
}

 

}
system.debug('---sDesc is --->'+sDesc);
Account acc = trigger.newMap.get(objA.Id); //you have to change the value which is the in the trigger i.e Account present in the trigger.new or trigger.newMap
acc.description = sDesc; // In before insert why its not updating to my account.
system.debug('---acc.description is --->'+acc.description);
}

}

 

Hope it helps.

SFDC_LearnerSFDC_Learner

It causes,

 System.FinalException: Record is read-only

 

.

 

 

Laxman RaoLaxman Rao

Might be your trigger is calling for after update also I think.

 

Can you just copy the above code in new account trigger and test it once.

It works for me.