You need to sign in to do that
Don't have an account?
Michael M
Remove Query from For Loop
Can someone help- i need to rewrite the below trigger, with the same effects, but with remove all SOQL queries from inside the for loop:
trigger CdlUpdateDischarge on ContentDocumentLink (after insert) {
Set <string> setlinkedid = new set<string>();
for (ContentDocumentLink cd : trigger.new){
setlinkedid.add(cd.linkedentityid);
List<Discharge__c> disList = [select id from discharge__c where id=:setlinkedid];
List<contentdocumentlink> cdl = [SELECT ContentDocumentId
FROM ContentDocumentLink
WHERE LinkedEntityId in (select id from discharge__c where id=:setlinkedid)];
try{
if (cdl.size() > 0 && cdl.size() < 2){
for (Discharge__c dis : disList){
dis.Referrals_Status__c = 'Documents Uploaded';
}
update disList;
}
}catch(exception e){
system.debug(e);
}
}
}
trigger CdlUpdateDischarge on ContentDocumentLink (after insert) {
Set <string> setlinkedid = new set<string>();
for (ContentDocumentLink cd : trigger.new){
setlinkedid.add(cd.linkedentityid);
List<Discharge__c> disList = [select id from discharge__c where id=:setlinkedid];
List<contentdocumentlink> cdl = [SELECT ContentDocumentId
FROM ContentDocumentLink
WHERE LinkedEntityId in (select id from discharge__c where id=:setlinkedid)];
try{
if (cdl.size() > 0 && cdl.size() < 2){
for (Discharge__c dis : disList){
dis.Referrals_Status__c = 'Documents Uploaded';
}
update disList;
}
}catch(exception e){
system.debug(e);
}
}
}
Please try the following code.
I hope it helps. Let me know if any problem arises.
Thanks and Regards
Ayush Sharma
All Answers
Set < String > setlinkedid = new set< String >();
Map < Id, Discharge__c > mapDisch = new Map <Id, Discharge__c >();
for ( ContentDocumentLink cd : trigger.new ){
if ( cd.LinkedEntityId.getSObjectType().getDescribe().getName() == 'Discharge__c' ) {
if ( !mapDisch.containsKey( cd.LinkedEntityId ) ) {
Discharge__c objDisch = new Discharge__c();
objDisch.Id = cd.LinkedEntityId;
objDisch.Referrals_Status__c = 'Documents Uploaded';
mapDisch.put( cd.LinkedEntityId, objDisch );
}
}
}
if ( mapDisch.size() > 0 ) {
try {
update mapDisch.values();
} catch(exception e){
system.debug(e);
}
}
}
--
Magulan Duraipandian
www.infallibletechie.com
Please try the following code.
I hope it helps. Let me know if any problem arises.
Thanks and Regards
Ayush Sharma
If this line:
if ( mapDisch.size() > 0 )
was changed to if ( mapDisch.size() > 0 && mapDisch.size() < 2)
Would that only change the discharge status to 'Documents Uploaded' if this is the very first file being uploaded?
global class BatchOnLead implements Database.Batchable<sObject>, Database.Stateful{
public List<Id> userId = new List<Id>();
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT Id, OwnerID FROM Lead'
);
}
global void execute(Database.BatchableContext bc, List<Lead> scope){
system.debug('Called......');
List<Lead> lstAccount = new List<Lead>();
for (Lead leads : scope) {
system.debug('leads.OwnerID : '+leads.OwnerID);
userId.add(leads.OwnerID);
}
system.debug('userId : '+userId);
}
global void finish(Database.BatchableContext bc){
for(Integer i=0; i<userId.size(); i++) {
List<User> email = [SELECT Email FROM User WHERE Id =: userId[i]];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {email[0].Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Lead Assigned ');
mail.setPlainTextBody('You Have been Assigned a Lead.. Please Check.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}