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
V1nitV1nit 

Cross Object Trigger to update a field

I am fairly new to apex.

I have two custom objects Obj1__c and Obj2__c. These two are related objects.

I have a field Status__c on Obj1__c which is a picklist with 5 values and I also have a field Status__c on Obj2__c which is a picklist again with 5 values.

 

Now, my question is, When the Status__c on Obj1__c is set to value4 or value5 then, I need to change the value of Status__c on Obj2 to value4 or value5 respectively. How can I do that using triggers? Any ideas?

 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

try this one

trigger updateRR on Opportunity__c(after update)
{
	try
	{	
		List<Resource_Request__c> lstRR=[Select r.Id,r.Status__c, r.Opportunity__c from  Resource_Request__c r where r.Opportunity__c in : Trigger.newMap.keyset()];
		for(Resource_Request__c objRR : lstRR)
		{
			if(Trigger.newMap.containskey(objRR.Opportunity__c) && ( Trigger.newMap.get(objRR.Opportunity__c).status__c == 'Withdrawn'|| Trigger.newMap.get(objRR.Opportunity__c).status__c == 'On Hold'))
			objRR.Status__c=Trigger.newMap.get(objRR.Opportunity__c).status__c;
		}
		update lstRR;
	}catch(Exception ex)
	{
		Trigger.new[0].addError('Error in resource request updates --'+ ex.getMessage());
	}
}

 

 

Thanks,

B

All Answers

b-Forceb-Force

Hi,

you need to build trigger when you are trying to update fields from related object.

 

If you come up with API name of object and fields of objects , We can help you to build trigger code.

 

Thanks

Bala

V1nitV1nit

Hi,

 

Thanks for the reply.

 

Opportunity__c is my main object and Resource_Request__c is a related list under the Opportunity__c object.

If I change the status on Opportunity__c to value4 or value5 then the status on the related Resource_Request__c record should change to the same value4 or value5

 

API names of Objects:

 

Opportunity__c

Resource_Request__c

 

API names of picklist fields:

 

Status__c (on Opportunity__c)

Status__c (on Resource_Request__c)

b-Forceb-Force

Does your Opportunity__c contains more than one Resource_Request__c records, 

In that case you want to change status of all records ?

 

Thanks,

 

V1nitV1nit

Yes, it might have multiple Resource_Request__c..... I want to change the Status on all the Related Resource_Request__c records.

 

Thanks,

V1nIt

b-Forceb-Force

what is API name of the relation ship field (Lookup to opportunity__c) on Resource_Request__c object ?

 

Thanks,

V1nitV1nit

The api name of the field is Opportunity__c

 

 

b-Forceb-Force

thanks for details

 

try this code

trigger updateRR on Opportunity__c(after update)
{
	try
	{	
		List<Resource_Request__c> lstRR=[Select r.Id,r.Status__c, r.Opportunity__c from  Resource_Request__c r where r.Opportunity__c in : Trigger.newMap.keyset()];
		for(Resource_Request__c objRR : lstRR)
		{
			if(Trigger.newMap.containskey(objRR.Opportunity__c))
			objRR.Status__c=Trigger.newMap.get(objRR.Opportunity__c).status__c;
		}
		update lstRR;
	}catch(Exception ex)
	{
		Trigger.new[0].addError('Error in resource request updates --'+ ex.getMessage());
	}
}

 

 

Hope this will help you ,

 

If you need any more help , let us know

 

Thanks,

B

V1nitV1nit

Works fantastic . Thanks for that. But I need the Status__c on Resource_Request__c to be changed only if the Status__c on Opportunity__c is changed to the values "On Hold" or "Withdrawn". 

b-Forceb-Force

try this one

trigger updateRR on Opportunity__c(after update)
{
	try
	{	
		List<Resource_Request__c> lstRR=[Select r.Id,r.Status__c, r.Opportunity__c from  Resource_Request__c r where r.Opportunity__c in : Trigger.newMap.keyset()];
		for(Resource_Request__c objRR : lstRR)
		{
			if(Trigger.newMap.containskey(objRR.Opportunity__c) && ( Trigger.newMap.get(objRR.Opportunity__c).status__c == 'Withdrawn'|| Trigger.newMap.get(objRR.Opportunity__c).status__c == 'On Hold'))
			objRR.Status__c=Trigger.newMap.get(objRR.Opportunity__c).status__c;
		}
		update lstRR;
	}catch(Exception ex)
	{
		Trigger.new[0].addError('Error in resource request updates --'+ ex.getMessage());
	}
}

 

 

Thanks,

B

This was selected as the best answer
V1nitV1nit

Great Thanks,

You saved my day. 

Working Perfect !

V1nitV1nit

To deploy this into production, do I need to have a test class ? If so what should be in the test class ?

lucy26lucy26

Hi B,

 

Can you please help me on the following functionality:

 

When  the Lead is conerted,I want the status of the Task  to automatically change to "Completed"

 

Kindly please help me with the trigger for this functionality.

 

Thankinh you in advance.

 

Regards

Lucy