Saturday 21 June 2014

Trigger on Contact Object!!

Hi Guys!!

How are you doing ? Its been very long I haven't written anything, well I just got time today . Its very hot out here at my place, What!! not because of weather but because of something else . Oh! Sorry , its a Technical blog . I should mind my language. let me stop all this Crap. Focus on Salesforce. Its growing at a rapid rate. need to catch up. :)


So , Guys again am here with a new scenario for trigger . This time we We will Write a trigger on Contact Object . I got this requirement on internet.

Scenario:-

I need a trigger with before events for the below scenario:
We have a Case object having a lookup relationship with opportunity as LX_Opportunity__c.
I wanted the below fields to get populated when case object is saved
LX_Sales_ORG__c on case object should be poulated with LX_Sales_Organization__c from opportunity
LX_Payment_Terms_Lead__c on case object should be poulated with Payment_Terms__c from opportunity.

So I guess it is looks very simple . Let me make it more simple. Lets plan the strategy n nail it.

1. We need to get the id of look up field on contact of opportunity.
2. Then we will querry the opprtunity field from that id.
3. We will then Iterate through that records. And will save those values in contact.


Dont Worry Here is the code

trigger FieldpopulatedFromOpportunity  on Case (Before insert, Before Update) {
    
    set<id> caseIds = new set<id>();
    list<case> opp= new List <case>();
    for(Case c: trigger.new){
        
//        c.AnkushTest__LX_Sales_ORG__c=
          caseIds.add(c.AnkushTest__LX_Opportunity__c);
          system.debug('===case'+c);
          system.debug('==='+caseIds);
    }
    Map<id,Opportunity>  mapopp = new Map<id,opportunity>([Select id, AnkushTest__LX_Sales_Organization__c from opportunity where Id in : caseIds]);
    //mapopp  =  [];
   // system.debug('===op'+op);
    //case c =[Select id from case where id in : caseIds];
    
    for(case cs : trigger.new){
    cs.AnkushTest__LX_Sales_ORG__c = mapopp.get(cs.AnkushTest__LX_Opportunity__c).AnkushTest__LX_Sales_Organization__c ;
    //insert cs;
    opp.add(cs);
    system.debug('===case2'+trigger.new);
    }
    
    
  
  
}



Just try it once . To be honest it took me around five hours to sove this. I hope you can do it too.

Happy coding!!
Cheers
ankushsalesforce@gmail.com




















Sunday 1 June 2014

Trigger On Custom Object!!!

Hi Guys!!

I was just going through some of the requirement on internet last night. So, thought that I should share it with you too. Note: It s for beginners like me , for those who have just made debut in salesforce. :p

Lets Say the scenario is I have a custom object name Friend and it has a field named as Company which is look up to on another object named as company. So if the Company field is empty i.e any of the friend is not associated with any company then it should be associated with company of its own name.
Like If my friend Ravi is not associated with any company then it should automatically get associated with company of his own namw i.e ravi.


trigger populateCompany on Friend__c (before insert,Before update) {
    list<Friend__c> lst = [Select id, name ,AnkushTest__Company__c ,AnkushTest__Pare__c from Friend__c ];
    system.debug('====='+lst);
    For(Friend__c  fi: trigger.new){
        if(fi.AnkushTest__Company__c == null){
            fi.AnkushTest__Pare__c = fi.Name;     
            system.debug('====='+lst);
        }
    }
    
}

So, here have a custom field named AnkushTest__Company__c  which is lookup to Company object.
So if this field is empty or Null then another field named  AnkushTest__Pare__c should be populated as same as Name field .  Important point here is I have used Before trigger because this whole transaction should occur before saving account.

























oAuth Use Case -  Headless API Explained in the Easiest way Wondering why? and When we use the Headless API flow Use case example: Imagine a...