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.

























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