• Allister McKenzie 10
  • NEWBIE
  • 40 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 1
    Replies
I am using the REGEX function in a workflow rule, but I keep getting an error

Error: Function REGEX may not be used in this type of formula

I checked https://help.salesforce.com/htviewhelpdoc?id=customize_functions_i_z.htm&siteLang=en_US#REGEX (https://help.salesforce.com/htviewhelpdoc?id=customize_functions_i_z.htm&siteLang=en_US#REGEX) and it says that REGEX is available everywhere except formula fields and custom buttons.

Can anyone tell me why this is not working? 
 
RecordType.DeveloperName = 'IT Case' && REGEX(Description, '[A-Z]{3}[0-9]{1}')


 
I'm receiving an error on my visualforce page when I try save records.  It is happening on the subject field for Tasks.  When I checked the Task subject field, it is defined as a picklist, but even when I choose one of the predefined picklist values, I still receive an error.


Task Subject Error
 
public with sharing class AddTasksToCases {

    public Case[] caseList = null;
    public Task cftTask {get; set;}
    
    public AddTasksToCases(ApexPages.StandardSetController setCon) {       
        caseList = setCon.getSelected();
        //newTask = new Task();        
    }

    public pageReference CreateTask(){
        Task[] newTasks = new Task[]{};
        
        for(Case c : caseList){
             Task tk = new Task(
             	 WhatId = c.Id, 
             	 OwnerId = cftTask.Owner.Id, 
             	 Subject = cftTask.Subject, 
             	 Role__c = cftTask.Role__c, 
                 ActivityDate = cftTask.ActivityDate,
                 Description = cftTask.Description,
                 Status = cftTask.Status,
                 Type = cftTask.Type);
             newTasks.add(tk);
            
            if(newTasks.size() >= 200){
                insert newTasks;
                newTasks.clear();
            }
        }
        
        if(!newTasks.isEmpty()){
            insert newTasks;
        }
        
        return new PageReference('/500');
    }
    
}

Visualforce page
 
<apex:page showHeader="true" sidebar="true" standardController="Case" recordSetVar="cases" extensions="AddTasksToCases">
    <apex:sectionHeader title="Cases" subtitle="Mass Task Create" />
    <apex:form >
        <apex:pageBlock title="CFT Task">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Add Tasks" action="{!CreateTask}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
           <apex:pageBlockSection columns="2" title="Assignee Information">
               <apex:inputField value="{!cftTask.OwnerId}"></apex:inputField>
               <apex:inputField value="{!cftTask.Role__c}"></apex:inputField>
               <apex:inputField value="{!cftTask.Type}"></apex:inputField>
               <apex:inputField value="{!cftTask.ActivityDate}"></apex:inputField>
           </apex:pageBlockSection> 
           <apex:pageBlockSection columns="1" title="Task Description">
               <apex:inputField value="{!cftTask.Status}"></apex:inputField>
               <apex:inputField value="{!cftTask.Subject}"></apex:inputField>
               <apex:inputField value="{!cftTask.Description}"></apex:inputField>
            </apex:pageBlockSection>        
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
I am using the REGEX function in a workflow rule, but I keep getting an error

Error: Function REGEX may not be used in this type of formula

I checked https://help.salesforce.com/htviewhelpdoc?id=customize_functions_i_z.htm&siteLang=en_US#REGEX (https://help.salesforce.com/htviewhelpdoc?id=customize_functions_i_z.htm&siteLang=en_US#REGEX) and it says that REGEX is available everywhere except formula fields and custom buttons.

Can anyone tell me why this is not working? 
RecordType.DeveloperName = 'IT Case' && REGEX(Description, 'RegEx_Text')

 
I am trying to use the case priority field, of the most recently inserted case, to change a field on the account. 
 
for (Account a : [SELECT id,(select id, priority from cases order by createddate desc limit 1) FROM account WHERE Id IN :parent]){

            if (a.Priority == 'medium') {
               // update account
            }

}

How would I do this.
I have tried several times but everything I try gives an error.  How would I rewrite this to avoid hitting limits.
 
trigger taskTrigger on Task (after insert, after update) {

    List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>();  
    for(task t : Trigger.new){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();       
        if(t.Status == 'Completed' && t.Type == 'Executive Call') {
            
            List<AccountTeamMember> recips = new List<AccountTeamMember>(
                [SELECT UserId 
                 FROM AccountTeamMember 
                 WHERE AccountId = :t.AccountId]);

            Contact c = [SELECT Name FROM Contact WHERE Id = :t.WhoId];
            Account a = [SELECT Name FROM Account WHERE Id = :t.WhatId];
            String taskURL = URL.getSalesforceBaseUrl().toExternalForm() + '/' + t.Id;

            String body = '<b>Executive Sponsor:</b><br/>' + '  ' + t.owner_read__c;
            body += '<br/><br/>';
            body += '<b>Contact:</b><br/>' + '  ' + c.Name;
            body += '<br/><br/>';
            body += '<b>Account:</b><br/>' + '  ' + a.Name;
            body += '<br/><br/>';
            body += '<b>Description:</b><br/>' + t.Description;
            body += '<br/><br/>';
            body += taskURL;  
            
            System.debug('body = '+body);
            mail.setSenderDisplayName('Salesforce System');
            mail.setUseSignature(false);
            mail.setBccSender(false);
            mail.setSaveAsActivity(false);
            mail.setSubject('The Executive Sponsor on this account made a call');
            mail.setHtmlBody(body);
            
            for(AccountTeamMember rid : recips){
                mail.setTargetObjectId(rid.UserId);
                
            // if Sent_Mail__c is null then change to zero then add email count else add email count
                if(t.Sent_Mail__c == null) {
                    t.Sent_Mail__c = 0;
                }
                t.Sent_Mail__c = t.Sent_Mail__c + 1;
            }
        }
        
        atm.add(mail);
    }
    
    Messaging.sendEmail(atm);
}

 
I am writing at trigger that sends an email to account team members whenever a task is completed.  The task description field shows up in the email but the Assigned to and Related to fields are null.  How can I get these fields to show in the email.

trigger MyCoolTrigger on Task (after insert, after update) {

	List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>();  
    for(task t : Trigger.new){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();       
        if(t.Status == 'Completed' && t.Type == 'Executive Call') {
            
            List<AccountTeamMember> recips = new List<AccountTeamMember>(
                [SELECT UserId 
                 FROM AccountTeamMember 
                 WHERE AccountId = :t.AccountId]);

            String body = 'Executive Sponsor:  ' + t.Owner;
            body += '<br/><br/>';
            body += 'Account:  ' + t.What;
            body += '<br/><br/>';
            body += 'Description:<br/> ' + t.Description;  

            mail.setSenderDisplayName('Salesforce System');
            mail.setUseSignature(false);
            mail.setBccSender(false);
            mail.setSaveAsActivity(false);
            mail.setHtmlBody(body);
            
            for(AccountTeamMember rid : recips){
                mail.setTargetObjectId(rid.UserId);
            }
        }
        
        atm.add(mail);
    }
    
    Messaging.sendEmail(atm);
}


I want to send emails to the account team members when a task is complete and the type is executive call.  I got the user ID from the account team members but am getting errors.  what do I need to fix for this trigger to work.

trigger ExecSponsTrig on Task (after insert, after update) {
    
    List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>();  
    EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Executive_Sponsor_Call'];
    for(task t : Trigger.new){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();       
        if(t.Status == 'Completed' && t.Type == 'Executive Call') {
            
            List<AccountTeamMember> recips = new List<AccountTeamMember>(
                [SELECT UserId 
                 FROM AccountTeamMember 
                 WHERE AccountId = :t.AccountId]);
            
            for(AccountTeamMember rid : recips){
                mail.setTargetObjectId(rid.Id);
                mail.setSenderDisplayName('Salesforce System');
                mail.setUseSignature(false);
                mail.setBccSender(false);
                mail.setSaveAsActivity(false);
                mail.setTemplateId(et.Id);
            }
        }
        
        atm.add(mail);
    }
    
    Messaging.sendEmail(atm);
}

I get this error message

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ExecSponsTrig caused an unexpected exception, contact your administrator: ExecSponsTrig: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only Users, Contact or Lead allowed for targetObjectId : 01MM000000CQBLs.: []: Trigger.ExecSponsTrig: line 28, column 1

I want to write a trigger to send an email to the account team whenever a task associated with the account has been completed.  I keep receiving an error on line 15.

Initial term of field expression must be a concrete SObject: LIST<AccountTeamMember>

What would be the best way to complete this trigger?

trigger ExecSponsTrig on Task (after insert, after update) {
    
    List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>();  
    for(task t : Trigger.new){
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();       
        if(t.Status == 'Completed' && t.Type == 'Executive Call') {
            
            List<AccountTeamMember> recips = new List<AccountTeamMember>(
                [SELECT UserId 
                 FROM AccountTeamMember 
                 WHERE AccountId = :t.AccountId]);
            
            for(Id rid : recips){
                mail.setTargetObjectId(recips.Id);
                
            }
        }
        
        atm.add(mail);
    }
}




I'm receiving an error on my visualforce page when I try save records.  It is happening on the subject field for Tasks.  When I checked the Task subject field, it is defined as a picklist, but even when I choose one of the predefined picklist values, I still receive an error.


Task Subject Error
 
public with sharing class AddTasksToCases {

    public Case[] caseList = null;
    public Task cftTask {get; set;}
    
    public AddTasksToCases(ApexPages.StandardSetController setCon) {       
        caseList = setCon.getSelected();
        //newTask = new Task();        
    }

    public pageReference CreateTask(){
        Task[] newTasks = new Task[]{};
        
        for(Case c : caseList){
             Task tk = new Task(
             	 WhatId = c.Id, 
             	 OwnerId = cftTask.Owner.Id, 
             	 Subject = cftTask.Subject, 
             	 Role__c = cftTask.Role__c, 
                 ActivityDate = cftTask.ActivityDate,
                 Description = cftTask.Description,
                 Status = cftTask.Status,
                 Type = cftTask.Type);
             newTasks.add(tk);
            
            if(newTasks.size() >= 200){
                insert newTasks;
                newTasks.clear();
            }
        }
        
        if(!newTasks.isEmpty()){
            insert newTasks;
        }
        
        return new PageReference('/500');
    }
    
}

Visualforce page
 
<apex:page showHeader="true" sidebar="true" standardController="Case" recordSetVar="cases" extensions="AddTasksToCases">
    <apex:sectionHeader title="Cases" subtitle="Mass Task Create" />
    <apex:form >
        <apex:pageBlock title="CFT Task">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Add Tasks" action="{!CreateTask}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
           <apex:pageBlockSection columns="2" title="Assignee Information">
               <apex:inputField value="{!cftTask.OwnerId}"></apex:inputField>
               <apex:inputField value="{!cftTask.Role__c}"></apex:inputField>
               <apex:inputField value="{!cftTask.Type}"></apex:inputField>
               <apex:inputField value="{!cftTask.ActivityDate}"></apex:inputField>
           </apex:pageBlockSection> 
           <apex:pageBlockSection columns="1" title="Task Description">
               <apex:inputField value="{!cftTask.Status}"></apex:inputField>
               <apex:inputField value="{!cftTask.Subject}"></apex:inputField>
               <apex:inputField value="{!cftTask.Description}"></apex:inputField>
            </apex:pageBlockSection>        
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
I am using the REGEX function in a workflow rule, but I keep getting an error

Error: Function REGEX may not be used in this type of formula

I checked https://help.salesforce.com/htviewhelpdoc?id=customize_functions_i_z.htm&siteLang=en_US#REGEX (https://help.salesforce.com/htviewhelpdoc?id=customize_functions_i_z.htm&siteLang=en_US#REGEX) and it says that REGEX is available everywhere except formula fields and custom buttons.

Can anyone tell me why this is not working? 
 
RecordType.DeveloperName = 'IT Case' && REGEX(Description, '[A-Z]{3}[0-9]{1}')