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
Mohan SFDC 25Mohan SFDC 25 

How to update the field value which is not direct related

I have a requirement. I have a object obj1 and another obj obj2 which are not direct related. 
obj1 is related to contact and contact is related to obj2. so now my requirement is 
suppose in obj1 if I am inserting a field f1 , then in obj 2 field2 should add the value which already exists and display the total value.
that means suppose in field f1 contains 100 value and obj2 field2 contains 200 values then it should update the field2 values by adding field1 value and display the total value.
Please try to help me.
Thanks in advance
Mohan SFDC 25Mohan SFDC 25
Note : I need to write an apex class not a trigger
Patcs_1Patcs_1
Hi Mohan

You can use process builder to call the apex class when some event hs occured. so when you insert some value in field1 of obj1 you have to update the vlaue in the obj2 field 2. you can use process builder which will be invoked when you insert value in field 1 and obj1 and then you can call the apex class to do the update in obj2.

below is the link for your refernce.

https://help.salesforce.com/articleView?id=process_action_apex.htm&language=en_US&type=0

Thanks

Please mark this as a solution, if the above reply answered to your question.
Mohan SFDC 25Mohan SFDC 25
Thank you for the reply, but I have to devlop this in apex class itself. I am not suppose to use any process builder and all.
Mohan SFDC 25Mohan SFDC 25
I have already started a method in my already existing apex class but got stuck in middile.
public static void exapme(List<obj1> lstobj1){
		List<obj1> lstobj= new List<obj1>();
        Set<Id> set1= new Set<Id>();
        Set<Id> set2= new Set<Id>();
        List<obj2> lstobj2= new List<obj2>();
		if(lstobj2.size()>0){
            for(obj1trans :lstobj1){
                if(lstobj1.contactfield!= null) 
                set1.add(trans.id); 
                set2.add(trans.contactfield);
            }
        }
		Map<id,obj1> mapobj = new Map<id,obj1>();
		for(obj1 obj: [Select id, (Select id, field1from obj1__r)from contact]){
		obj.put(field2);
		Decimal dollar = dollar+field1;
	
		}
		update dollar;