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 Answer 1

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;
2
  • 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
    Commented Feb 7, 2019 at 8:02
  • cool, Can you please mark this as a answer to this question. Commented Feb 7, 2019 at 9:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .