function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nihar SharmaNihar Sharma 

Not able to Send Email for Person Account

I am facing one issue and i don't know what is it, i am going to send and Email template to the Contact via Send Email Method but it will not working for Person Account...It is working for Business Account Perfectly.

Can anybody knows this...?

User-added image

Here is the Code : 
 
// Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        // Reference the attachment page and pass in the account ID
        PageReference quotePdf = Page.PrintQuote;
        quotePdf.getParameters().put('id', quote.Id);
        quotePdf.setRedirect(true);

        // Take the PDF content
        Blob b= Test.isRunningTest() ? Blob.valueOf('UNIT.TEST') : quotePdf.getContent();
      /*  Blob  b;
       if (Test.IsRunningTest()){
            b=Blob.valueOf('UNIT.TEST');
           }
       else{
            b = quotePdf.getcontentAsPdf();
        }*/


        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName( quote.Name + '.pdf');
        efa.setBody(b);
        fileAttachments.add(efa);

        Attachment att = new Attachment();
        att.body = b;
        att.Name = quote.Name + '.pdf';
        attList.add(att);

        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :oppty.Id]) {
            // Add to attachment file list
            Messaging.Emailfileattachment opptyAttach = new Messaging.Emailfileattachment();
            opptyAttach.setFileName(a.Name);
            opptyAttach.setBody(a.Body);
            fileAttachments.add(opptyAttach);

            Attachment att1 = new Attachment();
            att1.body = a.Body;
            att1.Name = a.Name;
            attList.add(att1);
        }
        email.setFileAttachments(fileAttachments);

        //create dummy contact
        contact con = new contact();
        con.lastname = 'test con';
        con.Email = 'dummy@organization.com';
        con.accountid = quote.Opportunity__r.AccountId;
        insert con;

        email.setTemplateId( selectedTemplateId );
        email.setPlainTextBody(' ');
        email.setWhatId(oppty.id);
        email.setTargetObjectId(con.id);
        email.setToAddresses(new List<String>{quote.Opportunity__r.Account.Email__c});
        // Sends the email
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        if (r.get(0).isSuccess()) {
            //delete con;

            Task conTask = [Select Id, Subject, Description, Status, WhatId FROM Task WHERE WhoId =: con.Id];
            conTask.WhoId = null;
            update conTask;

            if(!attList.isEmpty()){
                for(Attachment attOne : attList){
                    attOne.ParentId = conTask.Id;
                }

                insert attList;
            }
            delete con;
        }

    } catch(Exception ex){
        system.debug('@@ex : '+ ex.getMessage());
    }
    return null;

Here is the Debug Log :
 
17:59:45.365 (365289954)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
17:59:45.374 (374242038)|DML_END|[80]
17:59:45.374 (374345877)|VF_PAGE_MESSAGE|Can not select a person account
17:59:45.374 (374455141)|EXCEPTION_THROWN|[80]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId]
17:59:45.374 (374871091)|HEAP_ALLOCATE|[80]|Bytes:131
17:59:45.374 (374911869)|VARIABLE_SCOPE_BEGIN|[106]|ex|Exception|true|false
17:59:45.375 (375008887)|VARIABLE_ASSIGNMENT|[106]|ex|"common.apex.runtime.impl.DmlExecutionException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId]"|0xb720d38
17:59:45.375 (375020072)|STATEMENT_EXECUTE|[106]
17:59:45.375 (375023602)|STATEMENT_EXECUTE|[107]
17:59:45.375 (375028953)|HEAP_ALLOCATE|[107]|Bytes:7
17:59:45.375 (375092756)|HEAP_ALLOCATE|[107]|Bytes:127
17:59:45.375 (375115362)|HEAP_ALLOCATE|[107]|Bytes:134
17:59:45.375 (375137936)|USER_DEBUG|[107]|DEBUG|@@ex : Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId]
17:59:45.375 (375148335)|STATEMENT_EXECUTE|[109]
17:59:45.375 (375537174)|CODE_UNIT_FINISHED|SendEmailWithAttachmentsController invoke(sendEmail)
17:59:45.376 (376134040)|VF_APEX_CALL|j_id30|{!sendEmail}|PageReference: none
17:59:45.380 (380563876)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5
17:59:45.380 (380574167)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:59:45.380 (380589584)|VARIABLE_ASSIGNMENT|[-1]|this.conSeverity|"ERROR"|0x59d07d55
17:59:45.380 (380706672)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
17:59:45.380 (380713611)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:59:45.380 (380724950)|VARIABLE_ASSIGNMENT|[-1]|this.conStrength|2|0x59d07d55
17:59:45.388 (388891596)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:5
17:59:45.388 (388901003)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:59:45.388 (388919195)|VARIABLE_ASSIGNMENT|[-1]|this.conSeverity|"ERROR"|0x59d07d55
17:59:45.389 (389015758)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
17:59:45.389 (389021441)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
17:59:45.389 (389031672)|VARIABLE_ASSIGNMENT|[-1]|this.conStrength|2|0x59d07d55

Thank you in Advance....
ProlayProlay
Where are you querying for the person account through Opportunity and Quote? I am not able to see the SOQL query on Opportunity and Quote objects before inserting the dummy Contact