function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tommy GeorgiouTommy Georgiou 

prepopulating contact and predifining template for send an email through Orders

Hi,

I want to create a custom button where I can prepopulate the ''To'' on the send an email function through Orders and predefine the email tempate as well.

I was able to manage the first scale to prepopulate the ''To" button by the help of a fellow SF user.

My steps where: 
1) Created a contact record associated with Supplier's value under Order.
2) Created a custom text field under Account to capture contact id of supplier's contact (might be called as Supplier's Contact ID). Because I have multiple order records under that particular account, I created an apex trigger as well ( see below)
3)Created Custom Task button to do URL hacking in order to prepopulate the field : https://cs18.salesforce.com/_ui/core/email/author/EmailAuthor?p2_lkid={!Account.Supplier_s_Contact_ID__c}&p3_lkid={!Account.Id}&retURL=%2F{!Account.Id}

trigger : 
trigger PopulateSupplierContactUnderAccount on Order (after insert, after update) {
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert || Trigger.isUpdate)
        {
            Set<Id> setAccountIds = new Set<Id>();
            Map<Id,Id> mapOrderId_AccountId = new Map<Id,Id>();
            if(Trigger.isInsert)
            {
                for(Order o : Trigger.new)
                {
                    if(o.Supplier_s_Contact__c != NULL) //Supplier_s_Contact__c is API name of Supplier's Contact lookup field in Order object.
                    {
                        setAccountIds.add(o.AccountId);
                        mapOrderId_AccountId.put(o.Id, o.AccountId);
                    }
                }
            }
            else if(Trigger.isUpdate)
            {
                for(Integer i = 0; i < Trigger.size; i++)
                {
                    if(Trigger.new[i].Supplier_s_Contact__c != NULL && Trigger.new[i].Supplier_s_Contact__c != Trigger.old[i].Supplier_s_Contact__c)
                    {
                        setAccountIds.add(Trigger.new[i].AccountId);
                        mapOrderId_AccountId.put(Trigger.new[i].Id, Trigger.new[i].AccountId);
                    }
                }
            }
            //Supplier_s_Contact_ID__c is API name of Supplier's Contact ID custom text field in Account object.
            //Supplier_s_Contact_Name__c is API name of Supplier's Contact Name custom text field in Account object.
            Map<Id,Account> mapAccId_Account = new Map<Id,Account>();
            for(Account acc : [SELECT Id, Supplier_s_Contact_ID__c, Supplier_s_Contact_Name__c FROM Account WHERE Id IN :setAccountIds])
            {
                mapAccId_Account.put(acc.Id, acc);
            }
            List<Account> listAccounts = new List<Account>();
            Set<Id> setUpdatedAccountIds = new Set<Id>();
            for(Order o : [SELECT Id, AccountId, Supplier_s_Contact__c, Supplier_s_Contact__r.Name FROM Order WHERE Id IN :mapOrderId_AccountId.keySet()])
            {
                setUpdatedAccountIds.add(mapOrderId_AccountId.get(o.Id));
                Account getAcc = mapAccId_Account.get(mapOrderId_AccountId.get(o.Id));
                getAcc.Supplier_s_Contact_ID__c = o.Supplier_s_Contact__c;
                getAcc.Supplier_s_Contact_Name__c = o.Supplier_s_Contact__r.Name;
                if(!setUpdatedAccountIds.contains(getAcc.Id) || setUpdatedAccountIds.size() == 1)
                {
                    listAccounts.add(getAcc);
                }
            }
            if(!listAccounts.isEmpty())
            {
                update listAccounts;
            }
        }
    }
}

So far so good. "To" is prepopulated. What I need is to predefine the email template as well. I have found an article from Werewolf where he suggests a custom detail button with java behavior:  ​location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&p3_lkid={!Case.Id}&rtype=003&p2_lkid={!Case.ContactId}&template_id=<your template here>');

How can I mix these two into one button in order to prepopulate contact and predefine the template?
Best Answer chosen by Tommy Georgiou
Tommy GeorgiouTommy Georgiou

After digging a bit more. For the reference of others the solution is: 1) Create a custom field button that the takes the contact's ID from the account 2)Then a lookup field on the Orders for the contacts 3) Then create a custom button as a detail page button, executing Java. The code for the button is :
 
location.replace('/email/author/emailauthor.jsp?retURL=/{!Order.Id}&p3_lkid={!Order.Id}&rtype=003&p3_lkid={!Order.OrderNumber}&p3_1kid={!$User.Email}&p2_lkid={! Order.Supplier_s_ContactId__c }&template_id=00Xw0000001UHlg');




where Supplier_s_ContactId__c is the custom field that you create on the first step. Works like a charm