function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Neeraj Sharma 103Neeraj Sharma 103 

How to use trigger for compare two custom field from two different custom object

Hi  
I am new in salesforce  i am beginner so please help me


Thanks in advance
 
I have two custom objects say custobj1 and custobj2. In custobj1, In custobj1, I have a date/time field say date_time_custobj1__C and in custobj2 I have another  field but its only date field say date__custobj2__c. Now I need to compare values in these two fields If One Field value is greater then another filed value thats the condition is i enter less value compare to other filed then its throughs the error using trigger   So Please Help me How to use trigger for this what i do write in trigger for this Scenario 
Best Answer chosen by Neeraj Sharma 103
chanchal_:)chanchal_:)
trigger CompareDates on TestObj1__c (before insert, before update) {
    set<string> setTstObj3 = new set<string>();
    for(TestObj1__c objTst1 : trigger.new)
        if(objTst1.TestObj3ID__c !=null){
            setTstObj3.add(objTst1.TestObj3ID__c);
        }
    map<string,TestObj3__c> mapIdWiseTstObj3 = new map<string,TestObj3__c>();
    for(TestObj3__c objTstObj3: [select id,dateTimeTestobj3__c from TestObj3__c where Id IN :setTstObj3]){
        mapIdWiseTstObj3.put(objTstObj3.Id,objTstObj3);
    }
    list<TestObj1__c> compareDates = new list<TestObj1__c>();
    for(TestObj1__c objTst1 : trigger.new)
    {
        if(objTst1.TestObj3ID__c != null){
            if(mapIdWiseTstObj3.containsKey(objTst1.TestObj3ID__c)){
                TestObj3__c objTst3 = mapIdWiseTstObj3.get(objTst1.TestObj3ID__c);
                if(objTst1.dateTimeTestobj1__c > objTst3.dateTimeTestobj3__c){     
                    objTst1.addError('date should be lesser');
                }
            }
        }
    }
}
This example is like, if a date/time field is created on account, and if a related contact is being created or updated. If value of date/time field on contact is greater than value of date/time field on account, it will throw an error.
 

All Answers

chanchal_:)chanchal_:)
trigger CompareDates on TestObj1__c (before insert, before update) {
    set<string> setTstObj3 = new set<string>();
    for(TestObj1__c objTst1 : trigger.new)
        if(objTst1.TestObj3ID__c !=null){
            setTstObj3.add(objTst1.TestObj3ID__c);
        }
    map<string,TestObj3__c> mapIdWiseTstObj3 = new map<string,TestObj3__c>();
    for(TestObj3__c objTstObj3: [select id,dateTimeTestobj3__c from TestObj3__c where Id IN :setTstObj3]){
        mapIdWiseTstObj3.put(objTstObj3.Id,objTstObj3);
    }
    list<TestObj1__c> compareDates = new list<TestObj1__c>();
    for(TestObj1__c objTst1 : trigger.new)
    {
        if(objTst1.TestObj3ID__c != null){
            if(mapIdWiseTstObj3.containsKey(objTst1.TestObj3ID__c)){
                TestObj3__c objTst3 = mapIdWiseTstObj3.get(objTst1.TestObj3ID__c);
                if(objTst1.dateTimeTestobj1__c > objTst3.dateTimeTestobj3__c){     
                    objTst1.addError('date should be lesser');
                }
            }
        }
    }
}
This example is like, if a date/time field is created on account, and if a related contact is being created or updated. If value of date/time field on contact is greater than value of date/time field on account, it will throw an error.
 
This was selected as the best answer
Neeraj Sharma 103Neeraj Sharma 103
Can you please explain setTstObj3 ,TestObj1__c objTst1 ,TestObj3ID__c  i dont  understand them
 
chanchal_:)chanchal_:)
TestObj1__c  - Custom Object's Api name, name is TestObj1
TestObj3ID__c   - lookup field in TestObj1 which cosists id of TestObj3__c 
setTstObj3 - set of Ids of TestObj3__c which arer associated with atleast 1 TestObj1__c .
 
Neeraj Sharma 103Neeraj Sharma 103
this code will not work when insert the value in the field the trigger will not fired  but trigger will save its not shows any error but  when insert the value in the field the trigger will be not fired
 
Neeraj Sharma 103Neeraj Sharma 103
Hi

Its Done  the Code will work its My Mistake  to place api name Now its Fine Its Shows Error


Thanku So Much Again




 
Madhavi B TMadhavi B T
Hi Chanchal 
It was good but can you describe the same if i have three objects Ob1 with field date1__c, Ob2 with field date2__c and Ob3 with field date3__c where date1__c > date2__c and date3__c.
Thanks in advance.