• Aubrey
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 8
    Replies
Hi

I use the XmlStreamReader to parse a XML String. There is an element named duedate in the String.
I need to cast the duedate to Date type, but the XmlStreamReader only has a getText() to extract text
from XML elements. I searched Apex Language Reference, but didn't find a method do String to Date
casting.

Is there a convenient way to do that? Or I have to split each date part and then use
datetime
.newInstance(Year, Month, Day) to convert a String to a Date?

Thanks & Happy New Year!
Aubrey 
  • December 31, 2009
  • Like
  • 0
Hi

I got an Apex exception like this:
=============================================================
Apex script unhandled trigger exception by user/organization:
005R0000000I9rn/00DR00000006UWJ

SendTaskOverdueCompletedAlert: execution of AfterUpdate

caused by: System.Exception: Too many SOQL queries: 21

Class.SendTaskForm.sendTaskForm: line 299, column 23
Trigger.SendTaskOverdueCompletedAlert: line 110, column 29
=============================================================

I checked the SendTaskForm class:

297    Lead lead = null;
298    try {
299         lead = [SELECT Id, AnnualRevenue, NumberOfEmployees,LeadSource,
300         Industry, Commodity__c, Service_Required__c,Facility_Required__c,
301         Inbound_Transactions_Expected__c,Outbound_Transaction_Expected__c,
302         Tonnage_Expected__c FROM Lead WHERE Id = :whoId LIMIT 1];
303    } catch (QueryException e) {  
304         system.debug('Can not retrieve Lead fields');
305         lead = null;
306    }

The
whoId is the triggered Task's WhoId. Suppose the ID should be unique, right?
Then why the error said:
Too many SOQL queries: 21? I checked my sanbox's Leads.
The total number of Leads is 16. Can anybody give me some advices?

Thanks
Aubrey
  • December 23, 2008
  • Like
  • 0
I have below trigger

trigger SendLeadOverdueAlert on Lead (after update) {

for (Lead newLead : Trigger.new) {
if (newLead.Overdue__c.contains('Overdue') && newLead.Status!='Qualified') {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(newLead.OwnerId);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setSaveAsActivity(false);
mail.setTemplateId('00XR0000000DkLJ');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
}

The email template looks like:
==============================================
Subject Alert - Lead for {!Lead.Company} Assigned to {!Lead.OwnerFullName} is Overdue
Email Body The Lead assigned to {!Lead.OwnerFullName} is overdue on {!Lead.Due_Date__c} .

Lead Name: {!Lead.Name}
Region: {!Lead.Region__c}
Company: {!Lead.Company}
Industry: {!Lead.Industry}
Email: {!Lead.Email}
Phone: {!Lead.Phone}
Last Activitiy Date: {!Lead.LastActivityDate}

Detail:
{!Lead.Description}

Or click the link below to get detailed info:
{!Lead.Link}

Please attend to it immediately.
==============================================

Debug Log looks like:
==============================================
subject: Alert - Lead for Assigned to is Overdue, bccSender: false, saveAsActivity: false, useSignature: false, targetObjectId: 0052000000114MoAAI, templateId: 00XR0000000DkLJMA0, plainTextBody: The Lead assigned to is overdue on .

Lead Name:
Region:
Company:
Industry:
Email:
Phone:
Last Activitiy Date:

Detail:

Or click the link below to get detailed info:

Please attend to it immediately., charset: UTF-8,
===============================================

Thanks
Aubrey
  • December 12, 2008
  • Like
  • 0
Hi

I have an inbound email service to capture email to Task.
I specify the Task's CreatedById to the email SendFrom user's Id.
But I got an error:
Field is not writeable: Task.CreatedById


Can I specify the CreatedById manually?
Is it generated by system automatically?
If so, how does system decide which user creates the task?
Is it default to use an administrator?

Appreciate  for your help!

Aubrey
  • December 04, 2008
  • Like
  • 0
Hi

I want to create a trigger to send Task alert email, I met two issues.
The Apex codes like below:

trigger SendAlertMail on Task (after insert) {

    for (Task newTask : Trigger.new) {
        ......
        // Show the CreatedBy person in email subject
        String createBy  = newTask.CreatedBy.Name;
        ......

        // assign other Task info to outbound email html body
        // I used html form to show the Task's info as what users see in salesforce
        ...... // compose the html string here

        // Encode the html string as base64 and then create an attachment
        // Because different web mail systems have different appearances
        // of html email, so need to send the task form in a html file as email attachment
        Blob html = Crypto.generateDigest('SHA1',Blob.valueOf(htmlBody));   
        Attachment attachment = new Attachment();
        attachment.Body = Blob.valueOf(EncodingUtil.base64Encode(html));
        attachment.Name = String.valueOf('Task.html');

        // add an email attachment
       Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];
       //create an attachment object to populate with data
       Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
       fileAttachment.setBody(attachment.Body);
       fileAttachment.setFileName(attachment.Name);
       fileAttachments[0]=fileAttachment;
       //add attachments to mail message
       mail.setFileAttachments(fileAttachments);
       ......

       // Send the email
    }
}

The first issue is - the CreatedBy shown in email subject is null;
The second issue is - The Task.html body in the email attachment is encoded string, looks like: <body>XHB3Fy/uzPDD5f7NmDEvmZdJJIA=</body>

Can any body tell me why?
You help is appreciated!

Thanks
Aubrey
  • December 04, 2008
  • Like
  • 0
Dear Friends

I want to create a html attachment and attach it in email.
I got an exception when setting email attachment
body, the Apex codes like this:
=====================================================================
    // Encode the html string as base64 and then create an attachment  
    Blob html = Crypto.generateDigest('SHA1',Blob.valueOf(htmlBody));
    Attachment attachment = new Attachment();

    // set attachment body - base64, and
name
    attachment.Body = Blob.valueOf(EncodingUtil.base64Encode(html));
    attachment.Name = String.valueOf('Task.html');

    // if WhatId is not null, set the
attachment.ParentId to WhatId
    // else set the
attachment.ParentId to WhoId
    if (newTask.WhatId != null) {
        attachment.ParentId = newTask.WhatId;
    } else {
        attachment.ParentId = newTask.WhoId;
    }
    insert attachment;
   
           // add the
attachment to an email message
line251    Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];
line252    fileAttachments[0].setBody(attachment.body);
line253    fileAttachments[0].setFileName(attachment.Name);
line254    mail.setFileAttachments(fileAttachments);
=====================================================================
Error message like this:
SendAlertMail: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.SendAlertMail: line 252, column 21

But I can get the attachment.Name after the "insert attachment" operation.
And I can found the created attachment in related Opportunity.
Can anybody give me some advices?

Thanks


  • December 02, 2008
  • Like
  • 0
Hi

I use the XmlStreamReader to parse a XML String. There is an element named duedate in the String.
I need to cast the duedate to Date type, but the XmlStreamReader only has a getText() to extract text
from XML elements. I searched Apex Language Reference, but didn't find a method do String to Date
casting.

Is there a convenient way to do that? Or I have to split each date part and then use
datetime
.newInstance(Year, Month, Day) to convert a String to a Date?

Thanks & Happy New Year!
Aubrey 
  • December 31, 2009
  • Like
  • 0
Hi

I got an Apex exception like this:
=============================================================
Apex script unhandled trigger exception by user/organization:
005R0000000I9rn/00DR00000006UWJ

SendTaskOverdueCompletedAlert: execution of AfterUpdate

caused by: System.Exception: Too many SOQL queries: 21

Class.SendTaskForm.sendTaskForm: line 299, column 23
Trigger.SendTaskOverdueCompletedAlert: line 110, column 29
=============================================================

I checked the SendTaskForm class:

297    Lead lead = null;
298    try {
299         lead = [SELECT Id, AnnualRevenue, NumberOfEmployees,LeadSource,
300         Industry, Commodity__c, Service_Required__c,Facility_Required__c,
301         Inbound_Transactions_Expected__c,Outbound_Transaction_Expected__c,
302         Tonnage_Expected__c FROM Lead WHERE Id = :whoId LIMIT 1];
303    } catch (QueryException e) {  
304         system.debug('Can not retrieve Lead fields');
305         lead = null;
306    }

The
whoId is the triggered Task's WhoId. Suppose the ID should be unique, right?
Then why the error said:
Too many SOQL queries: 21? I checked my sanbox's Leads.
The total number of Leads is 16. Can anybody give me some advices?

Thanks
Aubrey
  • December 23, 2008
  • Like
  • 0
Hi

I have an inbound email service to capture email to Task.
I specify the Task's CreatedById to the email SendFrom user's Id.
But I got an error:
Field is not writeable: Task.CreatedById


Can I specify the CreatedById manually?
Is it generated by system automatically?
If so, how does system decide which user creates the task?
Is it default to use an administrator?

Appreciate  for your help!

Aubrey
  • December 04, 2008
  • Like
  • 0
Hi

I want to create a trigger to send Task alert email, I met two issues.
The Apex codes like below:

trigger SendAlertMail on Task (after insert) {

    for (Task newTask : Trigger.new) {
        ......
        // Show the CreatedBy person in email subject
        String createBy  = newTask.CreatedBy.Name;
        ......

        // assign other Task info to outbound email html body
        // I used html form to show the Task's info as what users see in salesforce
        ...... // compose the html string here

        // Encode the html string as base64 and then create an attachment
        // Because different web mail systems have different appearances
        // of html email, so need to send the task form in a html file as email attachment
        Blob html = Crypto.generateDigest('SHA1',Blob.valueOf(htmlBody));   
        Attachment attachment = new Attachment();
        attachment.Body = Blob.valueOf(EncodingUtil.base64Encode(html));
        attachment.Name = String.valueOf('Task.html');

        // add an email attachment
       Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];
       //create an attachment object to populate with data
       Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
       fileAttachment.setBody(attachment.Body);
       fileAttachment.setFileName(attachment.Name);
       fileAttachments[0]=fileAttachment;
       //add attachments to mail message
       mail.setFileAttachments(fileAttachments);
       ......

       // Send the email
    }
}

The first issue is - the CreatedBy shown in email subject is null;
The second issue is - The Task.html body in the email attachment is encoded string, looks like: <body>XHB3Fy/uzPDD5f7NmDEvmZdJJIA=</body>

Can any body tell me why?
You help is appreciated!

Thanks
Aubrey
  • December 04, 2008
  • Like
  • 0
Dear Friends

I want to create a html attachment and attach it in email.
I got an exception when setting email attachment
body, the Apex codes like this:
=====================================================================
    // Encode the html string as base64 and then create an attachment  
    Blob html = Crypto.generateDigest('SHA1',Blob.valueOf(htmlBody));
    Attachment attachment = new Attachment();

    // set attachment body - base64, and
name
    attachment.Body = Blob.valueOf(EncodingUtil.base64Encode(html));
    attachment.Name = String.valueOf('Task.html');

    // if WhatId is not null, set the
attachment.ParentId to WhatId
    // else set the
attachment.ParentId to WhoId
    if (newTask.WhatId != null) {
        attachment.ParentId = newTask.WhatId;
    } else {
        attachment.ParentId = newTask.WhoId;
    }
    insert attachment;
   
           // add the
attachment to an email message
line251    Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];
line252    fileAttachments[0].setBody(attachment.body);
line253    fileAttachments[0].setFileName(attachment.Name);
line254    mail.setFileAttachments(fileAttachments);
=====================================================================
Error message like this:
SendAlertMail: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.SendAlertMail: line 252, column 21

But I can get the attachment.Name after the "insert attachment" operation.
And I can found the created attachment in related Opportunity.
Can anybody give me some advices?

Thanks


  • December 02, 2008
  • Like
  • 0