You need to sign in to do that
Don't have an account?
Proposal Button
Trigger is saved but unable to throw the results in the object
Hi
I created an custom object called Campaign Alert in that Account is lookup. There is a field in campaign alert called phone no and there is a field called Main BTN in account. If anyone gives the phone no in campaign alert it needs to search whether the phone no with BTN in account and that account needs to popup in the account field in the Campaign alert. I wrote this trigger and the trigger is saved but when i am search with phone no it is not throwing the account. Please let me know where did i went wrong
trigger relatedaccount on CampaignAlert__c (after insert, after update) { Map<String, CampaignAlert__c> mapPhones = new Map<String, CampaignAlert__c>(); for (CampaignAlert__c c: Trigger.new) { if (c.phone_No__c != null) { mapPhones.put(c.phone_No__c, c); } } List<String> listPhones = new List<String>(); Map<String, Account> mapAccounts = new Map<String, Account>(); for (list<Account> acts : [Select Id, Main_BTN__c from Account where Main_BTN__c in :listPhones]) { for (Account act : acts) { mapAccounts.put(act.Main_BTN__c, act); } } List<CampaignAlert__c> listCampaignAlerts = new List<CampaignAlert__c>(); for (CampaignAlert__c c: mapPhones.values()) { if(mapAccounts.get(c.phone_No__c)==null) continue; CampaignAlert__c ca = new CampaignAlert__c(Name = c.Name); ca.Account__c = mapAccounts.get(c.phone_No__c).ID; listCampaignAlerts.add(ca); } update listCampaignAlerts; } |
You queried using listPhones, but it's empty when the query runs, so that query will return no results:
[Select Id, Main_BTN__c from Account where Main_BTN__c in :listPhones]
I'm guessing that you wanted to pull the list of phone numbers from CampaignAlert__c, so an easy way is to add them to listPhones in the previous for loop:
Hi
Thanks for your replay. I make changes like you said but when I create a new record its throwing this error
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger relatedaccount caused an unexpected exception, contact your administrator: relatedaccount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.relatedaccount: line 9, column 13
line 9 is which line you said me to add
please look it and help me if you can
It might be that listPhones was empty when the query ran, so you could do a check to only run the query & loop if listPhones.size()>0.
That error indicates some info is missing from line 9 - could be the variable, that the query returned nothing, or something else.