-3
trigger MandatoryDocumentsUploadedEmail on Verification_Documents__c (after insert) {
    if(Trigger.isAfter){
        if(!Trigger.new.isEmpty()){
            Set<Id> oppIds = new Set<Id>();
            Set<Id> verDocIds = new Set<Id>();
            for(Verification_Documents__c vd : Trigger.new){
                oppIds.add(vd.Opportunity__r.id);
                verDocIds.add(vd.Id);
            }
            List<Opportunity> verOpps = [SELECT Id, Name,stageName, (SELECT id, Name, Document_Name__c, docType__c, Document_Master__c,Document_Master__r.Stages__c,
                                                                     Document_Master__r.Document_Name__c, Document_Master__r.DocumentType__c, Reviewed__c, Approved__c,
                                                                     AttachmentId__c, Comment__c FROM Verification_Documents__r WHERE Opportunity__r.Id IN :oppIds) 
                                         FROM Opportunity WHERE Id IN :oppIds];
            system.debug('verOpps'+verOpps);

            system.debug('verOpps::: '+ verOpps);
            List<Verification_Documents__c> vd = [SELECT Id,Name,docType__c,Approved__c,AttachmentId__c,Document_Status__c,Photograph__c,Reviewed__c,Document_Master__r.Document_Name__c,Document_Master__r.User__c,Comment__c,
                                                  Document_Master__r.Stages__c,Document_Master__r.DocumentType__c,Opportunity__r.customer__r.email,Opportunity__r.OwnerId,Opportunity__r.Sales_Contractor__r.Id, 
                                                  Opportunity__r.Sales_Contractor__r.Email,Opportunity__r.Loan_Number__c FROM Verification_Documents__c where Document_Master__r.Mandatory__c=true and Opportunity__r.Id IN : oppIds and Document_Master__r.Stages__c= 'Verification'];
            List<TypeWiseDocument__c> fileListMandate = new List<TypeWiseDocument__c>();
            List<String> fileList = new List<String>();
            for(TypeWiseDocument__c documentDesc: [Select Document_Name__c, Stages__c, User__c, DocumentType__c 
                                                   FROM TypeWiseDocument__c 
                                                   WHERE Mandatory__c=true AND DocumentType__c = 'Predefined'])
            {
                fileListMandate.add(documentDesc);
            }
            //Map<String,Verification_Documents__c> MapAttchmentByType=new Map<String,Verification_Documents__c>();

            for (Opportunity o : verOpps) 
            {
                System.debug('opp id for record type is ====  '+o.Id);
                String opporId = o.Id;

                Map<String,Verification_Documents__c> MapAttchmentByType=new Map<String,Verification_Documents__c>();
                for(Verification_Documents__c attch : o.Verification_Documents__r)
                {
                    system.debug('attch.Document_Master__r.Stages__c****'+attch.Document_Master__r.Stages__c+'o.StageName***'+o.StageName);
                    MapAttchmentByType.put(attch.docType__c, attch);   
                    if(attch.Document_Master__r.Stages__c == 'Verification'){
                        if(attch.AttachmentId__c != null){
                            List<String> toAddress = new List<String> ();
                            User LoggedinUsr1= [SELECT Id,Name,Email,Partner_User_Type__c, Profile.Name FROM User where id='0056C000001CnFnQAK' LIMIT 1];
                            toAddress.add(LoggedinUsr1.Email);//LoggedinUsr1.Email
                            system.debug('LoggedinUser1'+LoggedinUsr1);
                            System.debug('****In try'+LoggedinUsr1.Email);
                            EmailTemplate emailTemp = [SELECT Id, Name, Body, HtmlValue FROM EmailTemplate where name='DocumentsUploadedByCustomer']; 
                            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                            email.setToAddresses(toAddress);
                            email.setSenderDisplayName('Oa Team');
                            email.setTargetObjectId(LoggedinUsr1.Id);
                            //email.setWhatId(vd[0].Id);
                            email.setTemplateId(emailTemp.id);   
                            email.setSaveAsActivity(false);

                            if(email != null){                
                                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email }); 
                            }
                        }
                    }
                }
            }
        }
    }
}

Will this send an email after uploading all the mandatory documents at verification stage.

  • Hey Mahesh, please don't change the content of your question to new code/new questions after receiving answers - it invalidates the posts that have been made and is confusing to other readers. You can post a new question if you need to but please be specific about what you're asking. – David Reed Mar 22 '19 at 13:11
  • Can you please look at the code and say where is the mistake.It is not getting into this for(Verification_Documents__c attch : o.Verification_Documents__r) loop.Please – mahesh Mar 22 '19 at 17:19
1
List<Verification_Documents__c> vd = [
    SELECT ... 
    FROM Verification_Documents__c 
    WHERE Opportunity__c IN: Trigger.new 
          AND Document_master__r.Stages__c IN: Trigger.new 
          AND Document_Master__r.Mandatory__c=true];

I've trimmed and formatted this so it is a little easier to read.

You have two bind expressions here, Opportunity__c IN: Trigger.new and Document_master__r.Stages__c IN: Trigger.new. We don't know the type of the second one so I'll focus on the first.

Presumably, Opportunity__c is a lookup from Verification_Documents__c to Opportunity. That means the field type is an Id of an Opportunity object. You're asking Salesforce to evaluate that field against a collection of a different object - Trigger.new is a collection of Verification_Documents__c objects.

The comparand must be of the same type. You'd need to iterate over Trigger.new and accumulate a Set of Opportunity Ids. You could then use that collection in your bind. A similar pattern would address the other bind, to Document_master__r.Stages__c, using whatever type that field happens to be.

| improve this answer | |

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