+ Start a Discussion
HB 1HB 1 

Trigger to match value against custom object.

Hi, I desperatly need some help creating my first trigger and i'm a bit lost.
I'm trying to create a trigger on a custom object called wp_leads__c which looks up a field called code_entered__c against another custom object called wp_cid__c on a field called coupon__c.

If wp_leads__c. code_entered__c = wp_cid__c.coupon__c I want to update a tickbox field called matched_code__c in the wp_leads__c object with a tick.

Please help!

Balaji Chowdary GarapatiBalaji Chowdary Garapati
@HB 1

 Couple of questions before i can suggest something:

1) What is the realtionship between wp_leads__c object and wp_cid__c (Is there any direct relationship??)
2) If there is no direct relationship, on what condition do you wanna check whether the code entered on WP  LEAD is matching against WP CID obejct??

  Your answer will help guiding you in right direction.

Thanks,
Balaji
HB 1HB 1

Thanks so much for getting back to me.

Theres no master-child relationship or lookup relationship between the two objects. What I want to do is tick a box called matched_code__c in the wp_leads__c if:
wp_leads__c. code_entered__c = wp_cid__c.coupon__c

Your help is very much appreciated!

Ben

 

Balaji Chowdary GarapatiBalaji Chowdary Garapati

@Ben.,

If there is no direct relation between those two objects,

  wp_leads__c and wp_cid__c are two different objects,  to compare the code_entered__c  field on wp_leads__c record, we need to query wp_cid__c records. So now my questions are:

>  what are the condtions those need to be considered to query the wp_cid__c records?
> If there are no conditions, are you trying to check whether the code_entered__c  from wp_leads__c record exists in wp_cid__c object or not? If so what should happen if you find multiple records in wp_cid__c object with the code you entered? 

  Im asking these questions not you confuse you :) but to know what the exact requirment is., hope you understand that!

Cheers.,

Thanks,
Balaji

HB 1HB 1
I understand and I appreciate your help! - As far as I know, the only conditions are if the two records match. - Yep thats correct, i'm trying to see if the code_entered__c in wp_leads__c exists in wp-cid__c. There wont be multiple records returned as the field is unique :) Hope that helps
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,

trigger demo on wp_leads__c(before insert, before update)
{
set<ID>  wpCID = new set<ID>();
for(wp_leads__c wp:trigger.new)
{
wpCID.add(wp.wp_cid__cID);
}

List<wp_cid__c> wpcids=[select id, name  ,coupon__c from wp_cid__c where id In :wpCID];

for(wp_leads__c wp:trigger.new)
{
if(wp. code_entered__c == wpcids[0].coupon__c)
{
wp.matched_code__c=true;
}
}
}
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Great! Then here is the code
 
Trigger VerifyCode on wp_leads__c(before Insert,before Update){
		
		set<String>CodesEnteredSet=new set<String>();
		Map<String,wp_cid__c>WPCodeMap=new Map<String,wp-cid__c>();
	for(wp_leads__c WpLeadRecord: Trigger.new){
			if(WpLeadRecord.code_entered__c != null){
				CodesEnteredSet.add(WpLeadRecord.code_entered__c); // Collect All the Codes entered
			}
	}
	
	//Time To Query And See If Those Codes Exist In wp_cid__c object
	
	for(wp_cid__c WPRecord:[select id, coupon__c,name From wp_cid__c where coupon__c IN : CodesEnteredSet]){
		
		WPCodeMap.put(WPRecord.coupon__c,WPRecord);
	}
	
	for(wp_leads__c WpLeadRecord: Trigger.new){

          // Verify If Code Exists Or Not 
			if(WpLeadRecord.code_entered__c != null && WPCodeMap.keyset().contains(WpLeadRecord.code_entered__c)){
				WpLeadRecord.matched_code__c= True; //Update your check box
			}
	}
	
}
Hope it helps.,

Thanks,
balaji
 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
Typo in previous post.,

check this out!
 
Trigger VerifyCode on wp_leads__c(before Insert,before Update){
		
		set<String>CodesEnteredSet=new set<String>();
		Map<String,wp_cid__c>WPCodeMap=new Map<String,wp_cid__c>();
	for(wp_leads__c WpLeadRecord: Trigger.new){
			if(WpLeadRecord.code_entered__c != null){
				CodesEnteredSet.add(WpLeadRecord.code_entered__c); // Collect All the Codes entered
			}
	}
	
	//Time To Query And See If Those Codes Exist In wp_cid__c object
	
	for(wp_cid__c WPRecord:[select id, coupon__c,name From wp_cid__c where coupon__c IN : CodesEnteredSet]){
		
		WPCodeMap.put(WPRecord.coupon__c,WPRecord);
	}
	
	for(wp_leads__c WpLeadRecord: Trigger.new){

          // Verify If Code Exists Or Not 
			if(WpLeadRecord.code_entered__c != null && WPCodeMap.keyset().contains(WpLeadRecord.code_entered__c)){
				WpLeadRecord.matched_code__c= True; //Update your check box
			}
	}
	
}

 
HB 1HB 1
Thanks you so much Balaji