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
Abraham kumar 4Abraham kumar 4 

Trigger to fire on update to send webservice notification

Hi All,

I am trying to wrie a trigger so that once any account name is edited It should get all the related contacts of that account and send a few fields of those related contacts  as a webservice method to another class, i just need a proper trigger for this Please help me write this trigger, im done some as below but no working
trigger Contactcallout2 on Account(after update) {
Map<Id, Account> mapaccount = new Map<Id, Account>();
list<Contact> ListContact = new list<Contact>();
set<ID> accIds = new set<ID>();
for(Account acct : trigger.new) {
        acctIds.add(acct.Id);
        mapAccount.put(acct.Id, acct);
        }
        if(Name!=old.Name){
    listContact = [SELECT c.id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,c.accName,c.status__c AccountId FROM Contact WHERE AccountId IN : acctIds AND c.recordtypeID = '012D0000000BaFA'];
    }
       if(listContact.size() > 0) {
        for(Contact con : listContact) {
         WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
        }
        }
        }

Thanks
Abraham
Best Answer chosen by Abraham kumar 4
KaranrajKaranraj
Try the below code
trigger Contactcallout2 on Account(after update) {
List<Contact> ListContact = new List<Contact>();
Set<ID> accIds = new set<ID>();
    for(Account acct : trigger.new) {
        if(Trigger.oldMap.get(acct.id).Name != acct.Name){
             acctIds.add(acct.Id);
        }
    }
    if(acctIds.size() > 0){
        for(Contact con : [SELECT id,Email,FirstName,LastName,phone,Title__c,Account.Name,status__c, AccountId FROM Contact WHERE AccountId IN : acctIds AND c.recordtypeID = '01q70000781hdAD']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
   
}

 

All Answers

KaranrajKaranraj
Try the below code
trigger Contactcallout2 on Account(after update) {
List<Contact> ListContact = new List<Contact>();
Set<ID> accIds = new set<ID>();
    for(Account acct : trigger.new) {
        if(Trigger.oldMap.get(acct.id).Name != acct.Name){
             acctIds.add(acct.Id);
        }
    }
    if(acctIds.size() > 0){
        for(Contact con : [SELECT id,Email,FirstName,LastName,phone,Title__c,Account.Name,status__c, AccountId FROM Contact WHERE AccountId IN : acctIds AND c.recordtypeID = '01q70000781hdAD']){
          WebServiceCallout.sendNotification(con.Id,con.Email,con.FirstName,con.LastName,con.phone,con.Title__c,con.Account.Name,con.status__c);
        }
    }
   
}

 
This was selected as the best answer
Abraham kumar 4Abraham kumar 4
Thank you soo much karanraj.. works without a glitch ..Amazing

Thanks
Abraham