All the hardwork for finding OwnerId is unnessessary because the error is coming because of WhatId, i just task.ownerid=opp.ownerid, and it work like a charm. Please note that I am not condaming or anything, I love your content but pointing out error.
No thats really good if you were able to figure out while writing it. Thats the idea of use cases and there are multiple ways of doing it. Happy learning 🙂
hi, himanshu nice explaination, I have a question ...we can not use opp.id to assign task.whatid...it is ok but we are using opp.id when we are assigning owner id to task...how it is possible ?
Hi Himanshu, Hope I am not too late to make a comment, I have a question here, why can't we use opp.OwnerId because the OwnerId of Parent 'Account' and Child 'Opportunity' will be same. And Sorry if I am not making sense here.
So, ideally you would want to update related records ONLY AFTER the parent is updated. Consider this: If parent update failed, you would end up updating child records as you have written code in before update. Since after update ensures your commit is successful, it opens the gate to update related records. Hope that makes sense :)
Take it slow, understand every line thats been written. Assess why I have written it, try to make sense of it and then write it on your console too and make sense of it again. This way you will get the hang dont worry.
Thank u for all videos I’ve done it this way , just wanted to share public class OpportunityTriggerHandler { public static void createTaskForAccOwner(list oppTriggerold,Map oppTriggerOldMap){ set accIds = new set(); for(Opportunity eachOpp : oppTriggerold){ if(eachOpp.AccountId!=null){ accIds.add(eachOpp.AccountId); } } Map accMap = new Map([select id,OwnerId from account where Id IN:accIds]); list accOwnerTaskList = new List(); for(Opportunity eachOpp : oppTriggerold){ Task accOwnerTask = new Task(); accOwnerTask.OwnerId = accMap.get(eachOpp.AccountId).OwnerId; accOwnerTask.WhatId = eachOpp.AccountId; accOwnerTask.Priority= 'High'; accOwnerTask.Subject = 'Please find out the reason of deletetion of opportunity named: '+eachOpp.Name; accOwnerTaskList.add(accOwnerTask); } if(!accOwnerTaskList.IsEmpty()){ insert accOwnerTaskList; } } }
i think u made it some what Complicated using Two Maps , crt me if i didnt follow the best pratices and didnt work for bulk records -- > trigger OppdeleteTaskAccOwner on Opportunity (after delete) { Set setid=new Set(); for(Opportunity opp:Trigger.old){ if(opp.AccountId!=null){ setid.add(opp.AccountId); } } List tsklist=new List(); Map mapowner=new Map(); List acclist=[select Id,OwnerId from Account where Id IN:setid]; for(Account acc:acclist){ mapowner.put(acc.Id,acc.OwnerId); } for(Account accrec:acclist){ Task tsk=new Task(); tsk.Subject='Call'; tsk.Status='Completed'; tsk.Priority='Low'; tsk.Description='Opp Record deleted please do inquiry'; tsk.OwnerId=mapowner.get(accrec.Id); tsklist.add(tsk); } insert tsklist; }
The idea is to explain a use case but also explain situations and scenarios and what can you do with what you have. But yes the solution you have provided here looks correct on first glance, so its a better approach to this problem for sure. Hope you got the idea.
public static void handleActivitiesAfterDelete(List oldRecords){
Set accountIds = new Set(); Map oppvsAccountMap = new Map(); Map accIdVsOwnerIdMap = new Map();
for(Opportunity opp : oldRecords){ accountIds.add(opp.AccountId); oppvsAccountMap.put(opp.Id, opp.AccountId); } for(Account acc : [Select Id, OwnerId From Account Where Id IN : accountIds]){ accIdVsOwnerIdMap.put(acc.id, acc.OwnerId); }
List taskRecordList = new List(); for(Opportunity opp : oldRecords){
Hi Bro, Very Informative videos thank u so much
So happy to hear that Sai. glad the videos are helping.
All the hardwork for finding OwnerId is unnessessary because the error is coming because of WhatId, i just task.ownerid=opp.ownerid, and it work like a charm. Please note that I am not condaming or anything, I love your content but pointing out error.
No thats really good if you were able to figure out while writing it. Thats the idea of use cases and there are multiple ways of doing it.
Happy learning 🙂
Mind Blowing
Glad it was helpful 🙂
even we dont requre set of accounids.we can fulfill need of accountid by map values. am i right?
@@Ankitkumar-ro6fl yes absolutely!
hi, himanshu nice explaination, I have a question ...we can not use opp.id to assign task.whatid...it is ok but we are using opp.id when we are assigning owner id to task...how it is possible ?
Hi Ankit, can you rephrase your question please? I am unclear of whats the ask
To assign owner we should use ownerId not oppId as oppId is opportunity and ownerId is the user id
Hi Himanshu, Hope I am not too late to make a comment,
I have a question here, why can't we use opp.OwnerId because the OwnerId of Parent 'Account' and Child 'Opportunity' will be same.
And Sorry if I am not making sense here.
What if Opportunity is reassigned to a new owner before deletion
can u share all the usecases and answer in document
Hi Kunal, I dont have that handy at the moment.
Bro, I have a Question.
I know we can Update Child Records on After Update trigger. Can we Update Child on Before Update ! If no, Why !
So, ideally you would want to update related records ONLY AFTER the parent is updated.
Consider this: If parent update failed, you would end up updating child records as you have written code in before update.
Since after update ensures your commit is successful, it opens the gate to update related records.
Hope that makes sense :)
@@salesforcemakessense Man you are making Salesforce Make Sense really ♥️
@@SfdcLearner thats the plan 😄
Hi bro, can we do with a single map
Mostly yes, I will encourage you to give it a try.
too much logical thinkings neede for this scenario. as a fresher can do it?☹
Take it slow, understand every line thats been written. Assess why I have written it, try to make sense of it and then write it on your console too and make sense of it again. This way you will get the hang dont worry.
Too much confusion
Hi Rajesh, have you started the playlist from the very first video?
Thank u for all videos
I’ve done it this way , just wanted to share
public class OpportunityTriggerHandler {
public static void createTaskForAccOwner(list oppTriggerold,Map oppTriggerOldMap){
set accIds = new set();
for(Opportunity eachOpp : oppTriggerold){
if(eachOpp.AccountId!=null){
accIds.add(eachOpp.AccountId);
}
}
Map accMap = new Map([select id,OwnerId from account where Id IN:accIds]);
list accOwnerTaskList = new List();
for(Opportunity eachOpp : oppTriggerold){
Task accOwnerTask = new Task();
accOwnerTask.OwnerId = accMap.get(eachOpp.AccountId).OwnerId;
accOwnerTask.WhatId = eachOpp.AccountId;
accOwnerTask.Priority= 'High';
accOwnerTask.Subject = 'Please find out the reason of deletetion of opportunity named: '+eachOpp.Name;
accOwnerTaskList.add(accOwnerTask);
}
if(!accOwnerTaskList.IsEmpty()){
insert accOwnerTaskList;
}
}
}
Looks good to me Alisha, well done!
hi i thinkk Triggeroldmap is not needed anyway ur fetching oldmapids
i think u made it some what Complicated using Two Maps , crt me if i didnt follow the best pratices and didnt work for bulk records -- >
trigger OppdeleteTaskAccOwner on Opportunity (after delete) {
Set setid=new Set();
for(Opportunity opp:Trigger.old){
if(opp.AccountId!=null){
setid.add(opp.AccountId);
}
}
List tsklist=new List();
Map mapowner=new Map();
List acclist=[select Id,OwnerId from Account where Id IN:setid];
for(Account acc:acclist){
mapowner.put(acc.Id,acc.OwnerId);
}
for(Account accrec:acclist){
Task tsk=new Task();
tsk.Subject='Call';
tsk.Status='Completed';
tsk.Priority='Low';
tsk.Description='Opp Record deleted please do inquiry';
tsk.OwnerId=mapowner.get(accrec.Id);
tsklist.add(tsk);
}
insert tsklist;
}
The idea is to explain a use case but also explain situations and scenarios and what can you do with what you have.
But yes the solution you have provided here looks correct on first glance, so its a better approach to this problem for sure.
Hope you got the idea.
@@salesforcemakessense but ur videos are too good broo, nice explanation and good content ❣️❣️
@@sasireddypolimera thank you 🙏
thankyou
public static void handleActivitiesAfterDelete(List oldRecords){
Set accountIds = new Set();
Map oppvsAccountMap = new Map();
Map accIdVsOwnerIdMap = new Map();
for(Opportunity opp : oldRecords){
accountIds.add(opp.AccountId);
oppvsAccountMap.put(opp.Id, opp.AccountId);
}
for(Account acc : [Select Id, OwnerId From Account Where Id IN : accountIds]){
accIdVsOwnerIdMap.put(acc.id, acc.OwnerId);
}
List taskRecordList = new List();
for(Opportunity opp : oldRecords){
Task taskRecord = new Task();
taskRecord.Priority = 'High';
taskRecord.Description = 'Task Created Successfully';
taskRecord.Subject = 'Follow Up';
taskRecord.OwnerId = accIdVsOwnerIdMap.get(oppvsAccountMap.get(opp.Id));
taskRecordList.add(taskRecord);
}
if(!taskRecordList.isEmpty()){
insert taskRecordList;
}
}