From this video I experienced that I'm new that company and working in already existing project, this is because of your selected content and the way of your delivery, because it's differed from video to video. Thank you.
I suppose this will not work when inserting contacts in bulk, as trigger executes in batches. How will you know the number of contacts tagged to an account when, in bulk insert operation there might be multiple contacts with same account id which might exeed the max_contacts_allowed__c value on account object.
Hi, this use case will obviously be credible for scenarios within limits. In any case there is a chance child records or number of records go beyond expectations/limits, it is recommended to go via Batch apex.
Note: Very Very Important point please don't write the code inside an already existing method instead start by creating a fresh method because for beginners it becomes very complex to understand the code
@user-co6wi9pk8p No need of giving note bro, if you follow the content from starting video of apex, you your self realized that, it's in step by step enhancement. It's a part of enhancing the subject.
Hi Himanshu, I did a lil modification in code. Is the following correct? public void conmethod(list nlcon){ set accId = new set(); map conacc = new map(); for(contact c : nlcon){ if(c.AccountId !=null){ accId.add(c.AccountId); conacc.put(c.id,c.AccountId); } } map macc= new map([select id, MaxCon__c,(select id from contacts) from Account where id in : accId]); map accConCount = new map(); for(id accId : macc.keyset()){ integer conCount = (macc.get(id).contacts).size(); accConCount.put(id,conCount); } for(contact c : nlcon){ if(accConCount.get(conacc.get(c.id))>=macc.get(conacc.get(c.id)).MaxCon__c){ c.addError('max Contacts exceeded'); } else{ macc.get(conacc.get(c.id)).MaxCon__c=macc.get(conacc.get(c.id)).MaxCon__c+1; } } }
From this video I experienced that I'm new that company and working in already existing project, this is because of your selected content and the way of your delivery, because it's differed from video to video. Thank you.
Thank you 🙏
I suppose this will not work when inserting contacts in bulk, as trigger executes in batches. How will you know the number of contacts tagged to an account when, in bulk insert operation there might be multiple contacts with same account id which might exeed the max_contacts_allowed__c value on account object.
Hi, this use case will obviously be credible for scenarios within limits.
In any case there is a chance child records or number of records go beyond expectations/limits, it is recommended to go via Batch apex.
Note: Very Very Important point
please don't write the code inside an already existing method instead start by creating a fresh method because for beginners it becomes very complex to understand the code
Feedback notes 😄
Let me know if theres something you want me to help you explain. I can give a try.
@user-co6wi9pk8p
No need of giving note bro, if you follow the content from starting video of apex, you your self realized that, it's in step by step enhancement.
It's a part of enhancing the subject.
@@user-pz5ys9mj2q true
Hi Himanshu, I did a lil modification in code. Is the following correct?
public void conmethod(list nlcon){
set accId = new set();
map conacc = new map();
for(contact c : nlcon){
if(c.AccountId !=null){
accId.add(c.AccountId);
conacc.put(c.id,c.AccountId);
}
}
map macc= new map([select id, MaxCon__c,(select id from contacts) from Account where id in : accId]);
map accConCount = new map();
for(id accId : macc.keyset()){
integer conCount = (macc.get(id).contacts).size();
accConCount.put(id,conCount);
}
for(contact c : nlcon){
if(accConCount.get(conacc.get(c.id))>=macc.get(conacc.get(c.id)).MaxCon__c){
c.addError('max Contacts exceeded');
}
else{
macc.get(conacc.get(c.id)).MaxCon__c=macc.get(conacc.get(c.id)).MaxCon__c+1;
}
}
}
Looks good to me, test it with 1 and multiple scenarios to confirm.