You need to sign in to do that
Don't have an account?
Ruchi Gupta Keshri
how to write trigger for this condition can any one help me. Check if contact exists for the email id and if not then create a contact record respective to the email id and populate the id to the A object contact field.
Create fields on Object A :- ObjectA
Request Status (Picklist) :- New, Approved, Duplicate, Not Matched, Rejected, Completed, Match Found.
Is Auto Approved (Checkbox)
Email
Roles (Multi-Select Picklist) :- Admin, Developer, Manager, CEO, Executive.
When a record is created for the first time request status is new. Also Check roles and email existence,
if already exist then request status is Duplicate.
Check if contact exists for the email id and if not then create a contact record respective to the email id
and populate the id to the A object contact field.
If the Request Status is Completed for the record created , then for another new record (with same email)
the Request Status should be Approved and Checkbox true.
i write trigger for above 1st condition.....can any one tell how to write for 2nd and 3rd point. with best practice....
trigger ObjectATrigger on ObjectA__c (before insert,after update) {
list<String> email= new List<string>();
list<string> roles= new List<string>();
list<ObjectA_c> objData= [Select id,Emailc,Rolesc,Request_Statusc from ObjectA_c limit 10];
if(trigger.isbefore && trigger.isinsert){
for(ObjectA__c obj:objData){
email.add(obj.Email__c);
roles.add(obj.Roles__c);
}
for(ObjectA__c objcheck: trigger.new){
if(email.contains(objcheck.Email_c) || roles.contains(objcheck.Roles_c)){
objcheck.Request_Status__c='Duplicate';
}else
objcheck.Request_Status__c='New';
}
}
if(trigger.isafter && trigger.isupdate){
list<ObjectA_c> objNewRecord= new List<ObjectA_c>();
for(ObjectA__c objupdate: trigger.old){
system.debug('objupdate'+objupdate);
if(objupdate.Request_Status__c == 'Completed'){
ObjectA_c newobj= new ObjectA_c();
newobj.Request_Status__c='Approved';
newobj.Name=objupdate.Name;
newobj.Is_Auto_Approved__c=true;
newobj.Email_c= objupdate.Email_c;
objNewRecord.add(newobj);
system.debug('objNewRecord'+objNewRecord);
}
insert objNewRecord;
}
}
}
Request Status (Picklist) :- New, Approved, Duplicate, Not Matched, Rejected, Completed, Match Found.
Is Auto Approved (Checkbox)
Roles (Multi-Select Picklist) :- Admin, Developer, Manager, CEO, Executive.
When a record is created for the first time request status is new. Also Check roles and email existence,
if already exist then request status is Duplicate.
Check if contact exists for the email id and if not then create a contact record respective to the email id
and populate the id to the A object contact field.
If the Request Status is Completed for the record created , then for another new record (with same email)
the Request Status should be Approved and Checkbox true.
i write trigger for above 1st condition.....can any one tell how to write for 2nd and 3rd point. with best practice....
trigger ObjectATrigger on ObjectA__c (before insert,after update) {
list<String> email= new List<string>();
list<string> roles= new List<string>();
list<ObjectA_c> objData= [Select id,Emailc,Rolesc,Request_Statusc from ObjectA_c limit 10];
if(trigger.isbefore && trigger.isinsert){
for(ObjectA__c obj:objData){
email.add(obj.Email__c);
roles.add(obj.Roles__c);
}
for(ObjectA__c objcheck: trigger.new){
if(email.contains(objcheck.Email_c) || roles.contains(objcheck.Roles_c)){
objcheck.Request_Status__c='Duplicate';
}else
objcheck.Request_Status__c='New';
}
}
if(trigger.isafter && trigger.isupdate){
list<ObjectA_c> objNewRecord= new List<ObjectA_c>();
for(ObjectA__c objupdate: trigger.old){
system.debug('objupdate'+objupdate);
if(objupdate.Request_Status__c == 'Completed'){
ObjectA_c newobj= new ObjectA_c();
newobj.Request_Status__c='Approved';
newobj.Name=objupdate.Name;
newobj.Is_Auto_Approved__c=true;
newobj.Email_c= objupdate.Email_c;
objNewRecord.add(newobj);
system.debug('objNewRecord'+objNewRecord);
}
insert objNewRecord;
}
}
}
try with below code.
If this helps, Please mark it as best answer.
Thanks!!
All Answers
try with below code.
If this helps, Please mark it as best answer.
Thanks!!
Thank you so much for the 2nd point condition is working fine now
but in 1st condition objcheck.Request_Status__c='Duplicate'; not working.
and 3rd condition is also not working can you please help me with 3rd codition(If the Request Status is Completed for the record created , then for another new record (with same email) the Request Status should be Approved and Checkbox true. ) not able to create new record when newobj.Request_Status__c='Approved';
Can you check, if you have any duplicate ObjectA__c records with email/Roles fields vaules or not.
3rd conditon:
Update the ObjectA__c record with Request_Status__c= completed and check whether new record is created or not?
If not, keep the system.debug stamenets and figure out the issue.
Thanks!!