+ Start a Discussion
Sachin Kumar 34Sachin Kumar 34 

Transfer field data from Contract to Custom object field

I want to transfer some of the field data from Contract to Service TimeCard (Custom object) field.

The same field in both the object. Once the contract is created and under that when the service timecard record is generated the (Daily rate and Daily rate offset) data will automatically transfer from Contract to my custom object field.

I had applied the below code.


trigger servicecontract on Contract (after insert) 
{
    
 Set<Id> Ids= new Set<Id>();
    for (Contract member : Trigger.new)
    {
       Ids.add(member.Id);       
    }

 List <Contract> memberList = new List <Contract> ([Select Id, Daily_Rate__c, Daily_Rate_Offsite__c from Contract where Id in :Ids]);    

    for(Contract cont : memberList)

    {
  
       SFDC_Service_Timecard__c member = new SFDC_Service_Timecard__c();
    
        member.Daily_Rate__c = cont.Daily_Rate__c;
        member.Offsite_Daily_Rate__c = cont.Daily_Rate_Offsite__c;
       insert member;
    }
 }
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi Sachin,

you need to change the code as follows.

 trigger servicecontract on Contract (after insert) {  
 
    list<SFDC_Service_Timecard__c> memberInsertList = new list<SFDC_Service_Timecard__c>();
    for (Contract cont : Trigger.new) {
        SFDC_Service_Timecard__c member = new SFDC_Service_Timecard__c();    
        member.Daily_Rate__c = cont.Daily_Rate__c;
        member.Offsite_Daily_Rate__c = cont.Daily_Rate_Offsite__c;
        member.parent__c = cont.Id; // You need to map the Id to map the child record to the parent parent__c will be your look up or M-D field                                                                   //on the SFDC_Service_Timecard__c object
       memberInsertList.add(member);      
    }    
    insert memberInsertList;
 }

Thanks,
Vijay

 
William TranWilliam Tran
What is the issue? It does not work? Any error message?

 List <Contract> memberList = new List <Contract> ([Select Id, Daily_Rate__c, Daily_Rate_Offsite__c from Contract where Id in :Ids]);    
 change to this:
 List <Contract> memberList = [Select Id, Daily_Rate__c, Daily_Rate_Offsite__c from Contract where Id in :Ids];    


how is the service card tied to the contact?   Do you need the relationship between the two? if yes, 
maybe something like this: 
member.contactid__c = cont.id;                  where contactid__c is the API name for the relationship
otherwise it should be okay.

thx.