+ Start a Discussion
badri nath 9badri nath 9 

upadet child record from parent record

I have parent object plan__c when i update name field in plan__c, trigger it will update child object process__c fields ,the tigger is below ,it's not working properly.....,
trigger trgr_plan on plan__c (after insert) {

{
    List<process__c> prcList = new List<process__c>();

    for(plan__c pcc:Trigger.newMap.Values())
    {
    process__c prc=new process__c ();
      prc.name=pcc.name;
      prc.processnewname__c=pcc.name;
        prcList .add(prc);
    }
   if(prcList .size()>0)
    {
        insert prcList ;
    }
}
}
Best Answer chosen by badri nath 9
Maharajan CMaharajan C
Try this trigger:

Trigger trgr_plan on plan__c (after insert){
        
        List<process__c> ToBeInserted=new List<process__c>();
        
    for(plan__c Recordcreate: trigger.new){
        ToBeInserted.add(new process__c(
                                    name=Recordcreate.Name,
                                    processnewname__c=Recordcreate.Id
                               
                            ));
    }
    
    try{
    if(ToBeInserted != Null){
        insert ToBeInserted;
    }
    }Catch(Exception e){
        System.debug('Exception ****'+e.getMessage());
    }
}

​Let me know if it works or not!!!
 

All Answers

Maharajan CMaharajan C
Hi Badrinath,

Try the simple below Trigger:

trigger trgr_plan on plan__c (after insert) {
for(plan__c pcc:Trigger.new){
process__c prc=new process__c ();
prc.name=pcc.name;
prc.processnewname__c=pcc.name;
insert prc;
}
}

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Badri,

 

Please try below Trigger;

 

trigger trgr_plan on plan__c (after update) {
    set<Id> planIds = new set<Id>();
    map<Id, plan__c> mapPlan = new map<Id, plan__c>();
    List<process__c> prcList = new List<process__c>();
    
    for(plan__c pl : trigger.new) {
        planIds.add(pl.Id);
        mapPlan.put(pl.Id, pl);
    }
    
    prcList = [SELECT Name FROM Process__c WHERE Plan__c IN : planIds];
    
    if(prcList.size() > 0) {
        for(Process__c pc : prcList) {
            pc.Name = mapPlan.get(pc.Plan__c).Name;
        }
        update listContact;
    }
}
badri nath 9badri nath 9
HI Deepak Maheshwari 7 it's for after update, but iam looking for after insert,



Any Answer was help full.....Thank you
 
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Badri,

 

When you will insert parent record Plan__c then at that time there would not be any child record Process__c.

 

Please confirm you want that if any child record Process__c is inserted then Name field should be auto populated by parent Plan__c name.

 

Then trigger will be on the Process__c not on Plan__c.

badri nath 9badri nath 9
Hi Deepak Maheshwari 7, 
trigger trgr_plan on plan__c (after update) { set<Id> planIds = new set<Id>(); map<Id, plan__c> mapPlan = new map<Id, plan__c>(); List<process__c> prcList = new List<process__c>(); for(plan__c pl : trigger.new) { planIds.add(pl.Id); mapPlan.put(pl.Id, pl); } prcList = [SELECT Name FROM Process__c WHERE Plan__c IN : planIds]; if(prcList.size() > 0) { for(Process__c pc : prcList) { pc.Name = mapPlan.get(pc.Plan__c).Name; } update listContact; } }

error as below:

No such column 'Plan__c' on entity 'process__c'. If you are attempting to use a custom field.
 plan__C is an custom object.....right.........
Maharajan CMaharajan C
Hi Badri,

Try the Below trigger in Plan Object and let me know:

Trigger trgr_plan on plan__c (after insert){
        
        List<process__c> ToBeInserted=new List<process__c>();
        
    for(plan__c Recordcreate: trigger.new){
        ToBeInserted.add(new process__c(
                                    name=Recordcreate.Name,
                                    processnewname__c=Recordcreate.Name
                               
                            ));
    }
    
    try{
    if(ToBeInserted != Null){
        insert ToBeInserted;
    }
    }Catch(Exception e){
        System.debug('Exception ****'+e.getMessage());
    }
}

​Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
Deepak Maheshwari 7Deepak Maheshwari 7
Please put the Api name of look up field of Plan on Process.
Maharajan CMaharajan C
Badri,Did you have any chance to try my triggers? 

Thanks,
​Raj
badri nath 9badri nath 9
Hi Maharaja C,
and your Triggercauses dml exception Error as below,
 Invalid id: planname1:  when i entering name field value as planename1..........,

Any Answer is HElpFul.........

customobject: plan__c
fields:name;
childobject:process__c
fields:name,processname__c;(Lookup)
if  i update name in plan__c it will auto populate fields name,processname__c;(Lookup)>>>>ok i.e Requirement....


 
badri nath 9badri nath 9
sry it was like belowi
f  i insert name in plan__c it will auto populate fields name,processname__c(Lookup)>>>>ok i.e Requirement....
Maharajan CMaharajan C
processname__c(Lookup) is child of plan__c right?

You have to create process reccord when plan__c record is inserted right?

Populate the processname__c(Lookup) with plan__c Id?
 
badri nath 9badri nath 9
ya right Maharaja C..,
Maharajan CMaharajan C
Try this trigger:

Trigger trgr_plan on plan__c (after insert){
        
        List<process__c> ToBeInserted=new List<process__c>();
        
    for(plan__c Recordcreate: trigger.new){
        ToBeInserted.add(new process__c(
                                    name=Recordcreate.Name,
                                    processnewname__c=Recordcreate.Id
                               
                            ));
    }
    
    try{
    if(ToBeInserted != Null){
        insert ToBeInserted;
    }
    }Catch(Exception e){
        System.debug('Exception ****'+e.getMessage());
    }
}

​Let me know if it works or not!!!
 
This was selected as the best answer