You need to sign in to do that
Don't have an account?
TRIGGER - attempt to Create a New Record on Custom Object when field on Standard Object changes.
I am trying to get this trigger to fire a new record on the custom object SVOC__c every time the field Account.SVOC__c is filled in OR changed with a number/text. or goes from nothing to something. Also, this field should always be unique (it already is set to "do not allow duplicate values" --> but i need to know if someone is trying to create the same number/text combination.
It compiles fine, and it runs, but is not creating the new record.
In debugging the trigger is not going through past the second debug method.
Any help would be greatly appreciated.
Thanks,
Chris
trigger AccountSVOC on Account (after update)
{
List <SVOC__c> SVOCs = new List <SVOC__c> ();
Map <Id, Account> oldmap = new Map <Id, Account>();
Map <Id, Account> newmap = new Map <Id, Account>();
System.debug('Chris in trigger');
for(Account a: Trigger.new)
{
System.debug('Chris in account loop');
if ((Trigger.oldMap.get(a.Id).SVOC__c != Trigger.newMap.get(a.Id).SVOC__c) && Trigger.newMap.get(a.Id).SVOC__c != null )
{
System.debug('Chris in status changed');
SVOC__c SVOC = new SVOC__c ();
SVOC.SVOC_ID__c = a.Id;
System.debug('Chris creating new SVOC and adding to list');
SVOCs.add(SVOC);
}
}
if(SVOCs.size()>0)
{
System.debug('Chris has values to insert = '+ SVOCs.size());
try{
insert SVOCs;
}catch (System.Dmlexception e)
{
system.debug (e);
}
}
}
Just cleaning up the trigger..
Try it that way.
Best Regards.
So it's working fine... for sure
In the case that it is a duplicate Account.SVOC__c, it doesn't create a new SVOC__c Record (b/c the SVOC__c custom object has the SVOC__c.SVOC_ID__c custom field set to not allow duplicate values) - which is great!!!
However:
I need to return an error to the Account when trying to save something that is already being used by the SVOC__c custom object at the Account.SVOC__c field. or - return an error that states that "SVOC is currently in use, please choose a unique SVOC number"...
Is there a way that I can do that?
Thanks in advance for all of your time & help,
Chris
Hi Chris.
Please try below :-
trigger AccountSVOC on Account (after update)
{
List <SVOC__c> SVOCs = new List <SVOC__c> ();
Map<Id,Account> accMap = new Map<Id,Account>([select SVOC__c from Account]);
System.debug('Chris in trigger');
for(Account a: Trigger.new)
{
System.debug('Chris in account loop');
if( (a.SVOC__c != null ) && (a.SVOC__c != Trigger.oldMap.get(a.Id).SVOC__c) )
{
if(!accMap.containsKey(a.SVOC__c)){
System.debug('Chris in status changed');
SVOC__c SVOC = new SVOC__c ();
SVOC.SVOC_ID__c = a.Id;
System.debug('Chris creating new SVOC and adding to list');
SVOCs.add(SVOC);
}
else
a.addError('Please select a different SVOC');
}
}
if(SVOCs.size()>0)
{
System.debug('Chris has values to insert = '+ SVOCs.size());
try{
insert SVOCs;
}catch (System.Dmlexception e)
{
system.debug (e);
}
}
}
But Chris, you said that Account.SVOC__c is an unique field, right? So it isn't a lookup field.. I think with that unique attribute is not possible that someone save an Account with a repeated value, therefore this trigger will never be execute in that case.
I'm right?
Regards
Account.SVOC__c is not a unique field, which is why I want to create this 1:1 SVOC__c custom object on the backend that holds a unique field (being pulled from the Account object (Account.SVOC__c field)). The Account object has reached its limit of 3 unique IDs, unfortunatelty - and I can't remove any of these unique IDs (or make any of these existing fields not unique).
Ahhh ok! You can ask to support for more unique's fields logging a case. Do you know that?
One question, What type is the Account.SVOC__c field? Is a Text Field?
Yes, text field. I can just ask SFDC support for more unique fields? lol...
@Vinit
This code is not working... (it's good to know this solution for future cases, anyway where I want to do this). I would like to solve this issue - even though the solution seems to be just to ask SFDC support for more unique IDs. lol.
"Error:Apex trigger AccountSVOC caused an unexpected exception, contact your administrator: AccountSVOC: execution of AfterUpdate caused by: System.StringException: Invalid id: 13579: External entry point"
Plus it's, on existing records, it's now returning the SVOC__c Custom Object's SFDC Id to the Account - and it needs to return the SVOC text, not the Id.
:)
Chris
Yes, you can. :D
Anyway... be very careful if you are saving IDs in Text Field, is not the same. SOQL isn't case sensitive so if you execute a query filtering by this field, you can get unexpected results. For example:
I could get :
So you cannot trust the query, you will need to re-filter in Apex (Apex is CaseSensitive)
Here is the code I'm using now, which is working, but again - is not a 1:1 relationship.
If I update the Account.SVOC__c field, it creates a NEW SVOC__c Record.
I only want to be able to create 1 of these SVOC__c records per Account.
If there is an update, I just want it to update the existing SVOC__c Record.
Thanks in advance for any more help.
FYI:
Account.SVOC__c = Text Field
SVOC__c.SVOC_ID__c = Text Field (Unique ID)
Which is why the code is written as it is below.
My code:
Thanks Juan,
I have requested a new Unique ID field - with business case, lol. They really don't want to give it up, huh?
Anyway - see my new and improved request above. Any more help would be appreciated...
Thanks a million!
It's really complicate, you have to consider a lot of things..
If I update the Account.SVOC__c field, it creates a NEW SVOC__c Record.
If the SVOC__c doesn't exist.
I only want to be able to create 1 of these SVOC__c records per Account.
You can check it on the Before Insert event, you could query if there is another Account with the same SVOC___c.
If there is an update, I just want it to update the existing SVOC__c Record.
But what happens when the SVOC__c value is changed to another value? What happens with the old SVOC record?, and what do you have to do with the new one? What happens if the account is deleted?
You have a lot of event that you need to take care for keeping the data integrity
It's really complicate, you have to consider a lot of things.. yes, agreed
If I update the Account.SVOC__c field, it creates a NEW SVOC__c Record.
If the SVOC__c doesn't exist. Yes.
I only want to be able to create 1 of these SVOC__c records per Account.
You can check it on the Before Insert event, you could query if there is another Account with the same SVOC___c. That sounds like a good idea, how would I do that? I'm sorry for so many questions. I am still a noob, for sure.
If there is an update, I just want it to update the existing SVOC__c Record.
But what happens when the SVOC__c value is changed to another value? It should change on the existing SVOC object to whatever it has been changed to on the Account.
What happens with the old SVOC record? "There can be only one!" (Highlander - at 1:40 or so), so there shouldn't be an old SVOC record... I mean that's what I want to make happen.
and what do you have to do with the new one? Nothing, there should not be a new one, it should update the existing SVOC Record.
What happens if the account is deleted? The SVOC Record should be deleted. :)
You have a lot of event that you need to take care for keeping the data integrity. Yes
Try comparing maps:
if( Trigger.newMap.get(a,Id).SVOC__c != null && (Trigger.newMap.get(a,Id).SVOC__c != Trigger.oldMap.get(a.Id).SVOC__c) )
If you don't want it to run more than once, add a boolean to a public class where you keep constants.
i.e.
public class Constants
{
public static boolean THISTRIGGERNAME_FIRST_RUN = True;
}
at the end of your trigger (before closing) set:
THISTRIGGERNAME_FIRST_RUN = False;
When I try the above with this in my Constants class:
I get the error: Error: Compile Error: unexpected token: private at line 1 column 0
I've tried the following
with this in my Constants Class:
Here is my code, but it's still duplicating whenever I edit an existing Account.SVOC__c:
You need to check that it is True or don't let it run.
Hello,
I am still getting duplicate SVOC Records...
public class:
Trigger:
Chris, about this topic:
You can check it on the Before Insert event, you could query if there is another Account with the same SVOC___c. That sounds like a good idea, how would I do that? I'm sorry for so many questions. I am still a noob, for sure.
SVOC___c contains an ID or a alphanumeric text? Is it CaseSensitive or "AAA" is the same of "aaa"?
SVOC__c is an alphanumeric text field... on the Account object. It is an alphnumeric key assigned somewhere else (a parent company) - and given to us. However, that is a whole other story that I will be working to automate in the future...
It is not currently case sensitive - "AAA" is the same as "aaa".
At this point, I don't have the reqs on whether or not it needs to be - but, it is something that I will find out.
But, that being said, for now it will remain case insensitive.
Thanks,
Chris
I did it in a notepad, so maybe you will have to correct the syntaxis:
It's make sense to you?
PD: You will need to create a class "MyException" (or whatever) extended from Standard Exception Class
This works, except that it is still creating duplicate SVOC__c records when the Account.SVOC__c field is changed.
Thanks, Juan for the effort you put into this, however there is still not a 1:1 relationship... can anyone help to prevent duplicate records on this field change?
I just want to make sure to update the existing SVOC__c record if this field on the Account changes instead of creating a NEW SVOC__c record.
:)