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
thomasarnold81thomasarnold81 

Orphaned Cases and AccountID Apex Trigger Issue from a Newbie

Hi Guys,


I am looking for some help in regards to a trigger I am writing on the case object.


The case is currently orphaned as it has no account ID inserted with it. However, on the case I do have the name of the account in a text field wriiten exactly as it is shown on the account page. Why I have this data structured like this is a long dull story and I know there were probably better ways to do this when setting up my process but I am new at this.


Here's what I want to - I want a trigger to look at the text field with the account name, search for the AccountID and link the case to the correct account so it's not orphaned any more.


My code is below - where am I going wrong?

 

trigger CaseAccountUpdater2 on Case (before insert, before update) {

 

 for(Case cas : Trigger.New){  

 

   if(cas.AccountId != null) {  

      cas.Account = [Select ID    

      From Account    

      Where Name = :Cas.Council_Name_Text__c    

      Limit 1];  

    }

  }

}

Best Answer chosen by Admin (Salesforce Developers) 
thomasarnold81thomasarnold81

Thanks for replying!


Turns out there's a bit more to it than that. I got this code to work.......


trigger AccountIDUpdater4 on Case (before insert, before update) {
//Change the order owner before saving to the data base

//Create a set of Names to get Ids for
Set<string> accountnames = new Set<string>();
//Create of Map of Names and their respective Ids
Map<string,id> accountids = new Map<string,id>();

//Add Council_Names_Text__c to the Set

for(Case cas : trigger.new){
if(accountnames.contains(cas.Council_Name_Text__c)){
}
else{
accountnames.add(cas.Council_Name_Text__c);
}
}
system.debug('*****accountnames*****'+accountnames);

//Get the Ids for respective Council_Name_Text__c

for(Account u : [SELECT Id,Name FROM Account Where Name IN :accountnames]){
accountids.put(u.Name,u.Id);
}
system.debug('*****accounts and their ids*****'+accountids);

//Set proper account if not already set

for(Case cas : trigger.new){
system.debug('*****ownerID*****'+cas.AccountId);
system.debug('*****Sales Rep ID*****'+accountids.get(cas.Council_Name_Text__C));
if(cas.AccountId!=accountids.get(cas.Council_Name_Text__C))
cas.AccountId = accountids.get(cas.Council_Name_Text__C);
}


}

All Answers

CholericCholeric

Hey,

 

you're checking if the case got a lookup to an Account

[if(cas.AccountId != null) {  ]

As you want to set that lookup this makes no sence.

Try to check if the Case-Account lookup is NULL and if the Council_Name_Text__c is NOT NULL.

 

 

Stefan

thomasarnold81thomasarnold81

Thanks for replying!


Turns out there's a bit more to it than that. I got this code to work.......


trigger AccountIDUpdater4 on Case (before insert, before update) {
//Change the order owner before saving to the data base

//Create a set of Names to get Ids for
Set<string> accountnames = new Set<string>();
//Create of Map of Names and their respective Ids
Map<string,id> accountids = new Map<string,id>();

//Add Council_Names_Text__c to the Set

for(Case cas : trigger.new){
if(accountnames.contains(cas.Council_Name_Text__c)){
}
else{
accountnames.add(cas.Council_Name_Text__c);
}
}
system.debug('*****accountnames*****'+accountnames);

//Get the Ids for respective Council_Name_Text__c

for(Account u : [SELECT Id,Name FROM Account Where Name IN :accountnames]){
accountids.put(u.Name,u.Id);
}
system.debug('*****accounts and their ids*****'+accountids);

//Set proper account if not already set

for(Case cas : trigger.new){
system.debug('*****ownerID*****'+cas.AccountId);
system.debug('*****Sales Rep ID*****'+accountids.get(cas.Council_Name_Text__C));
if(cas.AccountId!=accountids.get(cas.Council_Name_Text__C))
cas.AccountId = accountids.get(cas.Council_Name_Text__C);
}


}

This was selected as the best answer