You need to sign in to do that
Don't have an account?
Pranav Chitrans
Trigger on lookup relationship
I am having 2 scenario something like this :
1. I have created one custom field named "lookup realtion" on standard object Account.. I want to fire trigger in such a way that when ever new account is inserted with the same name contact is being created and the custom field on Account which i have created should get populated with the contact name. and
2. is on the account and contact I had created the custom field named "Checkbox" which is of checkbox type... and while insertion if account checkbox is checked the checkbox field on contact should also be get checked and later on when i uncheck the checkbox on account the contact field checkbox also be get unchecked. I had written the code which perfectly well for creating the contact when ever new account is being created.. but I am facing problem in implementin these two above mentioned logics.. plz edit my code and suggest me your innovative Ideas..
1. I have created one custom field named "lookup realtion" on standard object Account.. I want to fire trigger in such a way that when ever new account is inserted with the same name contact is being created and the custom field on Account which i have created should get populated with the contact name. and
2. is on the account and contact I had created the custom field named "Checkbox" which is of checkbox type... and while insertion if account checkbox is checked the checkbox field on contact should also be get checked and later on when i uncheck the checkbox on account the contact field checkbox also be get unchecked. I had written the code which perfectly well for creating the contact when ever new account is being created.. but I am facing problem in implementin these two above mentioned logics.. plz edit my code and suggest me your innovative Ideas..
public class CreateAccountContact { trigger CreateAccountContact on Account (after insert, after update){ if(Trigger.isInsert){ List<Contact> ct = new List <Contact>(); for(Account acc : trigger.new){ Contact c = new Contact(); c.LastName = acc.name; c.AccountId=acc.id; c.Fax=acc.Fax; c.MailingStreet=acc.BillingStreet; c.MailingCity=acc.BillingCity; c.MailingState=acc.BillingState; c.MailingPostalCode=acc.BillingPostalCode; c.MailingCountry=acc.BillingCountry; c.Phone=acc.Phone; ct.add(c); } insert ct; } } }
All Answers
Please try the below trigger and let me know if it works according to your need:
Account_checkbox__c is the checkbox in account object and Contact_checkbox__c is the checkbox in contact object.
Thanks,
JK
As per your requirement i have created a trigger for you.
Please find it below:
Please test your all cases and let me know if you have any issue in it.
Thanks,
Abhishek
# Abhishek Bansal,
thanks.. It works well for checkbox... but what about my 1st issue... I have created one custom field named "lookup realtion" on standard object Account.. I want to fire trigger in such a way that when ever new account is inserted with the same name contact is being created and the custom field on Account which i have created should get populated with the contact name
I have modified the trigger code according to both of your required cases.
Please change your trigger code with below code : Check all of your cases now and let me know if you have any issue.
Thanks,
Abhishek
Apex trigger*******
Apex Class***********
it does not give any error..... but it not working for the lookup field..no value is being inserted....
what i found is... I had seperately made the three methods... the last method updateCheckbox is correct because we have to perform the task after update also so we have to write diff method for this... but for creating contact and inserting value in lookup field we have to put both logics under one method.. becuase according to my code i had called first "CheckLookupRelation" which is for lookupfield and "CreateContactOnAccont" later on... and if this method is at second it will execute after the 1st method "CheckLookupRelation" executed... and to put value in account lookup we need Id... And we will not get any Id if it is before update.. I hope u are getting whta I am trying to say....
Noida uttarpradesh