+ Start a Discussion
Adam NilaAdam Nila 

Trigger to add account name to opportunity name if not already present

I'm working on a trigger that will run before update or before insert.  As the title suggests, I want the trigger to add the account name to the opportunity name but only if the opportunity name is not already present.  The before insert part is easy since lazy sales reps are already used to this being done for them.  But if/when they update an opportunity name, I don't want the account name added a second time.  Here is what I've got so far:
 
trigger OppNameTrigger on Opportunity (before insert, before update) {
    Set<Id> AccountIds = new Set<Id>(); //Initialize a list of Ids
	String sName; //Initialize a string variable to manipulate our Opp names
    String accName;//Initialize a string variable to hold account name value when opp is updated instead of inserted
    
    //Add an account ID to the list for every opportunity caught by trigger
    for (Opportunity op : Trigger.new) {
  
 	  accountIds.add(op.AccountId);
	}
    
//Create a map of Ids and account, querying the name from account records
//whose Id is in the AccountIds list generated earlier
Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :accountIds]);
    System.debug(accountMap);

//Loop through all opps setting the new name in a variable and then assigning that
//variables contents to the name field for each opportunity
for (Opportunity op : Trigger.new) {
    //Assigns account name of current op to a variable
    
    accName = accountMap.get(op.AccountId).Name;
    if(trigger.isInsert){
  		// add the account name and amount to current opp name
  		sName = accountMap.get(op.AccountId).Name + ' - ' + op.Name;

  		// set the name :-)
  		op.Name = sName;
    }else {
        if(trigger.oldmap.get(op.Name) != trigger.newmap.get(op.name)){
            if(!op.name.contains(accountMap.get(op.AccountId).Name))
                
                sName = accName + ' - ' + op.Name;
            	op.Name = sName;
        }
    }
    }
}
When I try to edit the opportunity, I get the following error, "Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger OppNameTrigger caused an unexpected exception, contact your administrator: OppNameTrigger: execution of BeforeUpdate caused by: System.StringException: Invalid id: Testing Testing Opps: External entry point".  Anybody have any thoughts on what i'm doing wrong?
 
Best Answer chosen by Adam Nila
ManojSankaranManojSankaran
Hi Adam,

The issue is on line number 30
if(trigger.oldmap.get(op.Name) != trigger.newmap.get(op.name)){

For trigger.oldMap and trigger.NewMap the key is "Id"

You are passing "op.name" instead use "op.id" it will work.

The error you got says that you are using a string in a place we need to use an ID. Thats the reason you got an error saying Invalid ID.


Mark it as answer if it solves your problem.

Thanks
Manoj S

All Answers

ManojSankaranManojSankaran
Hi Adam,

The issue is on line number 30
if(trigger.oldmap.get(op.Name) != trigger.newmap.get(op.name)){

For trigger.oldMap and trigger.NewMap the key is "Id"

You are passing "op.name" instead use "op.id" it will work.

The error you got says that you are using a string in a place we need to use an ID. Thats the reason you got an error saying Invalid ID.


Mark it as answer if it solves your problem.

Thanks
Manoj S
This was selected as the best answer
Dhanya NDhanya N
Hi Adam,

Please check the below code.
trigger OppNameTrigger on Opportunity (before insert, before update) {
    Set<Id> AccountIds = new Set<Id>(); //Initialize a list of Ids
    String sName; //Initialize a string variable to manipulate our Opp names
    String accName;//Initialize a string variable to hold account name value when opp is updated instead of inserted
    
    //Add an account ID to the list for every opportunity caught by trigger
    for (Opportunity op : Trigger.new) {
  
      accountIds.add(op.AccountId);
    }
    
    //Create a map of Ids and account, querying the name from account records
    //whose Id is in the AccountIds list generated earlier
    Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Name FROM Account WHERE Id IN :accountIds]);
    System.debug(accountMap);

    //Loop through all opps setting the new name in a variable and then assigning that
    //variables contents to the name field for each opportunity
    for (Opportunity op : Trigger.new) {
        //Assigns account name of current op to a variable
        
        accName = accountMap.get(op.AccountId).Name;
        if(trigger.isInsert){
            // add the account name and amount to current opp name
            sName = accountMap.get(op.AccountId).Name + ' - ' + op.Name;
    
            // set the name :-)
            op.Name = sName;
        }
        else {
            if(trigger.oldmap.get(op.Id).Name != trigger.newmap.get(op.Id).Name){
            
                sName = op.Name;
				op.Name = sName;
            }
        }
    }
}

Thanks,
Dhanya