+ 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