using SOQL Query not able to fetch Account Applicant_ID__c - salesforce

I have an external id in Account named Applicant_ID__c. I am using data loader to import data into salesforce Opportunity. Below is my mapping file content.
Date\ Cancelled=Date_Cancelled__c
Date\ Denied=Date_Denied__c
Date\ Decisioned=Date_Decisioned__c
App\ Status=Application_Status__c
Date\ Submitted=Application_Submitted__c
Closing\ Date=CloseDate
Application\ Source=Application_Source__c
Application\ Type=Application_Type__c
Application\ Sub-Type=Application_Sub_Type__c
App\ ID=App_ID__c
Property=Property_Name__r\:Property_Code__c
Applicant\ ID=Account\:Applicant_ID__c
Record\ Type\ ID=RecordTypeId
The above mapping is working correctly now what i want is to populate the opportunity name from trigger.
Below is my trigger content
trigger MapStatusToStageBeforeOpportunintyCreation on Opportunity (before insert, before update) {
for (Opportunity o : Trigger.New){
Account acc = [Select LastName From Account Where Applicant_ID__c =:Account:Applicant_ID__c];
o.Name =acc.LastName;
}
}
Thanks in advance.

That answer you created and excepted is going to blow up if insert 101 Opportunities, but if you want to use the Applicant_ID__c you can query it out in the account query
trigger MapStatusToStageBeforeOpportunintyCreation on Opportunity (before insert, before update)
{
Set<ID> acctIDS = new Set<ID>();
for (Opportunity o : Trigger.new)
{
if(o.AccountId != null)
{
acctIDS.add(o.AccountID);
}
}
Map<ID, Account> acctMap = new Map<ID, Account>([Select LastName, Applicant_ID__c From Account Where ID =: acctIDS]);
for(Opportunity o : Trigger.new)
{
for(ID acctID :acctMap.keySet())
{
if(o.AccountID == acctID)
{
o.Lastname = acctMap.get(acctID).LastName;
}
}
}
}

You are querying it wrong
First, you should Never Query in for loop
List'<'Opportuniy opplist = new list'<'Opportunity'>'();<Br/>
// Remove the single quotes <br/>
for (Opportunity o : Trigger.New){<Br/>
o.OpportunityApplicentID = o.Account.Applicant_ID__c;<Br/>
o.Name =acc.LastName;<Br/>
opplist.add(o);<Br/>
}
update opplist;

+Instead of using Applicant_ID__c =:Account:Applicant_ID__c this..Just use Applicant_ID__c =:Account.Applicant_ID__c.Don't use colon .Use dot operator

Related

salesforce update trigger on serviceterritory object

I have below requirement and I'm new to SF development, need assistance if my approach is correct or not.
When any update happens on ServiceTerritory object, if the Time_Zone__c field of ServiceTerritory is not matching with User Object TimeZoneSidKey field, then update ServiceTerritory object Time_Zone__c field with User Object TimeZoneSidKey field.
ServiceTerritory object : has Center_Instructor_Contact__c field tagged to ID field in Contact object.
Contact object : has ID field and AccountId field
User Object : has AccountId field
public static void afterUpdate(List<ServiceTerritory> serviceTerritories, Map<Id, ServiceTerritory> oldRecords) {
Set<Id> recIds = new Set<Id>();
for (ServiceTerritory record : serviceTerritories) {
recIds.add(record.Id);
}
Set<Id> STMembers = new Set<Id>();
for (ServiceTerritory member : [SELECT Id, Center_Instructor_Contact__c FROM ServiceTerritory WHERE Id IN :recIds]) {
STMembers.add(member.Center_Instructor_Contact__c);
}
//Contact object : has ID field and AccountId field
Set<Id> ContactIDs = new Set<Id>();
for (Contact Cnt : [SELECT AccountId FROM Contact WHERE Id IN :STMembers]) {
ContactIDs.add(Cnt.AccountId);
}
//User Object : has AccountId field
Set<Id> UserIDs = new Set<Id>();
for (User Cnt : [SELECT AccountId, TimeZoneSidKey FROM User WHERE AccountId IN :ContactIDs]) {
UserIDs.add(Cnt.AccountId);
}
}
and here how to compare and update ServiceTerritory object if the timezone is not matching between the objects.
Not many Salesforce instances will have access to "Field Service Lightning" which is where ServiceTerritory table is used. If you sign up for free Salesforce Developer Edition it probably won't exist there. So it's bit hard to understand the question and help. Plus if you wrote "User Object : has AccountId field" it sounds like you're using Experience Cloud (formerly known as communities), that narrows down the specialists even more.
So it's Service Territory -> "up" to -> Contact -> "down" to (community) -> User?
If you're using community users they'll have AccountId and ContactId in them, no need going via Account (and in fact you could get stupid results that way... What if not all contacts in account are community-enabled, what if they are but have different timezones...)
Try something like that but you'll have to experiment a lot. And change your code to run "before update", you'll get save to database for free
List<ServiceTerritory> serviceTerritories; // passed to your function
Set<Id> contactIds = new Set<Id>();
for (ServiceTerritory st : serviceTerritories) {
contactIds.add(st.Center_Instructor_Contact__c);
}
System.debug(contactIds);
// Grab all these Contacts and their community users (it's a related list so it'll be a subquery but really there will be at most one user
Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id,
(SELECT TimezoneSidKey FROM Users)
FROM Contact
WHERE Id IN :contactIds AND Id IN (SELECT ContactId FROM User)]);
// Loop again and check against "reference data"
for (ServiceTerritory st : serviceTerritories) {
if(contacts.containsKey(st.Center_Instructor_Contact__c)){
Contact c = contacts.get(st.Center_Instructor_Contact__c);
System.debug(c);
System.debug('comparing ' + st.Time_Zone__c + ' and ' + c.Users);
if(st.Time_Zone__c != c.Users[0].TimezoneSidKey){
System.debug('fixing ' + st.Id);
st.Time_Zone__c = c.Users[0].TimezoneSidKey;
}
}
}

Apex Trigger- how to update all case owners using trigger?

when user clicks on case to change caseOwner/user, i have to add this user to all associated cases(per customer). I am using below code ,but it is updating only one record/case. i can see in debug logs all cases were updated with latest owner but not really updated/stored in caseObject. please help me this.
trigger caseAssignment on Case (after insert, after update) {
set<id> ownerId = new Set<Id>();
set<id> customerId = new set<Id>();
for(Case caseobj : trigger.new){
ownerId.add(caseobj.OwnerId);
customerId.add(caseobj.AccountId);
}
for( User user:[Select id, FirstName, LastName from user where Id IN :ownerId]){
for(Case cas : [Select Id, OwnerId, First_Name__c, CaseNumber, AccountId From Case where AccountId IN: customerId]){
cas.OwnerId = user.Id;
}
}
Without an update DML statement, your code will have no effect on your data.
Probably should create an update list
i.e. list updateCaseList = new list()
Then in your inner for loop, add "cas" to updateCaseList and then at the end of the method, issue the update command (update updateCaseList).
Assuming that the logic you wrote is correct you're never updating the cases
trigger caseAssignment on Case (after insert, after update)
{
Set<Id> ownerId = new Set<Id>();
Set<Id> customerId = new Set<Id>();
List<Case> casesToUpdate = new List<Case>();
for(Case caseobj : trigger.new)
{
ownerId.add(caseobj.OwnerId);
customerId.add(caseobj.AccountId);
}
for( User user:[Select id, FirstName, LastName from user where Id IN :ownerId])
{
for(Case cas : [Select Id, OwnerId, First_Name__c, CaseNumber, AccountId From Case where AccountId IN: customerId])
{
Case c = new Case();
c.ID = cas.ID;
c.OwnerId = user.Id;
casesToUpdate.add(c);
}
}
update casesToUpdate;
}

Trigger on Task Salesforce

I am trying to write a trigger to update the 'Name' field (so WhoId in the API) to the 'Name' (ContactId) of the custom Primary Contact field of the Account the Task is related to.
trigger updateNameToPrimary on Task (after insert, after update) {
for(Task t : Trigger.new) {
t.WhoID = [SELECT Account.Id
FROM Account
WHERE Id = :t.Id].Custom_Primary_Contact__c;
}
}
I have been doing some testing and don't think it's working and cannot figure out why. Just looking for a point in the right direction as I still am in the learning process.
Change Trigger from "after insert after update" To "before insert before update".
Something like this
trigger updateNameToPrimary on Task (before insert, before update) {
set<Id> accIdSet = new set<Id>();
for(Task t : Trigger.new) {
if(t.AccountId!=null)
accIdSet.add(t.AccountId);
}
map<Id,Account> accMap = new map<Id,Account>([select Custom_Primary_Contact__c from Account where Id in:accIdSet]);
for(Task t : Trigger.new) {
if(t.AccountId!=null && accMap.containsKey(t.AccountId))
t.WhoID = accMap.get(t.AccountId).Custom_Primary_Contact__c;
}
}

Converting Trigger to Batch Apex: Help needed to make the code work

If possible, could anyone please direct me towards the right direction? The second code (Batch Apex) is just not compiling. Currently the error is,
Error: Compile Error: Invalid type: updateContactOnEmailOptOutChangeScheduler
at line 63 column 73
But I think there are other issues to that I can't seem to make it right.
On a contact update, the trigger updates all duplicate contacts with new value of "Email Opt Out" if this field has been updated. Also, the trigger only updates duplicate contacts that have a HasOptedOutOfEmail value different from one being updated. Now my task is to convert this requirement from trigger (that was written and tested by my colleague) to Batch Apex. First is the original trigger. Second is the code I just wrote in the format of batch apex.
Original Trigger Code
trigger updateContactOnEmailOptOutChange on Contac​t (after update) { ​ ​
//Initialize lists and maps
List<Contact> duplicateContacts = new List<Con​tact>();
Map<String, Contact> contactEmailMap = new Map​<String, Contact>();
Map<Id, Contact> contactIdMap = new Map<Id, Co​ntact>();
//Build a map with contacts to update. Only se​lect the ones that have a different "Email Opt Out​" value from the contact being updated.
for (Integer i = 0; i < Trigger.new.size(); i+​+) {
if (Trigger.old[i].HasOptedOutOfEmail != T​rigger.new[i].HasOptedOutOfEmail) {
contactEmailMap.put(Trigger.old[i].ema​il, Trigger.new[i]);
contactIdMap.put(Trigger.old[i].id, Tr​igger.new[i]);
}
}
//Only go through this process if "Email Opt O​ut" (HasOptedOutofEmail) was updated.
If (contactIdMap.size()>0) {
//Query the database and look for all cont​acts with a duplicate email address (same email as​ the contact currently being updated).
for (Contact dupContact : [SELECT Id, Name​, Email, HasOptedOutOfEmail
FROM Contact
WHERE Email IN ​: contactEmailMap.KeySet()
AND Id NOT IN :​ contactIdMap.KeySet()]) {
Contact contact = contactEmailMap.get(​dupContact.Email);
If (dupContact.HasOptedOutOfEmail <> c​ontact.HasOptedOutOfEmail) {
dupContact.HasOptedOutOfEmail = co​ntact.HasOptedOutOfEmail;
duplicateContacts.add(dupContact);
}
}
//If any duplicate contacts were found, up​date all duplicate contacts with the new HasOptedO​utOfEmail value.
If (duplicateContacts.size()>0) update dupl​icateContacts;
}
}
BATCH APEX
global class updateContactOnEmailOptOutChange implements Database.Batchable<sObject>
{
global string query;
global updateContactOnEmailOptOutChange()
{
query = 'SELECT id,Name, Email, HasOptedOutofEmail from Contact where HasOptedOutofEmail=true';
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List <sObject> duplicateContacts)
{
Map<String, Contact> contactEmailMap = new Map<String, Contact>();
Map <Id, Contact> contactIdMap = new Map<Id, Contact>();
// Build a map with contacts to update. Only select the ones that have a different "Email Opt Out" value from the contact being updated.
if(trigger.isUpdate){
for(Integer i=0; i<Trigger.new.size();i++)
{
if(Trigger.old[i].HasOptedOutOfEmail != Trigger.new[i].HasOptedOutOfEmail)
{
contactEmailMap.put(Trigger.old[i].email, Trigger.new[i]);
contactIdMap.put(Trigger.old[i].id, Trigger.new[i]);
}
}
if(contactidMap.size()>0)
{
//Query the database and look for all contacts with a duplicate email address(same email as the contact currently being updated)
for (Contact dupContact: [SELECT Id, Name, Email, HasOptedOutofEmail
FROM Contact
WHERE Email IN: contactEmailMap.KeySet()
AND Id NOT IN: contactIdMap.KeySet()])
{
Contact contact=contactEmailMap.get(dupContact.Email);
If(dupContact.HasOptedOutOfEmail <> contact.HasOptedOutOfEmail)
{
dupContact.HasOptedOutOfEmail = contact.HasOptedOutOfEmail;
duplicateContacts.add(dupContact);
}
}
// if any duplicate contacts were found, update all duplicate contacts with the new HasOptedOutOFEmail value.
If(duplicateContacts.size<>0) update duplicateContacts;
}
}
}
//The batch process has completed successfully. Schedule next batch.
global void finish(Database.BatchableContext BC){
// //Build the system time of now + 300 seconds to schedule the batch apex.
Datetime sysTime = System.now();
sysTime = sysTime.addSeconds(300);
String chron_exp=''+sysTime.second()+''+sysTime.minute()+​''+sysTime.hour()+''+sysTime.day()+''+sysTime.mont​h()+'?'+sysTime.year();
system.debug(chron_exp);
updateContactOnEmailOptOutChangeScheduler scheduleFieldUpdate = new updateContactOnEmailOptOutChangeScheduler();
//Schedule the next job, and give it the system time so name is unique
System.schedule('New Email Update Job'+sysTime.getTime(),chron_exp,scheduleFieldUpda​te);
}
}
Your batch apex is including references to the Trigger class that is only valid inside a trigger. The compile error message says there is an invalid type on line 63. Check the line numbers but that likely points to the reference to Trigger

Salesforce Account Owner Name on Case Trigger failing

I wrote a trigger that places the Account Owner Name on a Case created for that Account. It works and performs also in my bulk test of 200. Here is the code:
trigger CaseBeforeInsertUpdate on Case (before insert, before update) {
Set<Id> accountIds = new Set<Id>();
Set<Id> accountOwnerIds = new Set<Id>();
for (Case c : Trigger.new) {
if(c.AccountId != null) {
accountIds.add(c.AccountId);
}
}
Map<Id,Account> accountMap = new Map<Id, Account>([select Id, OwnerId from Account where Id IN :accountIds]);
for (Account a : accountMap.values()) {
if(a.OwnerId != null) {
accountOwnerIds.add(a.OwnerId);
}
}
Map<Id, User> userMap = new Map<Id,User>([select Name from User where Id IN :accountOwnerIds]);
if(userMap.size() > 0) {
for(Case c: Trigger.new) {
c.MerchantOwner__c = userMap.get(accountMap.get(c.AccountId).OwnerId).name;
}
}
}
By accident I discovered a bug and I can't figure out what is going wrong. If I go to a Case listview (i.e. My open cases) and select multiple Cases and Close them, I get an error: System.NullPointerException: Attempt to de-reference a null object for the row that updates the MerchantOwner field. When I mass close cases in my testclass everything works fine..
My best guess is I'm trying to do this for a Case that has no Account attached to it but as far as I see I try to not have these Cases updated by not adding them to the accountIds Set in the first place.
Does anyone know what I am doing wrong? Any help would be greatly appreciated.
I would change the following For loop from:
for(Case c: Trigger.new) {
c.MerchantOwner__c = userMap.get(accountMap.get(c.AccountId).OwnerId).name;
}
To
for(Case c: Trigger.new) {
if (c.AccountId != null // Make sure there is an Account linked to the Case
&& accountMap.ContainsKey(c.AccountId) // Make sure our Account query captured it
&& accountMap.get(c.AccountId).OwnerId != null // Make sure that account has an owner
&& usermap.ContainsKey(accountMap.get(c.AccountId).Ownerid) // Finally make sure our User query found the owner
){
c.MerchantOwner__c = userMap.get(accountMap.get(c.AccountId).OwnerId).name;
}
}

Resources