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
JsinghroyJsinghroy 

Creation of a new trigger to update data in a related object

Hi All,

 

I am new to salesforce and not a developer(I am a BA) however I am in need of some code and I am sure it is possible in Salesforce. In SQL I could do this with an update query. 

 

I have recently installed an app called chargent which has an object called Orders.  I use person accounts and on the account I created a custom field to keep track of the customer status with a field called Status_C. 

 

This order has a relationship(lookup) to the account.  The key is that there is also a status on the order to manage recurring payments. What I wanted to is:

 

when the user changes the account.status_C = 'Canceled' then change the order_R.PaymentStatus_C = 'Stopped'

 

How would I go about this? Is there a trigger I can create to do this?

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Since this appears from your description to be a lookup relationship then a trigger would be required.

 

Basically, for a NON BULKIFIED trigger:

 

**How do you identify which order to to pull**

 

**Assuming the lookup field on Orders is Account__c

 

trigger updateChargeStatus on Account (before update){

 

Orders__c[] ord = [Select PaymentStatus__c From Orders__c Where Account__c = trigger.new[0].id AND PaymentStatus__c = 'STATUS TO FIND' Limit 1];

 

if(trigger.new[0].status__c == 'Canceled' && ord.isEmpty() != true){

          ord[0].PaymentStatus__c = 'Stopped';

          update ord;

}

 

}

All Answers

Starz26Starz26

Since this appears from your description to be a lookup relationship then a trigger would be required.

 

Basically, for a NON BULKIFIED trigger:

 

**How do you identify which order to to pull**

 

**Assuming the lookup field on Orders is Account__c

 

trigger updateChargeStatus on Account (before update){

 

Orders__c[] ord = [Select PaymentStatus__c From Orders__c Where Account__c = trigger.new[0].id AND PaymentStatus__c = 'STATUS TO FIND' Limit 1];

 

if(trigger.new[0].status__c == 'Canceled' && ord.isEmpty() != true){

          ord[0].PaymentStatus__c = 'Stopped';

          update ord;

}

 

}

This was selected as the best answer
JsinghroyJsinghroy
Thank you so much!!!! I think I got it working. Below is my final version. You rock!!!!
trigger updateChargeStatus on Account (before update, after update){

  ChargentOrders__ChargentOrder__c[] ord = [Select ChargentOrders__Payment_Status__c From ChargentOrders__ChargentOrder__c Where ChargentOrders__Payment_Status__c  = 'Recurring' AND Account__c = :trigger.new[0].ID limit 1];
 
 if(trigger.new[0].status__c == 'Cancelled' && ord.isEmpty() != true){
          ord[0].ChargentOrders__Payment_Status__c = 'Stopped';
          update ord;
}
 
 
}

 

Starz26Starz26

Great.

 

Now time to bulkify the trigger so you can handle multiple updates at once.