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
AkshuAkshu 

how to clone related record of one object and copied to another realted object using trigger

Best Answer chosen by Akshu
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Akshu,

Can you try the below trigger on ship object.
 
trigger PorttoShipment on Ship__c (after update) {
    
    set<id> shipid= new set<id>();
    map<id,id> mapis= new map<id,id>();
    List<Shipment__c> updateship= new List<Shipment__c>();
    for(Ship__c s:Trigger.new){
        Ship__c oldship=Trigger.oldmap.get(s.id);
        if(s.shiptoport__c!=null && s.shiptoport__c!=oldship.shiptoport__c){
           // portid.add(s.shiptoport__c);
           shipid.add(s.id);
            mapis.put(s.id,s.shiptoport__c);
        }
        
    }
    
    if(shipid.size()>0){
        for(Shipment__c objCS :[select id,llokupToport__c,lookupToship__c from shipment__c where lookupToship__c in :shipid]){
          //mapship.put(objCS.ookupToship__c, objcs)  ;
            
            if(mapis.containsKey(objcs.lookupToship__c)){
                objCS.llokupToport__c=mapis.get(objcs.lookupToship__c);
                updateship.add(objCS);
            }
        }

    }
    if(updateship.size()>0){
        update updateship;
    }
    
    
    

}

If this solution helps, please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Akshu,

Can you please let the community know what excatly you need so it would be easy to answer that. 

By just what is in the question it is difficult to answer.

Please check if the below question is soomething which you are looking for.

https://salesforce.stackexchange.com/questions/327037/how-to-clone-indirectly-related-record-using-apex-trigger

If this solution helps, Please mark it as best answer.

Thanks,
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Akshu,

Can you try the below trigger on ship object.
 
trigger PorttoShipment on Ship__c (after update) {
    
    set<id> shipid= new set<id>();
    map<id,id> mapis= new map<id,id>();
    List<Shipment__c> updateship= new List<Shipment__c>();
    for(Ship__c s:Trigger.new){
        Ship__c oldship=Trigger.oldmap.get(s.id);
        if(s.shiptoport__c!=null && s.shiptoport__c!=oldship.shiptoport__c){
           // portid.add(s.shiptoport__c);
           shipid.add(s.id);
            mapis.put(s.id,s.shiptoport__c);
        }
        
    }
    
    if(shipid.size()>0){
        for(Shipment__c objCS :[select id,llokupToport__c,lookupToship__c from shipment__c where lookupToship__c in :shipid]){
          //mapship.put(objCS.ookupToship__c, objcs)  ;
            
            if(mapis.containsKey(objcs.lookupToship__c)){
                objCS.llokupToport__c=mapis.get(objcs.lookupToship__c);
                updateship.add(objCS);
            }
        }

    }
    if(updateship.size()>0){
        update updateship;
    }
    
    
    

}

If this solution helps, please mark it as best answer.

Thanks,
 
This was selected as the best answer