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
SF Beginner 2019SF Beginner 2019 

being overide by first record

I have these class wherein when I create or update the StatusGet__c to Completed it works, wherein it checks the (Indicator1__c)checkbox, and when I create != Completed it Unchecks(Indicator1__c), but my problem is that everytime I craete a new record  != Complete it overrides the checkbox, wherein what I want is that if there is a record StatusGet__c = Completed on the list of record the Indicator1__c will still be checked even if they add records != compelted

public class TaskTriggerHandles{
    public void  getAcct(List<Task> taskacct) {
        List<Account> accountsToUpdate = new List<Account>();
        
        taskacct = [SELECT Id, whatid,StatusGet__c FROM Task WHERE Id IN :taskacct];
        system.debug('taskacct' + taskacct );
        for (Task p : taskacct) {
        if(p.StatusGet__c ==  'Completed ')
           {
                Account a = new Account(
                    Id = p.whatid,
                    Indicator1__c= true);
                
                accountsToUpdate.add(a);
            system.debug('a' + a);
       }
        else if(p.StatusGet__c !=  'Completed')
        {
         Account a = new Account(
                    Id = p.whatid,
                    Indicator1__c= false);
                
                accountsToUpdate.add(a);
            system.debug('a' + a);
        }
        update accountsToUpdate;
        system.debug('accountsToUpdate' + accountsToUpdate);
         }
    }
}
Raj VakatiRaj Vakati
You dnt need else block i guess and  use this code

 
public class TaskTriggerHandles{
    public void  getAcct(List<Task> taskacct) {
        List<Account> accountsToUpdate = new List<Account>();
        for (Task p : taskacct) {
        if(p.StatusGet__c ==  'Completed ')
           {
                Account a = new Account(
                    Id = p.whatid,
                    Indicator1__c= true);
                
                accountsToUpdate.add(a);
            system.debug('a' + a);
       }
        
        update accountsToUpdate;
        system.debug('accountsToUpdate' + accountsToUpdate);
         }
    }
}

 
SF Beginner 2019SF Beginner 2019
it doesn't work, when I try to update/delete the status, the value doesn't get unchecked.
Raj VakatiRaj Vakati
On short notes  , can you give me all your conditions so that i will be able rewrite it 
SF Beginner 2019SF Beginner 2019
ok here it goes, if StatusGet__c  = Completed = checks the indicator field on the account  but as long as he see a record wherein it has StatusGet__c  = Completed the indicator field on the account will still be checked. regardless if there are many records without that StatusGet__c  =! Completed. but if ever he doesn't see a record StatusGet__c  =  Completed the checkbox will be unchecked already, or if the record is update from StatusGet__c  != Completed without any record having StatusGet__c  = Completed..
SF Beginner 2019SF Beginner 2019
help
Raj VakatiRaj Vakati
try tis
public class TaskTriggerHandles{
    public void  getAcct(List<Task> taskacct) {
        List<Account> accountsToUpdate = new List<Account>();
        Set<Id> ids = new Set<Id>(); 
        taskacct = [SELECT Id, whatid,StatusGet__c FROM Task WHERE Id IN :taskacct];
        system.debug('taskacct' + taskacct );
        for (Task p : taskacct) {
        if(p.StatusGet__c ==  'Completed ')
           {
			   if(!ids.contain(p.whatid)){
					Account a = new Account(
						Id = p.whatid,
						Indicator1__c= true);
					ids.add(p.whatid);
					accountsToUpdate.add(a);
			   }
          
       }
        else if(p.StatusGet__c !=  'Completed')
        {
			if(!ids.contain(p.whatid)){
			Account a = new Account(
                    Id = p.whatid,
                    Indicator1__c= false);
                ids.add(p.whatid);
                accountsToUpdate.add(a);
			}
            system.debug('a' + a);
        }
        update accountsToUpdate;
        system.debug('accountsToUpdate' + accountsToUpdate);
         }
    }
}

 
SF Beginner 2019SF Beginner 2019
it doesnt work still.

what happen is that

record 1 on task = completed
record 2 on task = incomplete
record 3 on task = test

the indicator checkbox should still be checked.
if we add another record wherein != completed the checkbox should still be checked
and if we add = completed the checkbox is still checked.
but if we delete the completed and no task = completed is existing on a list this will unchedk the checkbox.
 
SF Beginner 2019SF Beginner 2019
help
SF Beginner 2019SF Beginner 2019
help