0

we use taskMatch to find the records needed to be updated,

how can we update them? //dml below

List<Account> allaccounts = [SELECT phone from Account WHERE Status_Reason__c = 'Active - Existing' 
AND phone != null];
Set<String> accountSet = new Set<String>();
for(account a: allaccounts) accountSet.add(a.phone);
List<Task> taskMatch = [SELECT Source_Number_Tech__c, whatid from Task WHERE Source_Number_Tech__c IN :accountSet AND whatid != null];
if (taskMatch.size() >0) {
    //DML update taskMatch.whatid with accountSet.id
}
1

I think using a Map will do the trick here. Below is the code

Map<String, Id> phoneWithId = new Map<String, Id>(); //use this map to store the Id against the Phone
for(Account a : [SELECT Id, Phone from Account WHERE Status_Reason__c = 'Active - Existing' AND phone != null]){
    phoneWithId.put(a.Phone, a.Id);
}
//Provide the KeySet() of the Phone from Map
Task[] taskToUpdate = [SELECT Source_Number_Tech__c, whatid from Task WHERE Source_Number_Tech__c IN :phoneWithId.keySet() AND whatid != null]
for(Task t: taskToUpdate){
    t.WhatId = phoneWithId.get(t.Source_Number_Tech__c);//get and set the Id from Map
}
update taskToUpdate;
| improve this answer | |
  • 1
    Oh thanks very much, i figured we need a map of id and phone, could not put the finger on the right code. thank you! – Ran Algov Feb 7 '19 at 8:02
  • cool, Can you please mark this as a answer to this question. – Tarique Shamim Feb 7 '19 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.