You need to sign in to do that
Don't have an account?
HTANIRS
Trigger to update after inserting in the same object
Hi,
I have a the requirement: I need to create a Custom Object with a relationship to Contact. I need to create 5 checkbox in Contacts and same in custom object. Now when I enter the values in the checkboxes in contact I need to insert a new record in custom object with the checkbox values. After inserting the record in cusgom object I need to make the contact checkbox to unselsect if 2 checkbox is selected. How can I do this. I am done with insert and update trigger. Below is my Trigger for reference. Please give me your suggestions.
trigger projInsert on Contact (after insert, after update) {
System.debug('---- Inside Contact :----');
if(trigger.isInsert) {
List<Project__c> projList = new List<Project__c>();
for(Contact con : Trigger.new) {
System.debug('---- Inside For Loop : ----');
if (con.LastName != '' && con.LastName != null) {
System.debug('---- Inside IF Loop : ----');
Project__c proj = new Project__c();
proj.Contact__c = con.id;
proj.Feature_1__c = con.Feature_1__c;
proj.Feature_2__c = con.Feature_2__c;
if(con.Product_A__c == true) {
proj.Products__c = 'Product A';
}
else if (con.Product_B__c == true) {
proj.Products__c = 'Product B';
}
else {
proj.Products__c = '';
}
projList.add(proj);
}
System.debug('---- Proj List ----');
System.debug('---- Proj List After If : ----'+projList);
}
if(projList.size() > 0) {
insert projList;
}
System.debug('---- Proj List : ----');
System.debug('---- Proj List Size : ----' +projList.size());
}
if(trigger.isUpdate) {
Map <Id, Contact> mapContact = new Map <Id, Contact>();
List<Project__c> listProject = new List<Project__c>();
for(Contact cont : trigger.new)
mapContact.put(cont.Id, cont);
listProject = [SELECT id, Contact__c, Feature_1__c, Feature_2__c, Products__c
FROM Project__c
WHERE Contact__c IN : mapContact.keySet()];
if (listProject.size() > 0) {
for (Project__c pro : listProject) {
pro.Feature_1__c = mapContact.get(pro.Contact__c).Feature_1__c;
pro.Feature_2__c = mapContact.get(pro.Contact__c).Feature_2__c;
}
update listProject;
}
}
}
Thanks
I have a the requirement: I need to create a Custom Object with a relationship to Contact. I need to create 5 checkbox in Contacts and same in custom object. Now when I enter the values in the checkboxes in contact I need to insert a new record in custom object with the checkbox values. After inserting the record in cusgom object I need to make the contact checkbox to unselsect if 2 checkbox is selected. How can I do this. I am done with insert and update trigger. Below is my Trigger for reference. Please give me your suggestions.
trigger projInsert on Contact (after insert, after update) {
System.debug('---- Inside Contact :----');
if(trigger.isInsert) {
List<Project__c> projList = new List<Project__c>();
for(Contact con : Trigger.new) {
System.debug('---- Inside For Loop : ----');
if (con.LastName != '' && con.LastName != null) {
System.debug('---- Inside IF Loop : ----');
Project__c proj = new Project__c();
proj.Contact__c = con.id;
proj.Feature_1__c = con.Feature_1__c;
proj.Feature_2__c = con.Feature_2__c;
if(con.Product_A__c == true) {
proj.Products__c = 'Product A';
}
else if (con.Product_B__c == true) {
proj.Products__c = 'Product B';
}
else {
proj.Products__c = '';
}
projList.add(proj);
}
System.debug('---- Proj List ----');
System.debug('---- Proj List After If : ----'+projList);
}
if(projList.size() > 0) {
insert projList;
}
System.debug('---- Proj List : ----');
System.debug('---- Proj List Size : ----' +projList.size());
}
if(trigger.isUpdate) {
Map <Id, Contact> mapContact = new Map <Id, Contact>();
List<Project__c> listProject = new List<Project__c>();
for(Contact cont : trigger.new)
mapContact.put(cont.Id, cont);
listProject = [SELECT id, Contact__c, Feature_1__c, Feature_2__c, Products__c
FROM Project__c
WHERE Contact__c IN : mapContact.keySet()];
if (listProject.size() > 0) {
for (Project__c pro : listProject) {
pro.Feature_1__c = mapContact.get(pro.Contact__c).Feature_1__c;
pro.Feature_2__c = mapContact.get(pro.Contact__c).Feature_2__c;
}
update listProject;
}
}
}
Thanks
Try this Below Trigger, For your update functionality --
Hope it will help you
Thanks
Akshay
All Answers
Try this below Trigger --
Just make modification as per your need, if you are feeling any difficulties --
Object and Fields Names are not same as per your given condition, so make changes According to your Object and Field Names --
If found this answer helpful , Do mark as best answer so that it helps other also
Thanks
Akshay
Thanks for you reply. This is working and I need small change. How can I use the same logic in update. My requirement is when click on checkbox 1 in edit. Need to uncheck it after it updated in custom object.
Below Code I modified for Update but it is not working. Can you please suggest how can it done.
if(trigger.isUpdate) {
Map <Id, Contact> mapContact = new Map <Id, Contact>();
List<Project__c> listProject = new List<Project__c>();
Integer count = 0;
for(Contact con : trigger.new) {
mapContact.put(con.Id, con);
if(con.Product_A__c == true) {
CheckboxHoldingValue.FirstCheckbox = con.Product_A__c;
count++;
}
if(con.Feature_1__c == true) {
CheckboxHoldingValue.SecondCheckbox = con.Feature_1__c;
count++;
}
if(con.Feature_2__c == true) {
CheckboxHoldingValue.ThirdCheckbox = con.Feature_2__c;
count++;
}
if(count >= 1 ) {
con.Product_A__c = false;
con.Feature_1__c = false;
con.Feature_2__c = false;
}
}
listProject = [SELECT id, Contact__c, Feature_1__c, Feature_2__c, Products__c
FROM Project__c
WHERE Contact__c IN : mapContact.keySet()];
if (listProject.size() > 0) {
for (Project__c pro : listProject) {
pro.Feature_1__c = mapContact.get(pro.Contact__c).Feature_1__c;
pro.Feature_2__c = mapContact.get(pro.Contact__c).Feature_2__c;
}
update listProject;
}
}
Thanks
Try this Below Trigger, For your update functionality --
Hope it will help you
Thanks
Akshay
Thanks for your Help. My issue got solved and it is working.
Thanks.
How can I write Test Class for this Trigger. Please help in assisting.
Thanks.