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




















No comments:

Post a Comment

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...