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
Sachin Kumar 34Sachin Kumar 34 

Object Fields data to be transfer to the another objects fields

Hi I want to transfer some of the field’s data from Contract to CARD (Custom) object fields
 
Contract fields
1) DR Onsite (Formula -> Currency)
2) HR Onsite (Formula -> Currency)
3) DR Offsite (Formula -> Currency)
4) HR Offsite (Formula -> Currency)
 
CARD Fields
1) Onsite DR (Currency)
2) Onsite HR (Currency)
3) Offsite DR (Currency)
4) Offsite HR (Currency)
 
We can create multiple CARD record for one contract, so I want to populate the above field’s data whenever new CARD record will create
 
Eg : For Contract 1 -> Card Record 1  (Want to populate the Contract 1 fields data into the Card Record1)
For Contract 1 -> Card Record 2 (Want to populate the Contract 1 field’s data into the Card Record2)
 
For Contract 2 -> Card Record 1 (Want to populate the Contract 2 field’s data into the Card Record 1)
For Contract 2 -> Card Record 2 (Want to populate the Contract 2 field’s data into the Card Record 1) and so on.
 
What is the trigger and test class for the above problem.
bob_buzzardbob_buzzard
Have you done any work on this yourself, come up with a design or approach and hit a problem? If you just want someone to write code according to your requirements you'll have more luck posting on the jobs board.
Sachin Kumar 34Sachin Kumar 34
Please find the below code.

trigger servicecontract on Contract (after insert) 
{
    Set<Id> Ids= new Set<Id>();
        for (Contract cont: Trigger.new)
        {
            Ids.add(cont.Id);       
        }
List <Contract> memberInsertList = [Select Id, Daily_Rate__c, Daily_Rate_Offsite__c from Contract where Id in :Ids]; 
    
    for (Contract cont : Trigger.new) {
    
        SFDC_Service_Timecard__c stc = new SFDC_Service_Timecard__c();    
        stc.Daily_Rate__c = cont.Daily_Rate__c;
        stc.Offsite_Daily_Rate__c = cont.Daily_Rate_Offsite__c;
           
        memberInsertList.add(cont);      
    }    
    insert memberInsertList;
 }
bob_buzzardbob_buzzard
That trigger is on contract rather than card. Based on your requirements you need an after insert trigger on card that retrieves the associated contracts and copies the data. The trigger you have posted is automatically creating a service card from the contract, so you can copy the details at that point.