You need to sign in to do that
Don't have an account?
Rebecca 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
)
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
)
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).
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.
Please replace it with the following line.
And, please test it in your Sandbox and let me know if this helps.
thats the error in case you cant read the picture
It supposed to be
now i got this lol
im so sorry
Here is the updated code (But I am sure you have managed it already).
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.
Sorry again for putting you through this.
Did this code work for you at all?
Or, were you able to find an alternate solution for this issue?