You need to sign in to do that
Don't have an account?

Guidance on writing a trigger
Hi all,
I'm learning triggers in salesforce and want some advice on understanding how to write a trigger for below scenario. what is the approach?
There are two objects Object1_c(Parent) and Object__c(child) having look-up relationship.
Summary:
Object__c has record types namely "A" and "B".
Object__c has a field (currency) --> Myamount__c
Object__c has 2 picklislts --> Picklist_a__c and Pciklist_b__c
object1__c has a field(curreny_ --> amount__c
Scenario:
when amount__c(on Object1__c) is blank or null, and if record type is either A||B and Picklst_a__c and picklist__b is selected then Myamount__c should be required show be the error.
Thanks in advance.
As per your scenario, when picklist b have value "Checked" and having child records related to that then trigger will fire to display error message , otherwise, No error will come.
Thanks
All Answers
What you need is a validation rule on Object__c to achieve this.
Here is your validation rule.
As you can see, I didn't include the Record Type here because you have only two record types and both are eligible for this validation.
Hope this helps.
it throws out an error that the unknown function Object1__r.Amount__c check spelling.
Hi,
Please use below code snippet : -
Here in this code,
1. lookupField refer to lookupRelationship field between two objects.
2. IdCorrespondingtoA refer to RecordType Id for A.
3. IdCorrespondingtoB refer to RecordTypeId for B.
Please Mark as best answer,if it works :)
Hi,
please check and do amendment accordingly -
Object__c : - Replace with API Name of child object.
Object1__c : - Replace with API Name of parent Object.
lookupfield : - Replace with relationshipField API
Could you please paste your code over here, ?
Thanks
Please let me know in case of confusion
Thanks
There are two objects ObjectPar__c(Parent) and ObjectCh__c(child) having look-up relationship.
Summary:
ObjectPar__c has record types namely "A" and "B".
ObjectPar__c has a field (currency) --> Myamount__c
ObjectPar__c has 2 picklislts --> Picklist_a__c and Pciklist_b__c
ObjectCh__c has a field(curreny_ --> amount__c)
Scenario:
when amount__c(on ObjectCh__c(childobject)) is blank or null, and if record type is either A||B and Picklst_a__c and picklist__b (on parent) is selected then Myamount__c should be required it cannot be blank
Hi, Below is code snippet : -
I'm learning triggers in salesforce and want some advice on understanding how to write a trigger for below scenario. what is the approach?
There are two objects ObjectPar__c(Parent) and ObjectCh__c(child) having look-up relationship.
Summary:
ObjectPar__c has record types namely "A" and "B".
ObjectPar__c has a field (currency) --> Myamount__c
ObjectPar__c has 2 picklislts --> Picklist_a__c and Pciklist_b__c( two values "checked" , "Received")
ObjectCh__c has a field(curreny_ --> amount__c)
Scenario:
if record type is either A||B and Picklst_a__c and picklist__b ("checked") (on parent) is selected then Myamount__c is not enter and record is saved, then user from the related list selects the child object and on the child object if amount__c(on ObjectCh__c(child object)) is blank or null, and saves the record and when user come to parent object and changes the Pciklist_b__c("Received ") then the trigger should fire.
Let me know if it works :)
Thanks
Use and instead of && in soql query : -
List<ObjectCh__c> lst_objCh = new List<ObjectCh__c>([Select id,Amount__c,lookupField fromObjectCh__c where Amount__c = null And lookupField IN:st_ParentId]);
What i have understood from you requirement till now is.
You want to show error on parent record. when it is created and child records Amount field is null.
Let me correct you, if parent is not created yet how can a child exist.
So as per my understanding you should show the error on child when it is created.
And mention Error on child record using merge field which you want to be filled before child is created.
OR if you say that it should be shown after both the records are created. And should show error when parent is updated.
Please use @rajat Maheshwari 6 code
please let me know if you want more help.
Thanks
Ajay Mishra
Email: mishraajay.m1@gmail.com
Thank you for quick turn around, code is not working properly, after checking all condition I found that even if I enter Myamount__c value trigger is firing, actually requirement is not.It should fire only if amount__c on child is null and Myamount__c has a value it should not fire the error
Please let me know, your testing once again.
Thanks
Begginer, please include this attribute in if condition :-
obj.createdDate > 2017-02-15T00:00:00Z
Here 2017-02-15T00:00:00Z refer to YYYY-MM-DDThh:mm:ssZ
Please mark as best answer, It it works, otherwise let me know the issue
Thanks
Rajat Maheshwari
could you please post your code over here, so that quickly I can make amended
{
Set<Id> st_ParentId = new set<id>();
for(ObjectPar__c obj : Trigger.new)
{
if(Trigger.oldMap.get(obj.id).Myamount__c != obj.Myamount__c )
st_ParentId.add(obj.ObjectRe__c);
}
List<ObjectCh__c> lst_objCh = new List<ObjectCh__c>([Select id,Amount__c,ObjectRe__c from ObjectCh__c where Amount__c = null && ObjectRe__c IN: st_ParentId]);
if(lst_objCh!=null && lst_objCh.size()>0)
{
for(ObjectPar__c ob : Trigger.new)
{
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && obj.Myamount__c==null && obj.createdDate > '2017-02-15T00:00:00Z')
ob.addError('Myamount__c should be required');
} }
}
}
Please remove quote, as this was the reason for reason.
Use this and let me know : -
trigger ObjectTrigger on ObjectPar__c(before update)
{
Set<Id> st_ParentId = new set<id>();
for(ObjectPar__c obj : Trigger.new)
{
if(Trigger.oldMap.get(obj.id).Myamount__c != obj.Myamount__c )
st_ParentId.add(obj.ObjectRe__c);
}
List<ObjectCh__c> lst_objCh = new List<ObjectCh__c>([Select id,Amount__c,ObjectRe__c from ObjectCh__c where Amount__c = null && ObjectRe__c IN: st_ParentId]);
if(lst_objCh!=null && lst_objCh.size()>0)
{
for(ObjectPar__c ob : Trigger.new)
{
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && obj.Myamount__c==null && obj.createdDate > 2017-02-15T00:00:00Z)
ob.addError('Myamount__c should be required');
} }
}
}
See above example and let me know. It will definitely help you.
Begginer, Your issue get solved or not ? If Not, then what the error you are facing ?
Note : FYI, I am taking Today Date which is 15 feb 2017
Thanks
comparison arguments must be compatible types: DateTime
Begginer, Here I am seing one mistake : -
for(ObjectPar__c ob : Trigger.new)
{
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && obj.Myamount__c==null && obj.createdDate > 2017-02-15T00:00:00Z)
ob.addError('Myamount__c should be required');
} }
}
Here for loop instance is "ob" and we are using "obj" in body of for loop.
Please use this as below : -
for(ObjectPar__c obj : Trigger.new)
{
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && obj.Myamount__c==null && obj.createdDate > 2017-02-15T00:00:00Z)
obj.addError('Myamount__c should be required');
} }
}
okie, Begginer,
If you want to use ob as earlier, then replace obj from body of forLoop by ob like below : -
for(ObjectPar__c ob : Trigger.new)
{
if(ob.Picklist_a__c!=null && ob.Picklist_b__c!=null && (ob.RecordTypeId==IdCorrespondingtoA || ob.RecordTypeId==IdCorrespondingtoB) && ob.Myamount__c==null && ob.createdDate > 2017-02-15T00:00:00Z)
ob.addError('Myamount__c should be required');
} }
}
Yeah sure @Begginer
Email Id : rajatzmaheshwari@gmail.com
There are two objects ObjectPar__c(Parent) and ObjectCh__c(child) having look-up relationship.
Summary:
ObjectPar__c has record types namely "A" and "B".
ObjectPar__c has 2 picklislts --> Picklist_a__c and Pciklist_b__c
ObjectCh__c
Scenario:
if record type is either A||B and Picklst_a__c and picklist__b ("checked") (on the parent) is selected and there are records on the child object then trigger should fire on the parent object.
if there are no records on child object then trigger should not fire.
As per your scenario, when picklist b have value "Checked" and having child records related to that then trigger will fire to display error message , otherwise, No error will come.
Thanks
02 {
03 Set<Id> st_ParentId = new set<id>();
04
05for(ObjectPar__c obj : Trigger.new)
06 {
07
08 st_ParentId.add(obj.lookupField);
09 }
10
11List<ObjectCh__c> lst_objCh = new List<ObjectCh__c>([Select id,lookupField from ObjectCh__c wherelookupField IN:st_ParentId]);
12
13
14
15if(lst_objCh!=null && lst_objCh.size()>0)
16 {
17
18
19for(ObjectPar__c obj : Trigger.new)
20 {
21
22 if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && obj.Picklist_b__c=='Checked' && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && (obj.Myamount__c == null || obj.Myamount__c == 0)
23
24 {
25 obj.addError('Myamount is required ');
26 }
27 }
28 }
29
30
31
32}
but when I enter Myamount the trigger is firing. can you help me on this?
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && obj.Picklist_b__c=='Checked' && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && (obj.Myamount__c == null || obj.Myamount__c == 0))
Thanks
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && obj.Picklist_b__c=='Checked' && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && (obj.Myamount__c == null || obj.Myamount__c == 0) && CreatedDate > '2017-02-14')
Begginer,
CreatedDate returns DateTime, so you need to compare createdDate with dateTime value,..Please follow below one : -
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && obj.Picklist_b__c=='Checked' && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && (obj.Myamount__c == null || obj.Myamount__c == 0) && CreatedDate > 2017-02-15T00:00:00Z)
Let me know if any issue comes
Begginer,
Use this :P It will help you :)
datetime startDate = datetime.newInstance(2017,02,15);
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && obj.Picklist_b__c=='Checked' && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && (obj.Myamount__c == null || obj.Myamount__c == 0) && CreatedDate > startDate )
Enjoy :D
Please let me know the issue again comes
Please try this. It may work for you.
Thanks
Ajay Mishra
Email: mishraajay.m1@gmail.com
Hi Begginer,
Does your issue get solved?
I used that query and its working fine
Begginer,
Below is points, please look over it : -
1. In query, YYYY-MM-DDThh:mm:ssZ works fine as , in your case It works :)
2. In If Conditional logic, What I have suggested, will work fine
Ex-
datetime startDate = datetime.newInstance(2017,02,15);
if(obj.Picklist_a__c!=null && obj.Picklist_b__c!=null && obj.Picklist_b__c=='Checked' && (obj.RecordTypeId==IdCorrespondingtoA || obj.RecordTypeId==IdCorrespondingtoB) && (obj.Myamount__c == null || obj.Myamount__c == 0) && CreatedDate > startDate )
Note : - Query return child Object fields , Please let me know, Whose createdDate you want to check either parent object created Date or child Object created Date ?
Please Mark as best answer, If it make sense.
Thanks
Begginer,
Please feel free to contact me on any issue on email Id : rajatzmaheshwari@gmail.com
in the object Adventure_Note__c, with the email in the field "Email_From_Requestor__c" which is in the Adventure__c . I am trying to use the code below, but I think I have an error since it does nothing.
trigger OnAdventureFields on Adventure__c (after insert, after update) {
map<string, Adventure__c> ObjMap = new map<string, Adventure__c>();
for(Adventure__c obj: Trigger.new){
if (obj.Email_From_Requestor__c != Null) {
ObjMap.put(obj.Email_From_Requestor__c, obj);
}
}
List<Adventure_Note__c> AdventureNotes = [SELECT Id, Adventure_Email_in_Note__c, Adventure_ID__c
FROM Adventure_Note__c WHERE Adventure_Email_in_Note__c IN :ObjMap.KeySet()];
List<Adventure_Note__c> NoteUpdateList = new List<Adventure_Note__c>();
for (Adventure_Note__c c: AdventureNotes){
Adventure__c obj = ObjMap.get(c.Adventure_Email_in_Note__c);
c.Adventure_Email_in_Note__c = obj.Email_From_Requestor__c;
NoteUpdateList.add(c);
if(NoteUpdateList.size() > 0){
update NoteUpdateList;
}
}
}