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
gopikrishnagopikrishna 

field not update

trigger changestatus on Selected__c (before insert,before update) {
for(Selected__c sc:trigger.new) {
list<Selected__c> s = new list<Selected__c>([select id,Hours__c from Selected__c where availability__c = :sc.availability__c]);
Availability__c a = [select Hours__c,update_field__c from Availability__c where id=:sc.availability__c ][0];

if(sc.Hours__c <= a.update_field__c )
{
a.update_field__c = a.update_field__c - sc.Hours__c ;
system.debug(a.update_field__c+' ********************');
update a ;

}

}

}

cmlcml

First of all I will suggest you to change layout of your code because you code is having two SOQL and one DML statement within for loop which is bad design and you will hit governor limits.

 

You should not query within for loop and also there should not be any DML statement within for loop.

 

Also please give more information about Objects and fields. Like What are you trying to achieve from 'Selected__c' and 'Availability__c' objects and what are the data types of fields which you are using here or trying to update.

Tejpal KumawatTejpal Kumawat

HI GopiKrishna,

 

 

Use this Code:--

 

 

trigger changestatus on Selected__c (before insert,before update) {
list<Selected__c> lstUpdate = new list<Selected__c>();

Set<Selected__c> setAvali = new Set<Selected__c>();
for(Selected__c sc:trigger.new){
setAvali.add(sc);
}

for(Selected__c s : select id,Hours__c from Selected__c where availability__c = :sc.availability__c){
if(sc.Hours__c <= s.update_field__c )
{
s.update_field__c = s.update_field__c - sc.Hours__c ;
lstUpdate.add(s);
}
}
update lstUpdate ;
}

 

 

 

--------------------
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Thanks