You need to sign in to do that
Don't have an account?
Manid
i tried to write the code for below scenario but if not works properly
Create a new Custom objects Application and BlackList
Object Name Field Names
-------------------- -----------------
Application Name,Pancard ,Phone
BlacKList Name,Pancard,phone
a. When ever we are inserting new Application it has to check pancard no of the new application record is
in the Blakc list or not .
b.If the pancard of the Appliction is in the blacklist object then update the blackList phone with new application phone no and throw error
below code is throws error as per scenario but it not update the phone number in the object please help any one
Object Name Field Names
-------------------- -----------------
Application Name,Pancard ,Phone
BlacKList Name,Pancard,phone
a. When ever we are inserting new Application it has to check pancard no of the new application record is
in the Blakc list or not .
b.If the pancard of the Appliction is in the blacklist object then update the blackList phone with new application phone no and throw error
below code is throws error as per scenario but it not update the phone number in the object please help any one
trigger Application on Applications__c (before insert) { list<blacklist__c> balack=[select name,pancard__c,mobile_num__c from blacklist__c]; list<blacklist__c> newre=new list<blacklist__c>(); for(Applications__c c: trigger.new){ for(blacklist__c bp:balack){ if(bp.pancard__c==c.Pancard__c){ bp.mobile_num__c=c.mobile_num__c; newre.add(bp); c.name.adderror('applicant is in blacklist'); } } update newre; } }
The reason your code is not working is : you are adding adderror() before updating records. adderror() method blocks your further execution basically adderror() is exit point of your code.
Try Below code:
Please mark as best answer if helped for others help.
Regards,
Ramakant
Such a scenrio is not possible. You can't throw an error and also update the record in the same transaction. Why would you like to update the record if already that Pancard is blacklisted.
Rather than updating the record, you can just throw an error on the application record.
Please let me know if you need more assistance. Or else please elaborate your requirement so that i can help you further.
Thanks,
Jainam Contractor,
Salesforce Consultant
Varasi LLC
www.varasi.com
you won't be able to achieve this in salesforce. Because due addError() in your requirement other transaction will be rolled back.
This is my analysis after writing trigger and Future method associated with it as below.
Regards,
Ragards
trigger Trigger_PANBlacklist_SM on Student_Master__c (before insert, before update)
{
list<Black_Listed_Candidate__c> blackList =[SELECT Name,PAN__c,Phone__c FROM Black_Listed_Candidate__c];
list<Student_Master__c> newre = new list<Student_Master__c>();
Map<Id, string> IdAndBlackListPhoneMap = New Map<Id, string>();
for(Student_Master__c varStu: trigger.new)
{
for(Black_Listed_Candidate__c varBL:blackList)
{
if(varBL.PAN__c==varStu.PAN__c)
{
varBL.Phone__c=varStu.Phone__c;
IdAndBlackListPhoneMap.put(varBL.Id, varBL.Phone__c);
newre.add(varStu);
}
}
if(newre.size() > 0)
{
Class_PANBlacklist_SM.updateBlackListPhone(IdAndBlackListPhoneMap);
for(Student_Master__c newAppRecord : newre)
{
newAppRecord.Background_Check_Status__c='Candidate is Blacklisted';
}
}
}
}
APEX CODE
public class Class_PANBlacklist_SM
{
@future
public static void updateBlackListPhone(Map<Id, string> idAndPhoneMap)
{
list<Black_Listed_Candidate__c> blackListedPans = [SELECT Name,PAN__c,Phone__c FROM Black_Listed_Candidate__c WHERE Id IN:idAndPhoneMap.KeySet()];
for(Black_Listed_Candidate__c black :blackListedPans)
{
black.Phone__c = idAndPhoneMap.get(black.Id);
}
Update blackListedPans;
}
}
your code is showing error. Condition : in blacklist candidate already details are there. but if we insert then only it will change.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger PancardCheckClassTrigger caused an unexpected exception, contact your administrator: PancardCheckClassTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.PancardCheckClassTrigger: line 23, column 1
Try this for the first part worked for me .
trigger Application on Application__c (before insert) {
if(trigger.isbefore){
if(trigger.isinsert){
//When ever we are inserting new Application it has to check pancard no of the new application record is in the Blakclist or not .
list<BlackList__c> Blacklist = [SELECT id, PanCard__c ,Phone__c FROM BlackList__c];
for(Application__c app : Trigger.new){
for(BlackList__c bla : Blacklist){
if(app.PanCard__c == bla.PanCard__c){
app.adderror('Cannot insert blacklist pancard details');
}
}
}
}
}
}