You need to sign in to do that
Don't have an account?
Trigger on user object to auto populate fields on custom object
Hi Developers, I am new to salesforce, facing problem trying to figure out this question.
Reuirement - Write trigger on user and create a field on user named - DateOfUser__c of type text. Create a custom object named - New_Object__c with fields as MatchingDates__c (type - date) and a lookup field to user named RelatedUser__c.
Whenever a user is created with a single value in DateOfUser__c field (means date in the format of MM/DD/YY) then a record should be created on custom object with same name as user's name and same date should be auto populated in the custom object's MatchingDates__c field.
In addition to this if a user is created with multiple values seperated by ';' in DateOfUser__c field then records should be created on custom object based on the number of dates using spilt function.
For example if I create a user - Ananya with Values in DateOfUser__c as : '01/29/2020;01/30/2020' then 2 records should be created in custom object with same fields as user's fields.
Fields of user to use here are : Id, Name, DateOfUser__c
Fields of custom object to use here are : Id, Name, MatchingDates__c, RelatedUser__c
Any help will be much appreciated.
Thanks in advance. :)
Note: I pretended that Account was my custom object that I wanted to insert records to after a user records was created. To customize the code for your scenario, replace "Account" with "New_Object__c" everywhere it is reference
Apex Class Code:
Apex Trigger Code:
Hi Christan, you meaningful response was quite helpful. Meanwhile, I wrote an alternative code for this trigger. Can you help me with the test class of this along with assertions. Your help will be much appreciated.
Thanks a lot.
Class Code :
public class AutoPopulate {
@future
public static void newobjmethod(String jsonString, String jsonOldMap) {
List<User> userlist = (List<User>)JSON.deserialize(jsonString, List<User>.class);
system.debug('list of New_Object__c after json--'+userlist);
map<id, User> useridmap = (Map<id, User>) JSON.deserialize(jsonOldMap, map<id, User>.class);
system.debug('map of user after json--'+useridmap);
list<New_Object__c> objlist = new list<New_Object__c>();
for(User u : userlist)
{
system.debug('class first loop--'+u);
if(u.DateOfUser__c != null)
{
system.debug('class first if statement--'+u.DateOfUser__c);
if(useridmap!=null && useridmap.size()>0 && u.DateOfUser__c!=useridmap.get(u.id).DateOfUser__c)
{
if(u.DateOfUser__c.contains(';'))
{
system.debug('third if--'+u.DateOfUser__c.contains(';'));
for(String s : u.DateOfUser__c.split(';'))
{
system.debug('class for--'+s);
New_Object__c n = new New_Object__c();
n.RelatedUser__c = u.Id;
n.MatchingDates__c = date.valueof(s);
n.Name = u.LastName;
objlist.add(n);
system.debug('new record added to objlist?--'+objlist);
}
}
else{
New_Object__c nn = new New_Object__c();
nn.RelatedUser__c = u.Id;
nn.MatchingDates__c = date.valueof(u.DateOfUser__c);
nn.Name = u.LastName;
objlist.add(nn);
}
}
}
}
if(objlist!=null && !objlist.isEmpty())
{
insert objlist;
system.debug('inserted records or not?---'+objlist);
}
}
}
Trigger Code :
trigger AutoPopulateFromUser on User (after update) {
if(trigger.isAfter && trigger.isUpdate){
String jsonString = JSON.serialize(trigger.new);
String jsonOldMap = JSON.serialize(Trigger.oldMap);
AutoPopulate.newobjmethod(jsonString,jsonOldMap);
}
}
Scenario #1: A list of user names are updated but their values within their date of user field remain the same. Each user within the list has only one value in the date of user field and does not contain ;
Evaluation:
- In this scenario, the line that states if(useridmap!=null && useridmap.size()>0 && u.DateOfUser__c!=useridmap.get(u.id).DateOfUser__c) will return false since value of the date of user field in the new record would be the same as the old record.
- Thus, the rest the code below it will be ignored.
Result: Nothing will happen
Scenario #2: A list of user names are updated but their values within their date of user field remain the same. Each user within the list has multiples values that are separated by ;
Evaluation:
- In this scenario, the line that states if(useridmap!=null && useridmap.size()>0 && u.DateOfUser__c!=useridmap.get(u.id).DateOfUser__c) will return false since value of the date of user field in the new record would be the same as the old record.
Result: Nothing will happen
Questions / Concerns: In the above the scenarios, were you expecting for nothing to happen intentionally?
Scenario #3: A list of users' date of field values changed. Each user within the list has multiples values that are separated by ;
Evaluation:
- In this scenario, the line that states if(useridmap!=null && useridmap.size()>0 && u.DateOfUser__c!=useridmap.get(u.id).DateOfUser__c) will return true since value of the date of user field in the new record would not be the same as the old record.
- Thus, the rest the code below it execute.
Result: A new account for each date separated by ; will be created and associated with that user.
Questions / Concerns: In this scenarios, this will occur each time the date values separted by ; within a user record is updated. Please note that this has the potential to cause duplicates. For example, a user's initial values upon insert for date of user may be 2/30/2020;4/05/2027. After the update, it's new value became 5/20/2020;4/05/2027. Your code executes and two accounts: one with 5/20/2020 and another with 4/05/2027 within its matching field. After the user's record is updated again, it's new value becomes 12/25/2020;4/05/2027. Noticed how only one of its dates have changed. Based on the code you've written, one account will be created with 12/25/2020 as its matching date and a duplicate account with 4/05/2027 will be created. Is this what you were expecting to occur? Just wanted to ensure.
Scenario #4: A list of users' date of field values changed. Each user within the list has only one value in the date of user field and does not contain ;
Evaluation:
- In this scenario, the line that states if(useridmap!=null && useridmap.size()>0 && u.DateOfUser__c!=useridmap.get(u.id).DateOfUser__c) will return true since value of the date of user field in the new record would not be the same as the old record.
- However, when the line executes: if(u.DateOfUser__c.contains(';')) it will return false since it does not contain ;.
- Thus, the rest of the code is ignored.
Result: Nothing will happen. Is this what you were expecting?
Scenario #5: A list of users' name change but don't have a value for date of user.
Evaluation:
- In this scenario, the line that states if(u.DateOfUser__c != null) will return false since there is no value in the date of user field.
- In this situation, the else code block will execute.
- However, when the line executes: nn.MatchingDates__c = date.valueof(u.DateOfUser__c), an error will occur since one can't pass a null value to it.
Result: Nothing will happen or an error will be returned. Is this what you were expecting?