+ Start a Discussion
Abraham kumar 4Abraham 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!!
 
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
Best Answer chosen by Abraham kumar 4
Fahad-AkhtarFahad-Akhtar

Hi Abraham,
Because you are trying to access trigger.oldMap during insert, it will fail, Check below code it should work.
 
//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);
    }
}

 

All Answers

Fahad-AkhtarFahad-Akhtar
Hi Abraham,
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
Shrikant BagalShrikant Bagal
Hello Abraham,


Please try following code:
 
trigger Contactcallout on Contact (after insert, 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);
            
    
    }
}

Hope its help you.

Please marks as solution if it solve your issue, so it will help people who will face same issue.

Thanks!
 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Abraham,
I have modified your codeso that it can work on both inser and update.
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'){
        contact old = Trigger.oldMap.get(c.Id);
         if (Trigger.isUpdate && (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 if(Trigger.isInsert) {
			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);
            
    
    }
}

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
Abraham kumar 4Abraham kumar 4
Thank you Fahad and shrikank and prosenjith for your quick help in this 

@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
 
Shrikant BagalShrikant Bagal
Hello 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!
 
Abraham kumar 4Abraham kumar 4
Hi Shrikant and Prosenjith,

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.
WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,
Hope im clear let me know if any more inputs required.

Many Thanks
Abraham

 
Prosenjit Sarkar 7Prosenjit Sarkar 7
In that case use this,
 
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'){
        contact old = Trigger.oldMap.get(c.Id);
         if ((Trigger.isUpdate && (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)) || (Trigger.isInsert && c.Email != NULL && c.FirstName!= NULL && c.LastName!= NULL && c.phone!= NULL && c.Title__c!= NULL && c.status__c!= NULL && c.AccountID!= NULL ))
         {
             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);
            
    
    }
}

Thanks 
Prosenjit
Abraham kumar 4Abraham kumar 4
Yes, Exactly what i wanted... Amazing ..Thank you soo much Prosenjit..

Many Thanks shrikanth & fahad for ur help too

Regards
Abraham
Prosenjit Sarkar 7Prosenjit Sarkar 7
you are most welcome @Abraham
Abraham kumar 4Abraham kumar 4
Hi Prosenjit,
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
Fahad-AkhtarFahad-Akhtar

Hi Abraham,
Because you are trying to access trigger.oldMap during insert, it will fail, Check below code it should work.
 
//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);
    }
}

 
This was selected as the best answer
Abraham kumar 4Abraham kumar 4
Amazing ... Thank you soo much Fahad.. Works Fantastic..

Regards
Abraham