Salesforce Certified Application Architect

Thursday 26 June 2014

Be aware of the following considerations for trigger context variables:

  • trigger.new and trigger.old cannot be used in Apex DML operations.

  • You can use an object to change its own field values using trigger.new, but only in before triggers. In all after triggers, trigger.new is not saved, so a run time exception is thrown.

  • trigger.old is always read-only.

  • You cannot delete trigger.new.


Cheers...

How to Insert Transaction and Update Member on button click.

global class InsertTransaction {

WebService static string  InsertRecord(string id) {

    String OppId=id;
    string ErrorMesg;
    Map<ID,Member__c> mapmembers = new Map<ID,Member__c>();
    List<ID> MemberIDs =new List<ID>();
    List<ID> ProgramEnrIds =new List<ID>();
    List<Transaction__c> listTrans=new List<Transaction__c>();
    List<Member__c> lopp1=[select id ,Active__c,Program_Enrolled__c,Enrolled_Date__c,
    Loyalty_Enrolled__c,Points__c from Member__c where id =:OppId limit 1];
    integer counIn=[select count() from Transaction__c where Member__c =:OppId ];
    set<string> setPromId = new set<string>();
    List<Member__c> Members =new List<Member__c>();
    if(counIn > 0) {
    ErrorMesg ='You can not create a Transaction because this member having the Transaction ';
    }

    else {
    for(Member__c m:lopp1){
    Transaction__c tObj=new Transaction__c(Transaction_Date__c=system.today());
    tObj.Member__c=m.id;
    tObj.ProgramEnrolled__c=m.Program_Enrolled__c;
    tObj.Sub_Type__c='Enrollment';
    tObj.Status__c ='Processed';
    tObj.Loyalty_Product__c='a0590000008at2y';
    listTrans.add(tObj);
    }
 
    for(Transaction__c tObj1:listTrans){
    ProgramEnrIds.add(tObj1.ProgramEnrolled__c);
    MemberIDs.add(tObj1.Member__c);
 
    }
 
 list<Promotion__c > lisProm=[select id,Active__c,Program_Enrolled__c,Apply_To__c from                  Promotion__c where Program_Enrolled__c In : ProgramEnrIds];
    System.debug('lisProm---->'+lisProm.size());
    for(Promotion__c obj:lisProm){
    setPromId.add(obj.id);

    }
    list<Rule_Action__c>ListObj=[Select PointsAssigned__c,        id,Number_of_Points__c,Promotion__c,Promotion__r.Apply_To__c,
 Object__c from Rule_Action__c where Promotion__c in:setPromId];
    System.debug('ListObj---->'+ListObj.size());
 
    Members =[Select Points__c FROM Member__c Where ID in:MemberIDs];
    System.debug('Members ---->+Members )');
 
    for(Member__c m:Members){
    mapmembers.put(m.id,m);
    System.debug('mapmembers---->'+mapmembers.size() );
    }
 
    for(Transaction__c tn:listTrans){
    Member__c Member = mapmembers.get(tn.Member__c);
    System.debug('Member ---->'+Member);
 
    for(Rule_Action__c raObj:ListObj){
    if(raObj.Promotion__r.Apply_To__c == 'Enrollment'){
    Member.Points__c=raObj.Number_of_Points__c;
    System.debug('Member Points---->'+Member.Points__c );
    System.debug('raObj Points---->'+raObj.Number_of_Points__c);
    }
     mapmembers.put(Member.id,Member);
    }
    }
 
    if(listTrans.size()>0){
    Insert listTrans;
    System.debug('mapmembers---->'+mapmembers.size());
    }
 
    update mapmembers.values();
 
    ErrorMesg='Transction created succesfully';
 
    }
      return ErrorMesg;
   }

 }

Monday 9 June 2014

How to schedule Batch Class for every hour in Salesforce?

Sample code:

SampleSchedulableClass obj = new SampleSchedulableClass();

String cron = '0 59 * * * *'
System.schedule('Testing', cron, obj);


Expression
Description
0 0 13 * * ?
Class runs every day at 1 PM.
0 0 22 ? * 6L
Class runs the last Friday of every month at 10 PM.
0 0 10 ? * MON-FRI
Class runs Monday through Friday at 10 AM.
0 0 20 * * ? 2010
Class runs every day at 8 PM during the year 2010.

Cheers!!

System.schedule in Apex scheduler in Salsforce

Sample code:

SampleSchedulableClass obj = new SampleSchedulableClass();

String cron = '0 59 * * * *'
System.schedule('Testing', cron, obj);

The above code executes SampleSchedulableClass for every one hour.

Syntax for Cron:

Seconds Minutes Hours Day Month Week Year

Seconds : 0 - 59
Minutes  : 0 - 59
Hours     : 0 - 23
Day        : 1 - 31
Month    : 1 - 12
Week     : 1 - 7(Sunday is first and Saturday is last)
Year       : upto 2099

Year is optional.

Expression
Description
0 0 13 * * ?
Class runs every day at 1 PM.
0 0 22 ? * 6L
Class runs the last Friday of every month at 10 PM.
0 0 10 ? * MON-FRI
Class runs Monday through Friday at 10 AM.
0 0 20 * * ? 2010
Class runs every day at 8 PM during the year 2010.

? - No value
* - All values
L - Last
W - Nearest weekday


How to update a field from one to another object on button click.

global class TransactionProcessingUtility_1  {

WebService static void processTransaction (id transactionids) {

List<Transaction__c> TransactionsToUpdate =new List<Transaction__c>();
List<ID> MemberIDs =new List<ID>();
List<ID> ProductIDs =new List<ID>();
List<ID> ProgramEnrIds =new List<ID>();
List<Member__c> Members =new List<Member__c>();
List<LoyaltyProduct__c> Products =new List<LoyaltyProduct__c>();
Map<ID,Member__c>   mapmembers = new Map<ID,Member__c>();
Map<ID,LoyaltyProduct__c>   mapproducts = new Map<ID,LoyaltyProduct__c>();  
Map<ID,Transaction__c>   maptransactions = new Map<ID,Transaction__c>();
List<String> transfilter = new List<String>{'Processed','Cancelled','Failed'};  

if(transactionids != null) {
List<Transaction__c> listofTransactions =[SELECT ID,
Member__c,
Processing_Comment__c,
Loyalty_Product__c,
Status__c,
Sub_Type__c,ProgramEnrolled__c,
Type__c,
Transaction_Date__c
FROM Transaction__c WHERE id =:transactionids AND
Status__c NOT IN :transfilter];

for(Transaction__c t:listofTransactions){
MemberIDs.add(t.Member__c);
ProductIDs.add(t.Loyalty_Product__c);
maptransactions.put(t.id, t);
ProgramEnrIds.add(t.ProgramEnrolled__c);

}

set<string> setPromId = new set<string>();
Map<string,Rule_Action__c >  updateMap = new Map<string,Rule_Action__c >();
list<Promotion__c > lisProm=[select id,Active__c,Program_Enrolled__c,Apply_To__c from Promotion__c where Program_Enrolled__c In : ProgramEnrIds];

for(Promotion__c obj:lisProm){
setPromId.add(obj.id);

}

list<Rule_Action__c>ListObj=[Select id,Number_of_Points__c,Promotion__c,Promotion__r.Apply_To__c, Object__c from Rule_Action__c where Promotion__c in:setPromId];
Members  =[Select Points__c FROM Member__c Where ID in:MemberIDs];
Products =[Select Product_points__c FROM LoyaltyProduct__c Where ID in:ProductIDs];

for(Member__c m:Members){
mapmembers.put(m.id,m);

}

for(LoyaltyProduct__c p:Products){
mapproducts.put(p.id,p);

}

for(Transaction__c t:listofTransactions){
Member__c Member = mapmembers.get(t.Member__c);
LoyaltyProduct__c product = mapproducts.get(t.Loyalty_Product__c);
for(Rule_Action__c raObj:ListObj)
{

if(t.Type__c =='Accrual' && t.Sub_Type__c =='Product' && raObj.Promotion__r.Apply_To__c == 'Accrual-Product' ){
Member.Points__c=Member.Points__c+raObj.Number_of_Points__c;
t.Status__c ='Processed';
t.Processing_Comment__c ='Points added to member';
}

else if( t.Type__c =='Redemption' && t.Sub_Type__c =='Product' && raObj.Promotion__r.Apply_To__c == 'Redemption-Product' ) {
if( raObj.Number_of_Points__c < Member.Points__c  )
{

Member.Points__c=Member.Points__c - raObj.Number_of_Points__c;
t.Status__c ='Processed';
t.Processing_Comment__c ='Points redeemed from member';

}
else if(raObj.Number_of_Points__c > Member.Points__c ) {
t.Status__c ='Failed';
t.Processing_Comment__c ='Not enough points to redeem';

}
}
maptransactions.put(t.id,t);

}
}
update mapmembers.values();
update maptransactions.values();

}
}

}
=============================================================
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
    {!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}
   
     var result = sforce.apex.execute(
    "TransactionProcessingUtility_1",  //class name  
    "processTransaction",  //method name      
    { transactionids: '{!Transaction__c.Id}'    // method parameter
     });
window.alert("Are you sure want to proceed!!" );
window.location.reload(true);

Salesforce Certified Application Architect & Certified Data Architecture and Management Designer Exam

How to pass Salesforce Certified Data Architecture and Management Designer Exam This exam was 1st architect exam for me. Its not that muc...