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
ChazDadChazDad 

Cross Object Field Update Trigger

I simply want to update a date field in a CustomObj__c from Standard Contract Obj upon Save of the Standard Contract Obj

 

Example:

When the "PTO Date" is updated & Saved on the Contract obj - that updated Date should auto-fill into the Service_Start_Date field on the

Service Contract custom_Obj__c

 

This should be a simple trigger however, I'm a bit snagged on it - Help Please :) -  Thx in Advance !!

 

 

 

 

forecast_is_cloudyforecast_is_cloudy

What's the relationship between the Contract standard object and the 'Service Contract' custom object? Which one is the parent and which one is the child?

ChazDadChazDad

 

Hi - Thx for getting back on this - I actually asked my question erroneously:

 

My (correct) objective is to update a field on the [standard] Contract obj   from a value on a [custom] Project__c object.

 

Flow =  When a 'Status' picklist value on the [custom] Project__c  is selected to "PTO" - it requires a PTO Date (in an adjecent field).

That PTO date needs to auto-fill across to the [standard] Contract obj's  'Service_Start_Date' field when [custom] Project__c is Saved.

 

Now that I've corrected this question - any advice?  Thx Again!

forecast_is_cloudyforecast_is_cloudy

Since this is a Custom Object to Standard Object relationship, you can't use cross-object Field Updates using Workflow (the solution described here - http://developer.force.com/cookbook/recipe/updating-a-field-on-a-parent-record). I only mention that as an FYI and in case you have a similar requirement in the future (but with 2 custom objects). In your case, you'll have to write an Apex trigger. The following trigger on the Project__c object should work:

 

Note: I've made some assumptions about the field API names. Change as appropriate.

 

 

trigger updateServiceStartDate on Project__c(after update, after insert) {
    Integer i = 0;
    Set<ID> contractIds = new Set<ID>();
    Map<ID, Date> id2Date = new Map<ID, Date>();
    for (Project__c o : Trigger.new)
    {
        if ((Trigger.isInsert && o.PTO_Date__c != null) ||
            Trigger.old[i].PTO_Date__c != o.PTO_Date__c)
        {
            contractIds.add(o.Contract__c);
            id2Date.put(o.Contract__c, o.PTO_Date__c);
        }     
        i++;    
    }
    
    List<Contract> contracts = [select id  from Contract where id in :contractIds];
    
    for (Contract c : contracts)
    {
        c.Service_Start_Date__c = id2Date.get(c.Id);
    }
    
    update contracts;
}

 

 

LoserKidLoserKid

Im new to Apex and my first task has been to build a cross-obj trigger that goes for Campaign (standard) to CampaignItem (custom). I need to send the Item cost in the custom object to the actual cost in the standard object. I under stand that i must use a trigger but i am stuggling to grasp the concepts of apex. The big problem for me is i don't understand why you use all the Sets, Maps, Lists and loops. All i want to do is say this value here eqauls this value over here when the trigger fires. All i know is that it comes donw to Governing limits. Can you help, is the a place i can go that really explains the logic behind a Cross-Ojbect trigger.  

 

 

hello_im_herehello_im_here

Hello - 

 

This is very close to what I'm trying to do! 

 

Could you point me in the right direction to tweak this code for the following?

 

I'm trying to update a field on a parent account when an opportunity is marked as 'Closed Won'. 

 

Thanks

hunterhunter

I have used your example to create a similar trigger.  (Thank you.)   However, I have one slight difference.  

When my Transactions__c Status__c changes to any value except "Cancel", the Status__c field on the linked Object (Llistings__c) will change to that same value.  (ie Trans.Status__c = Pending, Linked_Listing__c.Listing_Status__c = Pending).

 

However, when the Transaction Status equals "Canceled", the Listing Status needs to change to "Active"...provided the Linked_Listing__c field on the Transactions__c Object is not null.

 

I can get the first part of my code to work.  The second half, Trigger.isUpdate, portion does not work.  What am I missing?

 

 

trigger NEW_updateListingfromSales on Transactions__c (after update, after insert)
{
if(Trigger.isInsert)
{
Integer i = 0;
Set<ID> ListingsIds = new Set<ID>();
Map<ID, String> id2Status = new Map<ID, String>();

for (Transactions__c o : Trigger.new)
{
if (o.Status__c <> 'Canceled' || Trigger.old[i].Status__c != o.Status__c)
{
ListingsIds.add(o.Linked_Listing__c); id2Status.put(o.Linked_Listing__c, o.Status__c);
}
}

List<Listings__c> listings = [select id from Listings__c where id in :ListingsIds];

for (Listings__c l : listings)
{
l.Listing_Status__c = id2Status.get(l.Id);
}

i++;
}

else

{
if(Trigger.isUpdate)
{
Integer i = 0;
Set<ID> ListingsIds = new Set<ID>();
Map<ID, String> id2Status = new Map<ID, String>();

List<Listings__c> listings = new List<Listings__c>();

for (Transactions__c o : Trigger.old)
{
if (o.Status__c == 'Canceled' || Trigger.old[i].Status__c != o.Status__c)
{
ListingsIds.add(o.Linked_Listing__c); id2Status.put(o.Linked_Listing__c, 'Active');
}
}

List<Listings__c> listings = [select id from Listings__c where id in :ListingsIds];

for (Listings__c l : listings)
{
l.Listing_Status__c = id2Status.get(l.Id);
}

}
}
update listings;
}