Tuesday, January 27, 2015

Get the updated field Name in trigger

Trigger updateAccount on Account(after update){
 for(Account act:trigger.new){
  Account oldAccountId=Trigger.oldMap.get(act.Id);
  Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();
  for (String str : M.keyset()) {
   if(act.str != oldAccountId.str){
    system.debug('******The value has changed!!!! ');
    // here goes more code
   }
  }
 }
}

Sending Emails Through Silverpop

Prerequiste:
Need to get the Web Portal Registration List Id from silver pop

Execute the below code to send an email

String xmlBody='<?xml version="1.0" encoding="UTF-8"?><Envelope><Body><AddRecipient><LIST_ID>Web Portal Registration List Id </LIST_ID><CREATED_FROM>2</CREATED_FROM><UPDATE_IF_FOUND>true</UPDATE_IF_FOUND><SEND_AUTOREPLY>true</SEND_AUTOREPLY><ALLOW_HTML>false</ALLOW_HTML><COLUMN><NAME>EMAIL</NAME><VALUE>xxxxx@gmail.com</VALUE></COLUMN><COLUMN><NAME>GUID</NAME><VALUE>d8a4ba8c-6fd0-acdb-1953-1726d797cb57</VALUE></COLUMN><COLUMN><NAME>FIRSTNAME</NAME><VALUE>xxx</VALUE></COLUMN><COLUMN><NAME>LASTNAME</NAME><VALUE>aaa</VALUE></COLUMN></AddRecipient></Body></Envelope>';

Http httpObj = new Http();
HttpRequest httpReq = new HttpRequest();       
httpReq.setEndpoint('http://api2.silverpop.com/XMLAPI');
httpReq.setMethod('POST');
httpReq.setHeader('Content-Type', 'text/xml;charset=utf-8');
httpReq.setBody(xmlBody);
HttpResponse httpResp = new HttpResponse();
try {
 httpResp = httpObj.send(httpReq);
 System.debug('Email Sent successfully');
} catch(Exception e) {
 System.debug('MEssage' + e.getMessage());
 System.debug('sendEmail : ' + e.getStackTraceString());
}

Wednesday, January 7, 2015

reference old or prior field value in apex trigger

To reference the old value of a field in a trigger, use the following syntax: 

Trigger.oldMap.get(account.Id).fieldName 

So to ensure a field was changed during an update trigger you will have: 

if (Trigger.isUpdate) { 
if (account.fieldname != Trigger.oldMap.get(account.Id).fieldName) { 
// Field has been changed! 
} 
}