Tuesday, 31 July 2012

Handling single quote in apex class

By using String.escapeSingleQuotes() method we handle single quotes in a string to inject query  as shown below

Eg:
==
squery='select id,name,Project_Number__c,Description__c,Project_Manager__c,Project_Director__c,Project_Manager__r.name,Project_Director__r.name from Milestone1_Project__c where ((name like\''+String.escapeSingleQuotes(searchString)+'%\' OR Project_Number__c like\''+String.escapeSingleQuotes(searchString)+'%\'))';

Handle single quotes within query in javascript

We are facing the problem with single quote characters in string query with filters.When single quotes are inluded in strings (either in filter conditions or searchstring) used in query we handle with replace function as shown below.
Eg: 


var projname=document.getElementById('pg:myForm:pb:newrecord:'+Rowindex+':pInput').value;
Here 'projname' is string with single quotes.
now  we want to use this in query as shown below

sforce.connection.sessionId = '{!$Api.Session_ID}';
 var result = sforce.connection.query("Select Id,Name From Milestone1_Project__c where name ="+"'"+projname.replace("\'","\\'")+"'");
 var records = result.getArray("records");

To retrieve values
if(records.length!=0){
            for(var i=0;i<records.length;i++){
                          alert(records[i].name); 
            }
}

Saturday, 28 July 2012

Rollup summary Using trigger(counting opportunity on account)

//To do rollup on Account with opportunity and display the sum of No of hours on related account
trigger OpportunityRollup on Opportunity (after delete, after insert, after update) {
        Set<Id> accountids=new Set<Id>();
        Map<id,Account> mapaccount=new Map<id,Account>();
        Map<id,List<opportunity>> mapopportunity=new Map<id,List<opportunity>>();
      if(Trigger.isInsert || Trigger.isUpdate){
                List<Opportunity> lstopp=[select id,name,No_of_Hours__c,accountid from opportunity where id in:Trigger.newMap.keyset()];
                for(Opportunity opp:lstopp){
                     accountids.add(opp.accountid);
                }
                List<Account>  acclst=[select id,name,No_of_opp__c,No_of_Hours__c,industry,(select accountid,id,No_of_Hours__c,name from opportunities) from account where id in:accountids];
                for(Account acc:acclst){
                    mapaccount.put(acc.id,acc);
                    mapopportunity.put(acc.id,acc.opportunities);
                 }
                AggregateResult i=[select sum(No_of_Hours__c) from opportunity where accountid in:mapaccount.keyset()];
                for(Id id:mapaccount.keyset()){
                        mapaccount.get(id).No_of_opp__c=0;
                        mapaccount.get(id).No_of_Hours__c=0;
                        mapaccount.get(id).No_of_opp__c=mapopportunity.get(id).size();
                        decimal total=0;
                        for(Opportunity opp:mapopportunity.get(id)) {
                            if(opp.No_of_Hours__c!=null)
                                //total+=opp.No_of_Hours__c;
                                mapaccount.get(id).No_of_Hours__c=mapaccount.get(id).No_of_Hours__c+opp.No_of_Hours__c;
                        }  
                        //mapaccount.get(id).No_of_Hours__c=total;
                }
               
                if(mapaccount!=null && mapaccount.size()>0){
                  try{
                        update mapaccount.values();
                  }
                  catch(Exception e){
                   
                  }
                }
      }
      if(Trigger.isDelete){
            List<Opportunity> lstopp=[select id,name,No_of_Hours__c,accountid from opportunity where id in:Trigger.oldMap.keyset()];
                for(Opportunity opp:lstopp){
                     accountids.add(opp.accountid);
                }
                List<Account>  acclst=[select id,name,No_of_opp__c,No_of_Hours__c,industry,(select accountid,id,No_of_Hours__c,name from opportunities) from account where id not in:accountids];
                for(Account acc:acclst){
                    mapaccount.put(acc.id,acc);
                    mapopportunity.put(acc.id,acc.opportunities);
                 }
                AggregateResult i=[select sum(No_of_Hours__c) from opportunity where accountid in:mapaccount.keyset()];
                for(Id id:mapaccount.keyset()){
                        mapaccount.get(id).No_of_opp__c=0;
                        mapaccount.get(id).No_of_Hours__c=0;
                        mapaccount.get(id).No_of_opp__c=mapopportunity.get(id).size();
                        decimal total=0;
                        for(Opportunity opp:mapopportunity.get(id)) {
                            if(opp.No_of_Hours__c!=null)
                                //total+=opp.No_of_Hours__c;
                                mapaccount.get(id).No_of_Hours__c=mapaccount.get(id).No_of_Hours__c+opp.No_of_Hours__c;
                        }  
                        //mapaccount.get(id).No_of_Hours__c=total;
                }
               
                if(mapaccount!=null && mapaccount.size()>0){
                  try{
                        update mapaccount.values();
                  }
                  catch(Exception e){
                   
                  }
                }
      }
       
}