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
FinneyFinney 

email error when trying to send an email to a lookup user using Apex Class

Hi,

 

I am trying to write a class which generates a pdf as we all which sends an email alert to a lookup field on the object.

 

I am getting an error when clicking the button.

 

SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : u.Email: []

Error is in expression '{!XFDFInitKOA}' in component <apex:page> in page mergepdfkoa

 

 

An unexpected error has occurred. Your development organization has been notified.

 

 

Please find my code here

 

public with sharing class PDFMergerKOA {
public PDFMergerKOA() {
}

public PDFMergerKOA(ApexPages.StandardController controller) {
}

private String getXmlStringKOA(pb__InventoryItem__c c)
{
String sname = [Select pb__Value__c From pb__leanParameters__c Where Name = 'Server_Instance'].pb__Value__c;
date todayis = system.today();
datetime nowis = system.now();
String s = '<?xml version="1.0" encoding="UTF-8"?>' +
'<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">' +
'<f href="https://'+sname+'.salesforce.com/resource/1375791520000/Key_Out_Acknowledgement"/>' +
'<fields>' +
'<field name="Date"><value>' + todayis + '</value></field>' +
//'<field name="Owner"><value>' + c.Seller_Name__c + '</value></field>' +
'<field name="text2"><value>' + c.Name + '</value></field>' +
//'<field name="owner1"><value>' + c.Seller_Name__c + '</value></field>' +
//'<field name="Agents Name"><value>' + c.Sales_Consultant_Name__c + '</value></field>' +
//'<field name="Owner2"><value>' + c.Seller_Name__c + '</value></field>' +
'</fields><ids original="AD4D2075CAACA047844B948756BB055F" modified="C2F97965812CB048A1CD08602C94D0A6"/>' +
'</xfdf>';

return s;
}


public PageReference XFDFInitKOA()
{
list<pb__InventoryItem__c> c = [Select Name,OwnerId, Key_Out_Acknowledgement__c,Sales_Consultant__c,Sales_Consultant_Email__c, Seller_Name__c, Sales_Consultant_Name__c from pb__InventoryItem__c
WHERE id = :ApexPages.currentPage().getParameters().get('id')limit 1];

if(c.size()>0){
pb__InventoryItem__c nc = c[0];
/* User u = [SELECT id, Name FROM User
WHERE id = :c.Ownerid];*/
String xmlContent = getXmlStringKOA(nc);


Attachment attachment = new Attachment();
attachment.Body = Blob.valueOf(xmlContent);
attachment.Name = 'KOA_' + c[0].Name + '.XFDF';
attachment.ParentId = c[0].Id;

insert attachment;

/*Document d = new Document();
d.FolderId = '00lG0000001HE4S';
d.Body = Blob.valueOf(xmlContent);
d.Description = c[0].Name;
d.Name = 'KOA_' + c[0].Name+ '_' + System.now() + '.XFDF';
insert d;*/


{
User u = [SELECT id, Email, Name FROM User
WHERE id = :c[0].Sales_Consultant__c];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'u.Email'};
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName('Testing');
mail.setSubject('Key Out Acknowledgement');
mail.setPlainTextBody('Please reassign the Qualified Lead: Lead Name as the owner has failed to take any action in the given 3 hours.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}



PageReference contactPage = new PageReference('/' + c[0].id);
contactPage.setRedirect(true);
return contactPage;
}
else{
PageReference contactPage = ApexPages.currentPage();
contactPage.setRedirect(true);
return contactPage;
}
}
}

 

Any help will be appreciated.

 

Thanks and Kind Regards

 

Finney

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The way you have used u.Email is as a string literal, due to the encapsulation in quotes, which is an invalid format email address:

 

String[] toAddresses = new String[] {'u.Email'};

 

you need to remove the quotes and then it will be interpreted as a the email field of the local variable u:

 

String[] toAddresses = new String[] {u.Email};

 

 

All Answers

bob_buzzardbob_buzzard

The way you have used u.Email is as a string literal, due to the encapsulation in quotes, which is an invalid format email address:

 

String[] toAddresses = new String[] {'u.Email'};

 

you need to remove the quotes and then it will be interpreted as a the email field of the local variable u:

 

String[] toAddresses = new String[] {u.Email};

 

 

This was selected as the best answer
FinneyFinney

Thanks Bob. Your a star mate :)