• QD93
  • NEWBIE
  • 20 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 12
    Replies
Hi,

I have an inbound email handler and am hoping to parse the email addresses that enters into Salesforce. I need help to only parse the email addresses from the 'To:' which will be located in the 'Text Body' of the email. What is the best way to only capture these individual emails and add it to a List of Contact emails? Provided below is an example of a email:

---------- Forwarded message ---------
From: John Smith <smith@gmail.com>
Date: Mon, Nov 4, 2019 at 10:19 PM
Subject: Test Email Handler
To: <brown@gmail.com>, <wayne@gmail.com>

This is a test email description.
  • November 05, 2019
  • Like
  • 0
Hi,

The task I am trying to accomplish is a related list button, where I can select related child records from a parent object and click JS button, that sends me to a VF page that provides those select records and a value (number field), where I can inline edit and enter values into the number fields, and once I save the records are updated. Can somebody help provide code sample? Thanks!
  • March 28, 2019
  • Like
  • 0
Hi,

I am new to Lightning Component with fair knowledge in Apex and VF. I have a custom object(B) that is lookup relationship field to the Contact record. I want to create a Lightning Component to display the Contact's related custom object (B) in Gmail (Lightning for Gmail side panel). So whenever I access an Email, the component will display that contact's related custom object. Please help with the code! Thanks.
  • July 25, 2018
  • Like
  • 2
Hi,

I am getting a Null Exception, on line 12-18. The idea here is that if that I am trying to only have my Task's related to ID field = Opportunity. So when I relate the Task to an Account the trigger shouldn't do anything. However, I am getting a null exception in my loop b/c the loop is returning null values. 
 
public class LFGEmailMessageClass {

    public static void InsertTask (list<Task> NewTasks) {
    
    List<Task> tasks       = new List<Task>();
    List<Task> Updatetasks = new List<Task>();
    Set<Id> opportunityIds = new Set<Id>();
    Set<Id> taskIds        = new Set<Id>();
    String Y               = '006';
     
     
     for (Task task: newtasks){  
     if (Task != Null &&
     !((string)task.WhatId).startsWith(Y)){                 
                opportunityids = Null;
        }
     Else if (Task != Null &&
     ((string)task.WhatId).startsWith(Y)){
     
                 opportunityIds.add(task.WhatId);
                taskIds.add(task.Id);   
        }}
       

        Map<Id, Id> opportunities = new Map<Id, Id> ();
        
       if(opportunityids != Null){ 
        for (Opportunity opportunity: [SELECT Id, Name, accountid, company__r.Id FROM Opportunity WHERE Id IN :opportunityIds AND accountid = null]) { 
          
            opportunities.put(opportunity.id, opportunity.company__r.Id);
            } 
            }
        
        Map<Id, Id> TasksMap = new Map <Id, Id> ();
        
        For (Task taskss: [SELECT Id, WhatId FROM Task WHERE Id IN :taskIds]){
            TasksMap.put(taskss.Id,Taskss.WhatId);}

        for (Task task: newTasks) {
            if (task.TaskSubType == 'Email' && 
                task.Status      == 'Completed' &&
                task.WhoId       != Null         &&
                task.WhatId      != Null &&
                ((string)task.WhatId).startsWith(Y)) {
                     
                    Task updateTask     = new Task();
                    updateTask.ID       = task.Id;
                    updateTask.WhatId   = task.WhatId;
                    tasks.add(updateTask);
               
                }
            Else IF (task.TaskSubType == 'Email' && 
                task.Status      == 'Completed' &&
                task.WhoId       != Null         &&
                task.WhatId      != Null &&
                !((string)task.WhatId).startsWith(Y)) {
                     
                    Task updateTask     = new Task();
                    updateTask.ID       = task.Id;
                    updateTask.WhatId   = task.WhatId;
                    UpdateTasks.add(updateTask);  
            } 
            }
            {if(tasks.size()>0){            
             update tasks; } 
            
            Else IF (updateTasks.size()>0){
            update updateTasks;}}
             
            System.debug( Tasks);        
     }

 
  • June 30, 2018
  • Like
  • 0
Hi ,

I would like to convert the below trigger to apex class , let me know how to do it. Currently, the trigger fires when an Opportunity is created on custom object A (company Summary), it create Opportunity Contact Roles relating the newly created opportunity to all the contacts related with the object A (company summary). 
 
trigger createOpportunityContactRoles on Opportunity (after insert) {

    Set<Id> CompanySummaryIds = new Set<Id>();
    List<OpportunityContactRole> ocrList = new List<OpportunityContactRole>();
    Map<Id, List<Contact>> CompanySummaryContacts = new Map<Id, List<Contact>>();
    
    for(Opportunity o: Trigger.New) {
        if(o.aduro__Company_Summary__c != Null) 
            CompanySummaryIds.add(o.aduro__Company_Summary__c);
    }
    
    for(Contact con: [select id, aduro__Company__c from Contact 
                    where aduro__Company__c in: CompanySummaryIds]) {
        if(!CompanySummaryContacts.containsKey(con.aduro__Company__c)) 
            CompanySummaryContacts.put(con.aduro__Company__c, new List<Contact>());
        CompanySummaryContacts.get(con.Aduro__Company__c).add(con);
    }
    
    for(Opportunity opp: Trigger.New) {
        if(CompanySummaryContacts.containskey(opp.aduro__Company_Summary__c) 
            && CompanySummaryContacts.get(opp.aduro__Company_Summary__c) != NULL) {
            Boolean isFirstContact = true;
            for(Contact c: CompanySummaryContacts.get(opp.aduro__Company_Summary__c)) {
                OpportunityContactRole ocr = new OpportunityContactRole(ContactId = c.Id, 
                                                                        OpportunityId = opp.id,
                                                                        Role = 'Investor: Influencer');
                if(isFirstContact) {
                    ocr.IsPrimary = true;
                    isFirstContact = false;
                }
                ocrList.add(ocr);
            }
        }           
    }
    
    if(ocrList.size() > 0)
        insert ocrList;
}

 
  • May 07, 2018
  • Like
  • 0
Hi,

I am looking to perform a Mass Record Create of Opportunity Contact Roles. Scenario: when I create an opportunity from a custom object record (very much like an account) with associated contacts, I'd like to automatically create  contact roles per contacts related to the custom object record and tie those Opportunities to the newly created Opportunity.

The account custom obejct record works very similar like the relationship for Account -- Opportunity -- Contact. 

Right now, the flow does NOT throw an error nor does it create any Opportnity Contact Role records... The Process builder fires when a new Opporunity record is created.

User-added image

User-added image
 
  • May 02, 2018
  • Like
  • 0
Hi,

I am looking for an Apex Class + Trigger to --> when I create an opportunity from a custom object record (very much like an account) with associated contacts, I'd like to automatically create X number of contact roles per contacts related to the custom object record and tie those Opportunities to the newly created Opportunity.

The account custom obejct record works very similar like the relationship for Account -- Opportunity -- Contact. 

I'd love to have the code also to prevent Duplicate Contact Roles (based on the contact Id) in the Opportunity Contact Role. Provided below is the Apex Trigger I have so far. The problem is the Trigger doesn't prevent duplicate Opportunity contact roles from being created, and I dont have an Apex class for it.

AccountSpecific__c --> custom object record
Accountspecificid__c --> custom field on the Contact
AccountSpecificOppId__c --> custom field on the Opportunity
trigger createOpportunityContactRoles on Opportunity (after insert) {

    Set<Id> accSpecificIds = new Set<Id>();
    List<OpportunityContactRole> ocrList = new List<OpportunityContactRole>();
    Map<Id, List<Contact>> accountSpecificContacts = new Map<Id, List<Contact>>();
    
    for(Opportunity o: Trigger.New) {
        if(o.AccountSpecificOppId__c != Null) 
            accSpecificIds.add(o.AccountSpecificOppId__c);
    }
    
    for(Contact con: [select id, AccountSpecificId__c from Contact 
                    where AccountSpecificId__c in: accSpecificIds]) {
        if(!accountSpecificContacts.containsKey(con.AccountSpecificId__c)) 
            accountSpecificContacts.put(con.AccountSpecificId__c, new List<Contact>());
        accountSpecificContacts.get(con.AccountSpecificId__c).add(con);
    }
    
    for(Opportunity opp: Trigger.New) {
        if(accountSpecificContacts.containskey(opp.AccountSpecificOppId__c) 
            && accountSpecificContacts.get(opp.AccountSpecificOppId__c) != NULL) {
            Boolean isFirstContact = true;
            for(Contact c: accountSpecificContacts.get(opp.AccountSpecificOppId__c)) {
                OpportunityContactRole ocr = new OpportunityContactRole(ContactId = c.Id, 
                                                                        OpportunityId = opp.id);
                if(isFirstContact) {
                    ocr.IsPrimary = true;
                    isFirstContact = false;
                }
                ocrList.add(ocr);
            }
        }           
    }
    
    if(ocrList.size() > 0)
        insert ocrList;
}

 
  • May 02, 2018
  • Like
  • 0
Hi, I'm New to Apex...

I'd like to create a Trigger that fires when --> I create an Opportunity record on a custom object. The Custom object has a list of related Contact record. I would like the Trigger to automatically create  Opportunity Contact Roles, each Contact Roles with the related Contact record (related to the custom object) as the Contact Id, and the newly created Opportunity record as the Opportunity Id. 

The custom object is almost use as in an Account (custom object)--Opportunity--Contact format. 

The custom recod field is --> a__Co_Sum__c

Provided below is fairly similar Apex trigger, but it is design for the Account. Where the trigger will add all account contacts to the contact roles when creating a new opportunity. I'd love to have this code written but replacing Account with the custom record a__Co_Sum__c
trigger CreateContactRole on Opportunity (after insert, after update) {

    //get the id of all involved accounts
    Set<ID> accountIds = new Set<ID>();
    for(Opportunity opt:Trigger.New){
        accountIds.add(opt.AccountId);
    }
    
    //get all contacts for those accounts
    list<Contact> contacts = new list<Contact>();
    contacts = [select id, AccountId from Contact where AccountId in: accountIds order by createddate Limit 5000];
    
    //organize these contacts by account
    Map<Id,List<Contact>> contactsByAccount = new Map<ID,List<Contact>>();
    for(Contact c:contacts){
        if(contactsByAccount.get(c.AccountId) == null){
            contactsByAccount.put(c.AccountId,new List<Contact>());
        }
        contactsByAccount.get(c.AccountId).add(c);
    }
    
    // check to see if the Opportunity already has a contact role.  If it does, add to a set of Ids to exclude
    List<OpportunityContactRole> existingOCR = new List<OpportunityContactRole>();
    Set<Id> existingOCRIds = new Set<Id>();
    existingOCR = [select OpportunityId from OpportunityContactRole where OpportunityId in:Trigger.newMap.keySet() limit 5000];
    for(OpportunityContactRole ocr:existingOCR) if(!existingOCRIds.contains(ocr.OpportunityId)) existingOCRIds.add(ocr.OpportunityId);
    
    //create the OpportunityContactRole objects
    list<OpportunityContactRole> lstOCR = new list<OpportunityContactRole>();
    for(Opportunity opt:Trigger.New){
        if(!existingOCRIds.contains(opt.Id) && contactsByAccount.get(opt.AccountId) != null){
            Boolean firstContact = true;
            for(Contact c: contactsByAccount.get(opt.AccountId)){
                OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId=opt.id, ContactId=c.id);
                if(firstContact) {
                    ocr.IsPrimary = TRUE;
                    firstContact = FALSE;
                }
                lstOCR.add(ocr);
            }
        }
    }
    insert lstOCR;
}

 
  • May 01, 2018
  • Like
  • 0
New to Apex...

Whenever a Task is created, I'd like to relate the Task's WhatID to the related Contact's (whoid) lookup field (custom field) TriggerTest__c. 

I have been receivin this error message : Error: Compile Error: Cannot save a trigger during a parse and save class call at line -1 column -1
 
trigger TaskRelatedToContact on Task (after insert,after update) {


Set<Id> ContactIds = new Set<Id>(); for(Task t : trigger.new)
{String wId = t.WhoId; 
if(wId!=null && wId.startsWith('003') && 
   !ContactIds.contains(t.WhoId)) 
    
    { ContactIds.add(t.WhoId); } }


List<Contact> 
taskContacts = [Select Id, TriggerTest__c 
from Contact where Id in :ContactIds]; 
Map<Id, Contact> ContactMap = new Map<Id, Contact>(); 
for(CONTACT c : taskContacts)
{ ContactMap.put(c.Id,c); } 


for(Task t : trigger.new){ String wId = t.WhoId; 
if(wId!=null && wId.startswith('003')){CONTACT thisContact = ContactMap.get(t.WhoId); 
if(thisContact!=null){t.WhatId = thiscontact.TriggerTest__c;} 
} 
} 
}

 
  • April 24, 2018
  • Like
  • 0
Hi,

I am new to Lightning Component with fair knowledge in Apex and VF. I have a custom object(B) that is lookup relationship field to the Contact record. I want to create a Lightning Component to display the Contact's related custom object (B) in Gmail (Lightning for Gmail side panel). So whenever I access an Email, the component will display that contact's related custom object. Please help with the code! Thanks.
  • July 25, 2018
  • Like
  • 2
Hi,

The task I am trying to accomplish is a related list button, where I can select related child records from a parent object and click JS button, that sends me to a VF page that provides those select records and a value (number field), where I can inline edit and enter values into the number fields, and once I save the records are updated. Can somebody help provide code sample? Thanks!
  • March 28, 2019
  • Like
  • 0
Hi,

I am getting a Null Exception, on line 12-18. The idea here is that if that I am trying to only have my Task's related to ID field = Opportunity. So when I relate the Task to an Account the trigger shouldn't do anything. However, I am getting a null exception in my loop b/c the loop is returning null values. 
 
public class LFGEmailMessageClass {

    public static void InsertTask (list<Task> NewTasks) {
    
    List<Task> tasks       = new List<Task>();
    List<Task> Updatetasks = new List<Task>();
    Set<Id> opportunityIds = new Set<Id>();
    Set<Id> taskIds        = new Set<Id>();
    String Y               = '006';
     
     
     for (Task task: newtasks){  
     if (Task != Null &&
     !((string)task.WhatId).startsWith(Y)){                 
                opportunityids = Null;
        }
     Else if (Task != Null &&
     ((string)task.WhatId).startsWith(Y)){
     
                 opportunityIds.add(task.WhatId);
                taskIds.add(task.Id);   
        }}
       

        Map<Id, Id> opportunities = new Map<Id, Id> ();
        
       if(opportunityids != Null){ 
        for (Opportunity opportunity: [SELECT Id, Name, accountid, company__r.Id FROM Opportunity WHERE Id IN :opportunityIds AND accountid = null]) { 
          
            opportunities.put(opportunity.id, opportunity.company__r.Id);
            } 
            }
        
        Map<Id, Id> TasksMap = new Map <Id, Id> ();
        
        For (Task taskss: [SELECT Id, WhatId FROM Task WHERE Id IN :taskIds]){
            TasksMap.put(taskss.Id,Taskss.WhatId);}

        for (Task task: newTasks) {
            if (task.TaskSubType == 'Email' && 
                task.Status      == 'Completed' &&
                task.WhoId       != Null         &&
                task.WhatId      != Null &&
                ((string)task.WhatId).startsWith(Y)) {
                     
                    Task updateTask     = new Task();
                    updateTask.ID       = task.Id;
                    updateTask.WhatId   = task.WhatId;
                    tasks.add(updateTask);
               
                }
            Else IF (task.TaskSubType == 'Email' && 
                task.Status      == 'Completed' &&
                task.WhoId       != Null         &&
                task.WhatId      != Null &&
                !((string)task.WhatId).startsWith(Y)) {
                     
                    Task updateTask     = new Task();
                    updateTask.ID       = task.Id;
                    updateTask.WhatId   = task.WhatId;
                    UpdateTasks.add(updateTask);  
            } 
            }
            {if(tasks.size()>0){            
             update tasks; } 
            
            Else IF (updateTasks.size()>0){
            update updateTasks;}}
             
            System.debug( Tasks);        
     }

 
  • June 30, 2018
  • Like
  • 0
Hi,

I am looking for an Apex Class + Trigger to --> when I create an opportunity from a custom object record (very much like an account) with associated contacts, I'd like to automatically create X number of contact roles per contacts related to the custom object record and tie those Opportunities to the newly created Opportunity.

The account custom obejct record works very similar like the relationship for Account -- Opportunity -- Contact. 

I'd love to have the code also to prevent Duplicate Contact Roles (based on the contact Id) in the Opportunity Contact Role. Provided below is the Apex Trigger I have so far. The problem is the Trigger doesn't prevent duplicate Opportunity contact roles from being created, and I dont have an Apex class for it.

AccountSpecific__c --> custom object record
Accountspecificid__c --> custom field on the Contact
AccountSpecificOppId__c --> custom field on the Opportunity
trigger createOpportunityContactRoles on Opportunity (after insert) {

    Set<Id> accSpecificIds = new Set<Id>();
    List<OpportunityContactRole> ocrList = new List<OpportunityContactRole>();
    Map<Id, List<Contact>> accountSpecificContacts = new Map<Id, List<Contact>>();
    
    for(Opportunity o: Trigger.New) {
        if(o.AccountSpecificOppId__c != Null) 
            accSpecificIds.add(o.AccountSpecificOppId__c);
    }
    
    for(Contact con: [select id, AccountSpecificId__c from Contact 
                    where AccountSpecificId__c in: accSpecificIds]) {
        if(!accountSpecificContacts.containsKey(con.AccountSpecificId__c)) 
            accountSpecificContacts.put(con.AccountSpecificId__c, new List<Contact>());
        accountSpecificContacts.get(con.AccountSpecificId__c).add(con);
    }
    
    for(Opportunity opp: Trigger.New) {
        if(accountSpecificContacts.containskey(opp.AccountSpecificOppId__c) 
            && accountSpecificContacts.get(opp.AccountSpecificOppId__c) != NULL) {
            Boolean isFirstContact = true;
            for(Contact c: accountSpecificContacts.get(opp.AccountSpecificOppId__c)) {
                OpportunityContactRole ocr = new OpportunityContactRole(ContactId = c.Id, 
                                                                        OpportunityId = opp.id);
                if(isFirstContact) {
                    ocr.IsPrimary = true;
                    isFirstContact = false;
                }
                ocrList.add(ocr);
            }
        }           
    }
    
    if(ocrList.size() > 0)
        insert ocrList;
}

 
  • May 02, 2018
  • Like
  • 0
Hi, I'm New to Apex...

I'd like to create a Trigger that fires when --> I create an Opportunity record on a custom object. The Custom object has a list of related Contact record. I would like the Trigger to automatically create  Opportunity Contact Roles, each Contact Roles with the related Contact record (related to the custom object) as the Contact Id, and the newly created Opportunity record as the Opportunity Id. 

The custom object is almost use as in an Account (custom object)--Opportunity--Contact format. 

The custom recod field is --> a__Co_Sum__c

Provided below is fairly similar Apex trigger, but it is design for the Account. Where the trigger will add all account contacts to the contact roles when creating a new opportunity. I'd love to have this code written but replacing Account with the custom record a__Co_Sum__c
trigger CreateContactRole on Opportunity (after insert, after update) {

    //get the id of all involved accounts
    Set<ID> accountIds = new Set<ID>();
    for(Opportunity opt:Trigger.New){
        accountIds.add(opt.AccountId);
    }
    
    //get all contacts for those accounts
    list<Contact> contacts = new list<Contact>();
    contacts = [select id, AccountId from Contact where AccountId in: accountIds order by createddate Limit 5000];
    
    //organize these contacts by account
    Map<Id,List<Contact>> contactsByAccount = new Map<ID,List<Contact>>();
    for(Contact c:contacts){
        if(contactsByAccount.get(c.AccountId) == null){
            contactsByAccount.put(c.AccountId,new List<Contact>());
        }
        contactsByAccount.get(c.AccountId).add(c);
    }
    
    // check to see if the Opportunity already has a contact role.  If it does, add to a set of Ids to exclude
    List<OpportunityContactRole> existingOCR = new List<OpportunityContactRole>();
    Set<Id> existingOCRIds = new Set<Id>();
    existingOCR = [select OpportunityId from OpportunityContactRole where OpportunityId in:Trigger.newMap.keySet() limit 5000];
    for(OpportunityContactRole ocr:existingOCR) if(!existingOCRIds.contains(ocr.OpportunityId)) existingOCRIds.add(ocr.OpportunityId);
    
    //create the OpportunityContactRole objects
    list<OpportunityContactRole> lstOCR = new list<OpportunityContactRole>();
    for(Opportunity opt:Trigger.New){
        if(!existingOCRIds.contains(opt.Id) && contactsByAccount.get(opt.AccountId) != null){
            Boolean firstContact = true;
            for(Contact c: contactsByAccount.get(opt.AccountId)){
                OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId=opt.id, ContactId=c.id);
                if(firstContact) {
                    ocr.IsPrimary = TRUE;
                    firstContact = FALSE;
                }
                lstOCR.add(ocr);
            }
        }
    }
    insert lstOCR;
}

 
  • May 01, 2018
  • Like
  • 0
New to Apex...

Whenever a Task is created, I'd like to relate the Task's WhatID to the related Contact's (whoid) lookup field (custom field) TriggerTest__c. 

I have been receivin this error message : Error: Compile Error: Cannot save a trigger during a parse and save class call at line -1 column -1
 
trigger TaskRelatedToContact on Task (after insert,after update) {


Set<Id> ContactIds = new Set<Id>(); for(Task t : trigger.new)
{String wId = t.WhoId; 
if(wId!=null && wId.startsWith('003') && 
   !ContactIds.contains(t.WhoId)) 
    
    { ContactIds.add(t.WhoId); } }


List<Contact> 
taskContacts = [Select Id, TriggerTest__c 
from Contact where Id in :ContactIds]; 
Map<Id, Contact> ContactMap = new Map<Id, Contact>(); 
for(CONTACT c : taskContacts)
{ ContactMap.put(c.Id,c); } 


for(Task t : trigger.new){ String wId = t.WhoId; 
if(wId!=null && wId.startswith('003')){CONTACT thisContact = ContactMap.get(t.WhoId); 
if(thisContact!=null){t.WhatId = thiscontact.TriggerTest__c;} 
} 
} 
}

 
  • April 24, 2018
  • Like
  • 0