You need to sign in to do that
Don't have an account?
Abraham kumar 4
Urgent:-To Modify Trigger to fire on insert Currently fires on update
Hi All,
I have the below trigger which when few field update happens to contact record it sends a webservice callout. Now i want it to send a webservice call out even when the contact is created in salesforce, can anyone please help me in modying this trigger such that it sends callout after record is newly created in salesforce also and not only on updation like it is currently doing. It should send callout during both update and create actions. Thanks!!
Many thanks in advance
Abraham
I have the below trigger which when few field update happens to contact record it sends a webservice callout. Now i want it to send a webservice call out even when the contact is created in salesforce, can anyone please help me in modying this trigger such that it sends callout after record is newly created in salesforce also and not only on updation like it is currently doing. It should send callout during both update and create actions. Thanks!!
trigger Contactcallout on Contact (after update) { Map<Id, String> m = new Map<Id, String>(); list<Contact> validContacts = new list<Contact>(); set<ID> accIds = new set<ID>(); for (contact c : Trigger.new) { if(c.RecordTypeId == '012D0000000BaFA'){ contact old = Trigger.oldMap.get(c.Id); if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID) { validContacts.add(c); accIds.add(c.accountid); } } } map<ID, Account> accMap; if(!accIds.IsEmpty()) // guard condition for SOQL accMap = new map<ID, Account>([select name from account where id in :accIds]); for (contact c : validContacts) { Account acc = accMap.get(c.AccountID); string accName = acc == null ? null : acc.Name; WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c); } }
Many thanks in advance
Abraham
Hi Abraham,
Because you are trying to access trigger.oldMap during insert, it will fail, Check below code it should work.
All Answers
I can point out a couple of things which will help you modify this trigger to work on update:
1) You are using a hardcoded recordtype id which may not working in production,you can query your record type id using "
RecordType rt = [SELECT Id FROM RecordType WHERE SObjectType='OBJECT_NAME_HERE' AND Name='RECORDTYPE_NAME_HERE' LIMIT 1];
" you can also get it dynamically.
2)
//added another event for after insert
trigger Contactcallout on Contact (after update, after insert ) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
for (contact c : Trigger.new) {
if(c.RecordTypeId == '012D0000000BaFA'){
//if trigger is update it will check the existing condition else it will directly add it to list of contacts and set if account ids.
if(trigger.isupdate){
contact old = Trigger.oldMap.get(c.Id);
if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID)
{
validContacts.add(c);
accIds.add(c.accountid);
}
}else{
validContacts.add(c);
accIds.add(c.accountid);
}
}
map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
accMap = new map<ID, Account>([select name from account where id in :accIds]);
for (contact c : validContacts) {
Account acc = accMap.get(c.AccountID);
//this can be change to acc.name == null ? null : acc.Name; without an additional variable
string accName = acc == null ? null : acc.Name;
WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
}
}
Thanks,
Fahad Akhtar
Please try following code:
Hope its help you.
Please marks as solution if it solve your issue, so it will help people who will face same issue.
Thanks!
I have modified your codeso that it can work on both inser and update.
Few words to tell you,
Previously, the call out will only be called when the Email, Firstname, Lastname, Phone, Title, Status or AccountId is changed of the the contact during updation. It will still be working. But in case of insertion you have not told any of these conditions, rather these above written conditions can't be checked as the Contact is new.
Please check cthe code, that it matches with your requirement or not and let me know.
Thanks
Prosenjit
@Prosenjith:- Yes during update i need it to check if Email, Firstname, Lastname, Phone, Title, Status or AccountId is changed then fire.
But during insert i need to check if the RecordTypeId == '012D0000000BaFA'. that is the only condition.
Can that one condition be check in insert?
Many Thanks in advance.
Abraham
What I get from your comment
During Update you just have check weather
- Email, Firstname, Lastname, Phone, Title, Status or AccountId has changed.
- NOT for RecordType
And On insert you only check for RecordType
correct me if I am wrong anywhere.
Thanks!
Sorry if i have misrepresented, Let me clarify in more detail
During update:-(Existing Record)
Should check the record type id is 012D0000000BaFA and if any of the following fields are changed Email, Firstname, Lastname, Phone, Title, Status or Account it should fire to external callout
During Insert:-
Should check if record type id is 012D0000000BaFA and once record is created with fields Email, Firstname, Lastname, Phone, Title, Status and Account name which are all mandatory fields it should fire a callout sending the record details these fields Email, Firstname, Lastname, Phone, Title, Status Accountname, and SF ID to external callout as below line. Hope im clear let me know if any more inputs required.
Many Thanks
Abraham
Thanks
Prosenjit
Many Thanks shrikanth & fahad for ur help too
Regards
Abraham
There is this error coming in line 7
Apex trigger Contactcallout caused an unexpected exception, contact your administrator: Contactcallout: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Contactcallout: line 7, column 1
I guess its because of contact old = Trigger.oldMap.get(c.Id);
can you suggest a way around for new records
Please help. Many Thanks
Thanks
Abraham
Hi Abraham,
Because you are trying to access trigger.oldMap during insert, it will fail, Check below code it should work.
Regards
Abraham