+ Start a Discussion
Rebecca PittsRebecca Pitts 

I am brand new to writing code and i contacted help and they can't seem to want to help me much can someone please help me PLEASE!!!

 i have a trigger that updates fields on an opportunity when it is created and anytime it is updated anytime after. It adds phone number fields and a few other fields that i can't map over. I need it to also update the opportunity name depending on if the account has a middle name or not. So i am dealing with if and else statements. I had a process builder done and it worked fine for a little bit but now it is not working at all. I will attach what it looks like as well as the trigger that i have so far. SO basically what i need to added to my trigger that i cant seem to figure out how to do is the opportunity name update.
For example-- If the account First Name: Bob --Middle Name: Null -Last Name: Smith  I would want Opportunity name to be updated to Bob Smith 
But if it was --First Name: Bob  - Middle Name: Mary -Last Name: Smith  I would want Opportunity name to be updated to Bob & Mary Smith 

this is the trigger that i have created that updates the opportunity fields 

trigger insertOpportunity on Opportunity(before insert,before update) {

if(trigger.isinsert)
{
for(Opportunity ac : Trigger.new){
try{

List<account> acc=[select id,Water_Source__c,Phone,PersonHomePhone,PersonMobilePhone,Water_Filters__c,PersonMailingStreet,PersonMailingCity,PersonMailingState,PersonMailingPostalCode,Water_Conditions__c,Type_of_Home__c from Account where id=: trigger.new[0].accountid];
trigger.new[0].Water_Source__c=acc[0].Water_Source__c;
trigger.new[0].Home_Phone__c=acc[0].PersonHomePhone;
trigger.new[0].Mobile_Phone__c=acc[0].PersonMobilePhone;
trigger.new[0].Water_Filters__c=acc[0].Water_Filters__c;
trigger.new[0].Street__c=acc[0].PersonMailingStreet;
trigger.new[0].City__c=acc[0].PersonMailingCity;
trigger.new[0].State__c=acc[0].PersonMailingState;
trigger.new[0].Zip_Code__c=acc[0].PersonMailingPostalCode;
trigger.new[0].Water_Condtitions__c=acc[0].Water_Conditions__c;
trigger.new[0].Type_of_Home__c=acc[0].Type_of_Home__c;
trigger.new[0].Phone__c=acc[0].Phone;
}
catch(Exception e ){
}
}
}

}



This is the formula that i had for the process builder that worked but now no longer does and this is what i need the opportunity name to look like 

IF(NOT(ISBLANK([Account].Spouse_Name__c)),
  [Account].FirstName & " " & "&" & " " & [Account].Spouse_Name__c & " " & [Account].LastName,
[Account].FirstName & " " &  [Account].MiddleName & " " &  [Account].LastName
 )
LBKLBK
Hi Rebecca,

Try this code. I have included your logic to update the opportunity name and bulkified the code to avoid hitting Governor limits (Moved your SOQL query outside the for loop).
 
trigger insertOpportunity on Opportunity(before insert,before update) {

	if(trigger.isinsert)
	{
		List<Id> lstAccountIds = new List<Id>();
		
		for (Opportunity opp : Trigger.new){
			lstAccountIds.add(opp.accountid);
		}
		Map<Id, Account> mapAccounts = [SELECT Id,Water_Source__c,Phone,PersonHomePhone,PersonMobilePhone,Water_Filters__c,PersonMailingStreet,PersonMailingCity,PersonMailingState,PersonMailingPostalCode,Water_Conditions__c,Type_of_Home__c FROM Account WHERE Id : lstAccountIds]

		for(Opportunity opp : Trigger.new){
			try{
				Account acc = mapAccounts.get(opp.accountid);
				opp.Water_Source__c=acc.Water_Source__c;
				opp.Home_Phone__c=acc.PersonHomePhone;
				opp.Mobile_Phone__c=acc.PersonMobilePhone;
				opp.Water_Filters__c=acc.Water_Filters__c;
				opp.Street__c=acc.PersonMailingStreet;
				opp.City__c=acc.PersonMailingCity;
				opp.State__c=acc.PersonMailingState;
				opp.Zip_Code__c=acc.PersonMailingPostalCode;
				opp.Water_Condtitions__c=acc.Water_Conditions__c;
				opp.Type_of_Home__c=acc.Type_of_Home__c;
				opp.Phone__c=acc.Phone;
				
				if(acc.Spouse_Name__c != null && acc.Spouse_Name__c != ')
					opp.Name = acc.FirstName + ' & ' + acc.Spouse_Name__c + ' ' + acc.LastName;
				else
					opp.Name = acc.FirstName + ' ' + acc.LastName;
			}
			catch(Exception e ){
			}
		}
	}
}
Let me know if you face any issues.

I have one question here. Why did you put all the code inside isInsert condition? If that is what you need, you can make the trigger as before insert only.

Hope this helps.
Rebecca PittsRebecca Pitts
I'm not sure I'm very new to all this and I wanted to make sure that it worked so I did them all to make sure it would do it and actually work. Rebecca Pitts RainSoft of the Wiregrass 201 W College St Enterprise, AL 36330 Tel:334-308-2782|
LBKLBK
I might have made a minor mistake in line 10 in the code above.

Please replace it with the following line.
 
Map<Id, Account> mapAccounts = [SELECT Id,Water_Source__c,Phone,PersonHomePhone,PersonMobilePhone,Water_Filters__c,PersonMailingStreet,PersonMailingCity,PersonMailingState,PersonMailingPostalCode,Water_Conditions__c,Type_of_Home__c FROM Account WHERE Id =: lstAccountIds]
And, please test it in your Sandbox and let me know if this helps.
 
Rebecca PittsRebecca Pitts
User-added imageIts giving me this error
Rebecca PittsRebecca Pitts
Compile Error: line breaks not allowed in string literals at line 27 column -1

thats the error in case you cant read the picture
 
LBKLBK
My bad. I missed a single quote in line 27.

It supposed to be
if(acc.Spouse_Name__c != null && acc.Spouse_Name__c != '')

 
Rebecca PittsRebecca Pitts
Error: Compile Error: expecting a semi-colon, found 'for' at line 12 column 0

now i got this lol 

im so sorry 
Rebecca PittsRebecca Pitts
for(Opportunity opp : Trigger.new){
LBKLBK
Oh...I have made so many syntactical errors in the code. Not my day.

Here is the updated code (But I am sure you have managed it already).
 
trigger insertOpportunity on Opportunity(before insert,before update) {

	if(trigger.isinsert)
	{
		List<Id> lstAccountIds = new List<Id>();
		
		for (Opportunity opp : Trigger.new){
			lstAccountIds.add(opp.accountid);
		}
		Map<Id, Account> mapAccounts = [SELECT Id,Water_Source__c,Phone,PersonHomePhone,PersonMobilePhone,Water_Filters__c,PersonMailingStreet,PersonMailingCity,PersonMailingState,PersonMailingPostalCode,Water_Conditions__c,Type_of_Home__c FROM Account WHERE Id =: lstAccountIds];

		for(Opportunity opp : Trigger.new){
			try{
				Account acc = mapAccounts.get(opp.accountid);
				opp.Water_Source__c=acc.Water_Source__c;
				opp.Home_Phone__c=acc.PersonHomePhone;
				opp.Mobile_Phone__c=acc.PersonMobilePhone;
				opp.Water_Filters__c=acc.Water_Filters__c;
				opp.Street__c=acc.PersonMailingStreet;
				opp.City__c=acc.PersonMailingCity;
				opp.State__c=acc.PersonMailingState;
				opp.Zip_Code__c=acc.PersonMailingPostalCode;
				opp.Water_Condtitions__c=acc.Water_Conditions__c;
				opp.Type_of_Home__c=acc.Type_of_Home__c;
				opp.Phone__c=acc.Phone;
				
				if(acc.Spouse_Name__c != null && acc.Spouse_Name__c != '')
					opp.Name = acc.FirstName + ' & ' + acc.Spouse_Name__c + ' ' + acc.LastName;
				else
					opp.Name = acc.FirstName + ' ' + acc.LastName;
			}
			catch(Exception e ){
				
			}
		}
	}
}

 
Rebecca PittsRebecca Pitts
Error: Compile Error: Illegal assignment from List<Account> to Map<Id,Account> at line 10 column 27
LBKLBK
Sorry Rebecca. I am not usually this bad in syntax. Because of those too many custom fields in your Account and Opportunity object, I have shared the code without much testing.

Please replace line 10 with the code below. This should work, because I have stripped out the custom fields lines in the trigger and ensured that it saves.
 
Map<Id, Account> mapAccounts = new Map<Id, Account>([SELECT Id,Water_Source__c,Phone,PersonHomePhone,PersonMobilePhone,Water_Filters__c,PersonMailingStreet,PersonMailingCity,PersonMailingState,PersonMailingPostalCode,Water_Conditions__c,Type_of_Home__c FROM Account WHERE Id =: lstAccountIds]);
Sorry again for putting you through this.
 
LBKLBK
Hi Rebecca,

Did this code work  for you at all?

Or, were you able to find an alternate solution for this issue?