Note: this process uses triggers. We wish Salesforce did this by default without workarounds!
If you want the Chili Piper meetings to be automatically moved to the opportunity activity section (when the meetings are related to opportunity contact roles), here is what you need to do:
- You should log in to your Sandbox, navigate to Build-> Customize -> Activities ->Event triggers, and add the below code:
trigger ChangeRelatedTo on Event (before insert) { List contactsToCheck = [SELECT ContactId, OpportunityId, CreatedDate FROM OpportunityContactRole WHERE IsDeleted = false ORDER BY CreatedDate DESC]; for( Event e : Trigger.new){ if (e.WhoId != null && e.Meeting_Type_CP__c == 'Demo Mid Market'){ for ( OpportunityContactRole c: contactsToCheck ){ if (e.WhoId.equals(c.ContactId) ){ String oId = c.OpportunityId; List opportunity; opportunity o = new opportunity(); o = [SELECT IsClosed, Id From Opportunity WHERE Id = :c.OpportunityId]; if (!o.IsClosed){ e.WhatId = c.OpportunityId; break; } } } } } }
Make sure the field: “IsActive” is checked before saving it.
- After that, you need to add an apex class that will test the trigger so that Salesforce will allow you to deploy it to prod environment. The test class should be added under Build->Develop ->Apex Classes
@isTest private class ChangeRelatedToTest{ static testMethod void myUnitTest() { System.debug('Start...' ); Set LeadIds = new Set(); //Create Data for Customer Objet //User System.debug('Inserting a user' ); User u = new User (Username = 'testName@name.com', EmailEncodingKey='ISO-8859-1', Alias='tdummy2', Email='testName@name.com', TimeZoneSidKey='America/Chicago', LanguageLocaleKey='en_US', LocaleSidKey='en_US', LastName='dummy', ProfileId='00e410000018LuUAAU'); insert u; System.debug('User ID' + u.Id ); //create account Account a = new Account(Name='dummyAccount'); Insert a; System.debug('Inserting account id = ' + a.Id ); //create contact Contact c = new Contact(LastName='dummylastname', OwnerId = u.Id, Email = 'dummy@email.com', AccountId = a.Id); insert c; System.debug('Inserting contact id = ' + c.Id ); //create opportunity Opportunity o = new Opportunity(name='dummyOpporunity', AccountId=a.Id, CloseDate =date.today(), StageName ='Qualification', Type='New Business', Probability =10.0); insert o; System.debug('Inserting opporuntiy id = ' + o.Id ); //addign a contact role OpportunityContactRole ocr = new OpportunityContactRole(OpportunityId =o.id, ContactId =c.Id, IsPrimary =true, Role = 'Decision Maker'); insert ocr; System.debug('Inserting OpporunityContactRole id = ' + ocr.Id ); //Now, our trigger will fire on After insert event Test.startTest(); Event e = new Event( Meeting_Type_CP__c='Demo Mid Market', WhoId = c.Id, OwnerId = u.Id, IsAllDayEvent = true, ActivityDate = Date.today(), WhatId=a.Id); insert e; List testqueryres = [SELECT WhatId FROM Event WHERE Id = :e.Id]; system.assertEquals(testqueryres.get(0).WhatId, o.Id ); Test.stopTest(); } }
Please note that you need to adjust it to work in your Salesforce instance.
You should replace:
-Meeting type “Demo Mid Market” – with a type from Chili Piper. If you want the trigger to work with multiple meeting types, you could add it as below:
if (e.WhoId != null && (e.Meeting_Type_CP__c == 'Demo Mid Market' || e.Meeting_Type_CP__c == '')
-ProfileId with a profile ID from your sandbox instance. Make sure you select a profile that has permissions to create Accounts, Contacts, Opportunities, and Events
-StageName – with an opportunity stage you have in Salesforce
-Type – a type that you have in Salesforce
-Role – a role that you have in Salesforce
- All you need to do now is deploy the apex trigger and the test class from Sandbox to your production environment.