function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ManidManid 

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
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;
    }
}

 
RKSalesforceRKSalesforce
Hi Manid,

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:
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);                
            }
        }
        if(newre.size() > 0){
			update newre;
			for(blacklist__c blRecord : newre){
				blRecord.Name.adderror('applicant is in blacklist');
			}
		}
    }
}

Please mark as best answer if helped for others help.

Regards,
Ramakant 
 
Jainam ContractorJainam Contractor
Hi Manid,

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
ManidManid
thank you for response. ramakant code didn't work properly . my requirement is if pancard is in blocklisted update the phone number in blacklist object and throw error in the aplication object .
RKSalesforceRKSalesforce
Hi Manid,

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.
TRIGGER:

trigger ApplicationTrigger on Application__c (before insert) {
    
    list<blacklist__c> balack = [select name,pancard__c,mobile_num__c from blacklist__c];
    list<Application__c> newre = new list<Application__c>();
	Map<Id, Decimal> IdAndBlackListPhoneMap = New Map<Id, Decimal>();
    for(Application__c c: trigger.new){
        for(blacklist__c bp:balack){
            if(bp.pancard__c==c.Pancard__c){
                bp.mobile_num__c=c.mobile_num__c;
				IdAndBlackListPhoneMap.put(bp.Id, bp.mobile_num__c);
                newre.add(c);                
            }
        }
        if(newre.size() > 0){
			ApplicationHandler.updateBlackListPhone(IdAndBlackListPhoneMap);
			for(Application__c newAppRecord : newre){
				newAppRecord.adderror('applicant is in blacklist');
			}
		}
    }
}


CLASS: 

global class ApplicationHandler {
    @future 
    public static void updateBlackListPhone(Map<Id, Decimal> idAndPhoneMap){
        list<blacklist__c> blackListedPans = [select name,pancard__c,mobile_num__c from blacklist__c where Id IN:idAndPhoneMap.KeySet()];
        for(blacklist__c blck :blackListedPans){
            blck.mobile_num__c = idAndPhoneMap.get(blck.Id);            
        }
        Update blackListedPans;
    }          
}



Regards,
Ragards 
Shekhar Shekhar 8Shekhar Shekhar 8
TRIGGER
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;
     }          
}

 
Rakesh SamalRakesh Samal
Hello Shekhar Shekhar 8,
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
chaitra K 16chaitra K 16
Hi Manid,
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');
                    }
                }
            }
        }
    }
}