+ Start a Discussion
Maf_007Maf_007 

Apex Trigger to update two objects

Hi Guys,

I am relatively new in Apex trigger. I have a custom object named Lead source with lead name, domain name and lead reference (lookup) field. These fields also exist in Lead Object. Now My trigger on lead should do the following:

 

1. Check if a record already exist in Lead source. if it does update the reference field on Lead with Lead source reference.

2. If it does not exist create a new record in lead source also update the Lead reference with the id of record to be saved while saving.

 

Any help will be appreciated....

Best Answer chosen by Admin (Salesforce Developers) 
Maf_007Maf_007

Hi guys,

I have solved the problem myself. If anyone needs help in future, my code is below:

 

public class LeadSourceFetcher{
//Function to carry out the operation when trigger fires
public static void LeadSourceFetch(List<Lead> leadsourcetrigger){
List<Lead_Source__c> temp = [Select id, where_hear_about, Domain__c,
Search_Cookie_source__c,Lead_Source__c from Lead_Source__c];
List<Lead_Source__c> newrecord = new List<Lead_Source__c>{};

//fetch the permission for users from user object
boolean ispermitted = [select Lead_Source_Trigger__c from User where id = :UserInfo.getUserId()].Lead_Source_Trigger__c;
//List<User> ispermitted = [Select id from User where Lead_Source_Trigger__c = true];
boolean flag = false;

//Trigger to on Lead object for given Criteria
for(Lead l:leadsourcetrigger){
//Iterate through the records for any match on required fields
for(Lead_Source__c ls:temp){
//if there is a match found with lead source object fire this condition
if((ls.where_hear_about_ == l.where_hear_about)&&
(ls.Domain__c == l.Domain__c)&&
(ls.Search_Cookie_source__c == l.Search_Cookie_source__c)){

//populate leadsource field of Lead object with leadsource field of Lead Source object
l.LeadSource = ls.Lead_Source__c;
l.lead_source_reference__c = ls.id;

//lupdate.add(l);
flag = true;
break;
}
}
//if there is no match found create a new record
if(!flag) {
//creating a new record on Lead Source and adding in object
newrecord.add(new Lead_Source__c(
where_hear_about = l.where_hear_about,
Domain__c = l.Domain__c,
Search_Cookie_source__c = l.Search_Cookie_source__c));
flag = true;
break;
}
}
//insert new record when there's no match
if(ispermitted){
insert newrecord;
}
//update lupdate;
system.debug('yes');
}

All Answers

Dr. WhoDr. Who
I will get you started.
1. Learn how to program before accepting a job that requires programming.

Regards, Dr. Who
k_bentsenk_bentsen

There is no need for a comment like that, Dr. Who. Just because he is new to Apex, don't assume he has no programming knowledge. 

 

Maf, if you do understand the basics of programming, Apex follows very similar syntax to Java and C++, so I think you should be able to look at some sample trigger code and get an idea of what is going on. This is a great site that really helped me get going on learning Apex: http://wiki.developerforce.com/page/Apex_Code_Best_Practices Some of the sample triggers on this page have very good comments that explain what is going on if you don't quite understand the code. See if this is enough to get you started writing a few lines of code, then post what you have and I'll do my best to help you.

Maf_007Maf_007
I think u misunderstood my question. I was not asking for the solution code. I was just looking for ideas. I have got a big trigger which is partially doing the job but there are something I am missing. now just wanted to see how other people approach this problem since there are many ways of solving a problem. did not want post the because that would make the post messy and would not be readable to some readers. thanks for getting me started btw DR WHO.....
Maf_007Maf_007
Thanks bentsen. The bit I am struggling at is can i assign the future ID of a record to a custom field since the user still has not pressed saved. Or I have to do it as update on a different trigger?
Maf_007Maf_007

Hi guys,

I have solved the problem myself. If anyone needs help in future, my code is below:

 

public class LeadSourceFetcher{
//Function to carry out the operation when trigger fires
public static void LeadSourceFetch(List<Lead> leadsourcetrigger){
List<Lead_Source__c> temp = [Select id, where_hear_about, Domain__c,
Search_Cookie_source__c,Lead_Source__c from Lead_Source__c];
List<Lead_Source__c> newrecord = new List<Lead_Source__c>{};

//fetch the permission for users from user object
boolean ispermitted = [select Lead_Source_Trigger__c from User where id = :UserInfo.getUserId()].Lead_Source_Trigger__c;
//List<User> ispermitted = [Select id from User where Lead_Source_Trigger__c = true];
boolean flag = false;

//Trigger to on Lead object for given Criteria
for(Lead l:leadsourcetrigger){
//Iterate through the records for any match on required fields
for(Lead_Source__c ls:temp){
//if there is a match found with lead source object fire this condition
if((ls.where_hear_about_ == l.where_hear_about)&&
(ls.Domain__c == l.Domain__c)&&
(ls.Search_Cookie_source__c == l.Search_Cookie_source__c)){

//populate leadsource field of Lead object with leadsource field of Lead Source object
l.LeadSource = ls.Lead_Source__c;
l.lead_source_reference__c = ls.id;

//lupdate.add(l);
flag = true;
break;
}
}
//if there is no match found create a new record
if(!flag) {
//creating a new record on Lead Source and adding in object
newrecord.add(new Lead_Source__c(
where_hear_about = l.where_hear_about,
Domain__c = l.Domain__c,
Search_Cookie_source__c = l.Search_Cookie_source__c));
flag = true;
break;
}
}
//insert new record when there's no match
if(ispermitted){
insert newrecord;
}
//update lupdate;
system.debug('yes');
}

This was selected as the best answer
thisisnotaprilthisisnotapril

Dr Who, surely at some point in your career you've started a job and found the roles and responsibilites of that job changed over time, and you may have even found that you had to learn some new skills as a result. That someone turns to the community here for help in that learning path is awesome.