• Ramit
  • NEWBIE
  • 25 Points
  • Member since 2012

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 9
    Replies

I have a task Trigger that validates and works great on Leads. The problem is that the trigger fires on also for tasks on Accounts and Opporutnities. I've very new to Apex, so I'm sure I am overlooking something simple. 

 

How do I make this trigger fire only when inserting a task on a lead?

 

trigger UpdateLeadStatusOnTask on Task (after insert) {

//Retrieve task data after a new task has been saved
Task theTask = trigger.new[0];

	//Create the lead (Lead Status and the IsConverted fields) for the Lead which the task was logged against
        Lead theLead= [Select Status,IsConverted from Lead where id=:theTask.WhoId];

    //Check to see if Marketo is the owner of the task, we only want tasks not owned by Marketo
    if(theTask.ownerid<>'00540000001GDw2'){
    
        
            //Only use leads that have a status of open
            if(theLead.Status == 'Open'){
            
            //Change the status to contacted
            theLead.Status = 'Contacted';
            
        //apply the changes to the lead
        update theLead;
        }
    } 
}

 

I am getting error message on hitting the governor limits due to the trigger below. What this trigger does is that whenever a task's description it copies over the data from the task object on to the custom object. Can someone please help so that my code does not hit the governor limits.

 

Many thanks in advance.

 

trigger CommentLog on Task (before update) {
for(Task t: trigger.old)
{
CommentLog__c c = new CommentLog__c();
c.Previous_Comment__c=t.Description;
c.CommentDate__c=t.LastModifiedDate;
task t1= [select owner.name,LastModifiedBy.name from task where id=:t.id];
c.Name=t1.LastModifiedBy.name;
c.Task_owner__c=t1.owner.name;
c.type__c='Task';
c.Link_to_Task__c='https://na1.salesforce.com/'+t.id;
insert c;

}

}

 

How I check in Apex Class, as given  string year parameter is grater then or equal to current year

APEX

List<Contact> contactList = [SELECT firstname,lastname,Current_Fees_Due__c,Id,A_Level_Year__c from Contact WHERE Contact_Type__c = 'Student' AND Tuition__c='Customer' AND A_Level_Year__c>='%Current Year%'

ORDER BY Name ASC];

 

Note: A_Level_Year__c = '2012' [ parameter is strin g format]

I'm using Apex to perform some basic XML parsing. I'm having no luck getting to any of the actual XML node names. The getLocalName()  method within the XmlStreamReader class always returns null for all nodes as I loop through them.

 

TEST CLASS:

@isTest
private class BPTCalloutTest {
private static final String XML_STR = '<document>' +
'<result>success</result>' +
'<resultcode>000000</resultcode>' +
'<note></note>' +
                                        '<item>' +
      '<quantity>1</quantity>' +
  '<fname>Bob</fname>' +
  '<lname>Tungsten</lname>' +
  '<address>23232 Fleet Street</address>' +
  '<city>Santa Clara</city>' +
  '<state>CA</state>' +
  '<zip>94105</zip>' +
  '<country>United States</country>' +
  '<email>blahblahblah@blahblahblah.com</email>' +
  '<phone>4155555555</phone>' +
    '</item>' +
  '</document>';
 
    static testMethod void xmlStreamReaderTest() {
        XmlStreamReader reader = new XmlStreamReader(XML_STR);
        Opportunity[] opptyList = BPTCallout.parseXML(reader);
        System.assert(opptyList != null);
        System.assert(!opptyList.isEmpty());
    }
}

 

MAIN CLASS:

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

    public static Opportunity[] parseXML(XmlStreamReader reader) {
        Opportunity[] oppList = new List<Opportunity>();

        if (reader != null) {
            while (reader.hasNext()) {
            	System.debug('$$$ reader.getEventType(): ' + reader.getEventType());
                if (reader.getEventType() == XmlTag.END_DOCUMENT) {
                    break;
                }
                else if (reader.getEventType() == XmlTag.CHARACTERS) {
                	System.debug('$$$ reader.getLocalName(): ' + reader.getLocalName());
                	System.debug('$$$ reader.getText(): ' + reader.getText());
                	System.debug('$$$ reader.getNamespace(): ' + reader.getNamespace());
                	System.debug('$$$ reader.getlocation(): ' + reader.getlocation());
                	System.debug('$$$ reader.getprefix(): ' + reader.getprefix());
                	
                	if (/*reader.getLocalName() == 'lname'*/ reader.getAttributeCount() == 4 && reader.getText() != null) {
	                    oppList.add(new Opportunity(Name = reader.getText()));
                	}
                }
                reader.next();
            }
        }
        System.debug('$$$ oppList: ' + oppList);
        return oppList;
    }
}

 

Very basic functionality at this point. If you run this, you will see that reader.getLocalName() always returns null and so do all accompanying methods (getNameSpace, getLocation, getPrefix).

 

Thanks in advance.

 

I have a task Trigger that validates and works great on Leads. The problem is that the trigger fires on also for tasks on Accounts and Opporutnities. I've very new to Apex, so I'm sure I am overlooking something simple. 

 

How do I make this trigger fire only when inserting a task on a lead?

 

trigger UpdateLeadStatusOnTask on Task (after insert) {

//Retrieve task data after a new task has been saved
Task theTask = trigger.new[0];

	//Create the lead (Lead Status and the IsConverted fields) for the Lead which the task was logged against
        Lead theLead= [Select Status,IsConverted from Lead where id=:theTask.WhoId];

    //Check to see if Marketo is the owner of the task, we only want tasks not owned by Marketo
    if(theTask.ownerid<>'00540000001GDw2'){
    
        
            //Only use leads that have a status of open
            if(theLead.Status == 'Open'){
            
            //Change the status to contacted
            theLead.Status = 'Contacted';
            
        //apply the changes to the lead
        update theLead;
        }
    } 
}

 

I have a date field on Object__c called Contact_Date__c. The user cannot create an Object__c record where Contact_Date__c can only be the current month unless it is for the previous month and today's date is before the 5th.

 

Basically, an Object__c record for November (Contact_Date__c) must be created no later than December 5th.

 

I played around with AND/OR statments but am having difficulty. I was able to create the validation rule for having Today's month and Contact month the same: 

 

MONTH( Today() ) <> MONTH( hhs__Contact_Date__c )

 

but I need to somehow write in an exception of UNLESS, Month(Today()) is Month(hhs__Contact_Date__c)+1 and Day(Today()) < 5. 

 

Any ideas?

In my quest to find a way to report on rolling months in custom object, I decided to brainiack this approach.

I set a field to todays date.  Then I add a month.  So, this formula works somewhat (returns wrong month right now):

IF(MONTH(TODAY()) + 1 >= 12, "1", TEXT(MONTH(TODAY()) + 1)) & "/1/" & IF(MONTH(TODAY()) + 1 >= 12, TEXT(YEAR(TODAY()) +1), TEXT(YEAR(TODAY())))

 

But if I want to calculate the following months based on today (for example):

IF(MONTH(TODAY()) + 2 >= 12, "1", TEXT(MONTH(TODAY()) + 2)) & "/1/" & IF(MONTH(TODAY()) + 2 >= 12, TEXT(YEAR(TODAY()) +1), TEXT(YEAR(TODAY())))
IF(MONTH(TODAY()) + 3 >= 12, "1", TEXT(MONTH(TODAY()) + 3)) & "/1/" & IF(MONTH(TODAY()) + 3 >= 12, TEXT(YEAR(TODAY()) +1), TEXT(YEAR(TODAY())))
IF(MONTH(TODAY()) + 4 >= 12, "1", TEXT(MONTH(TODAY()) + 4)) & "/1/" & IF(MONTH(TODAY()) + 4 >= 12, TEXT(YEAR(TODAY()) +1), TEXT(YEAR(TODAY())))
IF(MONTH(TODAY()) + 5 >= 12, "1", TEXT(MONTH(TODAY()) + 5)) & "/1/" & IF(MONTH(TODAY()) + 5 >= 12, TEXT(YEAR(TODAY()) +1), TEXT(YEAR(TODAY())))
IF(MONTH(TODAY()) + 6 >= 12, "1", TEXT(MONTH(TODAY()) + 6)) & "/1/" & IF(MONTH(TODAY()) + 6 >= 12, TEXT(YEAR(TODAY()) +1), TEXT(YEAR(TODAY())))
IF(MONTH(TODAY()) + 7 >= 12, "1", TEXT(MONTH(TODAY()) + 7)) & "/1/" & IF(MONTH(TODAY()) + 7 >= 12, TEXT(YEAR(TODAY()) +1), TEXT(YEAR(TODAY())))

 these all return the same date (1/1/2013).

 

AND if I try to use the first rolling date calculated like this:

IF(MONTH(DATEVALUE(Rolling_Months_1__c)) + 2 >= 12, "1", TEXT(MONTH(TODAY()) + 2))  & "/1/" & IF(MONTH(DATEVALUE(Rolling_Months_1__c)) + 2 >= 12, TEXT(YEAR(TODAY()) +1), TEXT(YEAR(TODAY())))

 I do not get any syntax errors, BUT my field displays this -->   #error   <--  instead of a date.

 

I should point out that ALL my formula fields are set to return values as text.

 

Anyone have any ideas on this?

 

Thanks!

Temple