• SFWes
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 5
    Replies

Is it possible to write an Email2Case Email Trigger that would overwrite the From Address? Right now, a user can select from multiple email addresses when sending responses to a customer. This is a potential problem as they may select the wrong email address.

 

I looked into how this can be done from Salesforce without Apex, but apparently it doesn't seem possible right now. So can this be done with the Email Message Trigger?

 

Thank you,

  • October 03, 2013
  • Like
  • 0

I've created the following Trigger below. This Trigger works as expected when manually creating records, but for some reason when it's executed because of Workflow, it doesn't work. It also seems as if the Trigger is being executed multiple times. I put some System.Debug lines in to see where the code is and isn't executing and I noticed that some of the debug lines were being executed multiple times. Am I doing something wrong?

 

Here is how it should work - When a specific field on the Opp is altered, a workflow is triggered which creates a new Task. When the Task is inserted, then this Triggered executes. This Trigger is supposed to check if the owner of the Task is the same owner of the Account that the Opportunity is tied to. If they are the same, then a field on the Task is updated. 

 

Like is said, if I manually make this Task then the Trigger works as intended. It even works when I make updates to the Task, like change the owner or Record Type, but it doesn't work when triggered from workflow.

 

Any help would be greatly appreciated.

 

 

This was really my first Trigger learning about not exceeding govern limits, so my apologies if the code isn't all that great.

 

Trigger TaskandAccountOwnerMatch on Task (after insert, after update) {
    
    List<Task> tasks = new List<Task>(); //Tasks to Update
    Map<Id, Task> taskMap = new Map<Id, Task>();//Task & Opportunity Mapping
    Map<id, id> oppAccMap = new Map<id, id>(); //Account & Opportunity Mapping
    Map<Account, Task> accTaskMap = new Map<Account, Task>(); //Account & Task Mapping
    Id rtTaskID = [select Id from RecordType where name = 'Standard Task' and SObjectType = 'Task' limit 1].Id;
    
    
    //Loop through the Trigger
    for(Task tsk : Trigger.new){
        
        //This code should only apply only to one Task Record Type
        if(tsk.RecordTypeId == rtTaskId){
            //Ensure there is a value provided and that it's for an Opportunity
            if(tsk.WhatId != null && string.valueOf(tsk.WhatId).startsWith('006')){
                if(Trigger.isInsert ){ 
                    taskMap.put(tsk.WhatId, tsk); //Add to Mapping
                }
                else if (Trigger.isUpdate){
                    //To avoid recursion (govern limits), we check to see if the fields have actually changed.
                    if(Trigger.oldMap.get(tsk.id).ownerId != tsk.ownerid || Trigger.oldMap.get(tsk.id).WhatId != tsk.WhatId){
                        taskMap.put(tsk.WhatId, tsk); //Add to Mapping
                    }
                }
            }else{//No value provided or it's not an Opportunity
                if (Trigger.isUpdate){
                    //If the related object has changed or the checkbox is true
                    if(Trigger.oldMap.get(tsk.id).WhatId != tsk.WhatId || tsk.Task_and_Account_Owner_Match__c == True
                      || Trigger.oldMap.get(tsk.id).RecordTypeId != tsk.RecordTypeId){
                        tasks.add(new Task(id = tsk.id, Task_and_Account_Owner_Match__c = False));
                    }
                }
            }
        }
    }
    
    //Only move forward if the taskMap has values
    if(taskMap.size() > 0){
        //Map AccountID with OpportunityID
        for(Opportunity opp : [select Id, AccountId from Opportunity where Id in : taskMap.keySet()]){
            oppAccMap.put(opp.AccountId, opp.Id); //Add the AcountID and OpportunityId to the Map
        }
    
        //Map Account to Task
        for(Account acc : [select id, Name, OwnerId from Account where Id in : oppAccMap.keySet()]){
            accTaskMap.put(acc, taskMap.get(oppAccMap.get(acc.id)));    
        }
    
        for(Account accTmp : accTaskMap.keySet()){
            //Assume the Owners don't match
            Boolean isMatch = True;
            //Check if Account owner and Task owner match
            if (accTmp.OwnerId == ((Task)accTaskMap.get(accTmp)).OwnerId){
                isMatch = True;
            }

            tasks.add(new Task(id = accTaskMap.get(accTmp).id, Task_and_Account_Owner_Match__c = isMatch));
        }
    }
    
    //Proceed with update if tasks size is greater then 0
    if (tasks.size() > 0){
        update tasks;
    }
}

 

  • August 14, 2013
  • Like
  • 0

Hi. I'm writing a Trigger for the Task object and I'm not sure if the code below is the most efficient way of achieving the result I need. The code works, but I want to see if it can be more efficient. I'm also just learning how to use Maps, which is a bit confusing right now for me.

 

This Trigger fires on Update / Insert and will set a checkbox to True on the Task object if the owner matches the Account Owner. If they don't match, then it will be set to False.

 

Any advise would be appreciated.

 

trigger TaskTest on Task (after insert, after update) {
	
	List<Task> tasks = new List<Task>(); //Tasks to Update
	Map<Id, Task> taskMap = new Map<Id, Task>();//Task & Opportunity Mapping
    Map<id, id> oppAccMap = new Map<id, id>(); //Account & Opportunity Mapping
    Map<Account, Task> accTaskMap = new Map<Account, Task>(); //Account & Task Mapping
	//Id rtTaskID = [select Id from RecordType where DeveloperName = 'Standard Task' and SObjectType = 'Task' limit 1].Id;
    
    
    //Loop through the Trigger
	for(Task tsk : Trigger.new){
        //This code should only apply to one Task Record Type
        //if(tsk.RecordTypeId == rtTaskId && tsk.WhatId != null && string.valueOf(tsk.WhatId).startsWith('006')){

            if(Trigger.isInsert ){ 
                taskMap.put(tsk.WhatId, tsk); //Add to Mapping
            }
            else if (Trigger.isUpdate){
                if(Trigger.oldMap.get(tsk.id).ownerId != tsk.ownerid || Trigger.oldMap.get(tsk.id).WhatId != tsk.WhatId)
                    taskMap.put(tsk.WhatId, tsk); //Add to Mapping
            }
    	//}
	}
	
    //Only move forward if the taskMap has values
    if(taskMap.size() > 0){
    
        //Map AccountID with OpportunityID
        for(Opportunity opp : [select Id, AccountId from Opportunity where Id in : taskMap.keySet()]){
            oppAccMap.put(opp.AccountId, opp.Id); //Add the AcountID and OpportunityId to the Map
        }
    
        //Map Account to Task
        for(Account acc : [select id, Name, OwnerId from Account where Id in : oppAccMap.keySet()]){
            accTaskMap.put(acc, taskMap.get(oppAccMap.get(acc.id)));	
        }
    
        for(Account accTmp : accTaskMap.keySet()){
            //Assume the Owners don't match
            Boolean isMatch = false;
            //Check if Account owner and Task owner match
            if (accTmp.OwnerId == ((Task)accTaskMap.get(accTmp)).OwnerId){
                isMatch = True;
            }

            tasks.add(new Task(id = accTaskMap.get(accTmp).id, Task_and_Account_Owner_Match__c = isMatch));
        }
        
        //Proceed with update if tasks size is greater then 0
        if (tasks.size() > 0){
            update tasks;
        }
    
    }
 
}

 

 

 

  • July 16, 2013
  • Like
  • 0

I'm fairly new to Apex code and was wondering if someone could help review a trigger for me.

 

Background: We have On-Demand Email-To-Case enabled and will be routing a lot of emails to different addresses in SalesForce

 

What I need is to have the email address that the email was sent to populated on the Case. This will be used for filtering list views in the service console. I couldn't figure out how to get this field on the Case since it resides on the EmailMessage object, so I wrote a Case Email Trigger.

 

I basically just want to know if what I did here is best practice. Any advice would greatly be appreciated.

 

trigger UpdateToField on EmailMessage (after insert) {
    
    List<Case> cases = new List<Case>();
    
    for(EmailMessage em: Trigger.new){
        Case cs = [select SentToAddress__c from Case where Id =: em.ParentId];
        cs.SentToAddress__c = em.ToAddress;
        cases.add(cs);
    }
    update cases;
    
}

 

  • June 28, 2013
  • Like
  • 0

Hi All. I'm having an issue when trying to run this trigger. I believe it's because the record is locked.

 

This is all based off of an email-to-case process. When the Case is created, the system should link the appropriate contact to the Case. However, if the contact is not in ur system, only the Supplied fields are populated. What I want to do is, create a new Contact, Add it to a generic Account and then insert it into the newly created Case. But I'm having issues. My code is below.

 

trigger NewContactAdd on Case (after insert) {

        Id rccAcc = [select Id from Account where Name = 'RCC Unassigned'].Id;
        Id rtID = [select Id from RecordType where name = 'Customer Email' and SObjectType = 'Case' limit 1].Id;
        List<Case> cases = new List<Case>();
        Contact webCont = null;
        
        for(Case cs: Trigger.new){
			cases.add(cs);
            if(cs.RecordTypeId == rtId && cs.Origin == 'Email (CUS)'){

                if(cs.Contact == Null){
                    webCont = new Contact(
                    FirstName = cs.SuppliedName,
                    LastName = cs.SuppliedName,
                    Email = cs.SuppliedEmail,
                    AccountId = rccAcc
                   ); 
                   insert webCont;
                }
            }
        }
    
        for(Integer i=0; i < cases.size(); i++){
            Case c = cases.get(i);
        	c.ContactId = webCont.Id;
        }

}

 

  • June 24, 2013
  • Like
  • 0

I'm fairly new to Apex code and was wondering if someone could help review a trigger for me.

 

Background: We have On-Demand Email-To-Case enabled and will be routing a lot of emails to different addresses in SalesForce

 

What I need is to have the email address that the email was sent to populated on the Case. This will be used for filtering list views in the service console. I couldn't figure out how to get this field on the Case since it resides on the EmailMessage object, so I wrote a Case Email Trigger.

 

I basically just want to know if what I did here is best practice. Any advice would greatly be appreciated.

 

trigger UpdateToField on EmailMessage (after insert) {
    
    List<Case> cases = new List<Case>();
    
    for(EmailMessage em: Trigger.new){
        Case cs = [select SentToAddress__c from Case where Id =: em.ParentId];
        cs.SentToAddress__c = em.ToAddress;
        cases.add(cs);
    }
    update cases;
    
}

 

  • June 28, 2013
  • Like
  • 0

Hi All. I'm having an issue when trying to run this trigger. I believe it's because the record is locked.

 

This is all based off of an email-to-case process. When the Case is created, the system should link the appropriate contact to the Case. However, if the contact is not in ur system, only the Supplied fields are populated. What I want to do is, create a new Contact, Add it to a generic Account and then insert it into the newly created Case. But I'm having issues. My code is below.

 

trigger NewContactAdd on Case (after insert) {

        Id rccAcc = [select Id from Account where Name = 'RCC Unassigned'].Id;
        Id rtID = [select Id from RecordType where name = 'Customer Email' and SObjectType = 'Case' limit 1].Id;
        List<Case> cases = new List<Case>();
        Contact webCont = null;
        
        for(Case cs: Trigger.new){
			cases.add(cs);
            if(cs.RecordTypeId == rtId && cs.Origin == 'Email (CUS)'){

                if(cs.Contact == Null){
                    webCont = new Contact(
                    FirstName = cs.SuppliedName,
                    LastName = cs.SuppliedName,
                    Email = cs.SuppliedEmail,
                    AccountId = rccAcc
                   ); 
                   insert webCont;
                }
            }
        }
    
        for(Integer i=0; i < cases.size(); i++){
            Case c = cases.get(i);
        	c.ContactId = webCont.Id;
        }

}

 

  • June 24, 2013
  • Like
  • 0

Hi Guys,

 

I have created a field in the Standard object and i didn't added to any pagelayouts. Will this field by available the reports or not?

 

Thanks,

Bujji

  • May 29, 2013
  • Like
  • 0

Does anyone know how to get the value of the Account Team Members to the Account object?  My issue is that I have a custom object with a master-detail relationship to the Account object.  On my custom object, I would like to list one of the Account Team members if they are a certain role.  The only way I can see to do this would be to have the value of the Account Team on the Account level either as a text field or a lookup field.  I don't need it to be a lookup field on my custom object, just the first and last names.  Can anyone help or at least let me know if it's possible?  Thanks

  • September 25, 2012
  • Like
  • 0