0

I've a child object Dr_Invite__c, and parent object Lead.

My requirement is when i create a record for Dr_Invite__c, my trigger will look for the same record is available or not (same record by fpn id field ...). suppose same record is available with name X ,

attach the newly created Dr_Invite__c record to respective Lead record of X.

Lead is parent object to all existing Dr_Invite__c object record.

I've to write a bulk trigger, I've written a code but it not working as required.

trigger ContactAfterInser1t on Dr_Invite__c (before insert, before update) {
    list<Dr_Invite__c> aList = new list<Dr_Invite__c>();

    Set<Id> DrId = new Set<Id>();
    Set<String> EntityId = new Set<String>();
    Set<String> NPISet = new Set<String>();
    Set<String> PECOSID = new Set<String>();
    List<Id>  LeadId= new List<Id>();
    List<Dr_Invite__c>  updaetDrInvite= new List<Dr_Invite__c>();

    for(Dr_Invite__c drIn:trigger.new){
        EntityId.add(drIn.fpn_entity_id__c); 
        NPISet.add(drIn.npi__c);
        PECOSID.add(drIn.pecos_group_pac_id__c);    
    }

    List<Dr_Invite__c> DrinviteList = [
        select id,lead__c,dr_name__c,fpn_entity_id__c,npi__c,pecos_group_pac_id__c 
        from Dr_Invite__c 
        where (fpn_entity_id__c IN :EntityId OR npi__c IN :NPISet OR pecos_group_pac_id__c IN :PECOSID) 
        limit 1];

    if(DrinviteList.size()>0)
    {
        if(trigger.isBefore && Trigger.isupdate)
        {
            for(Dr_Invite__c drIn:trigger.new)
            {   
                for(Dr_Invite__c c:DrinviteList) 
                {    
                    if(c.fpn_entity_id__c==drIn.fpn_entity_id__c || c.npi__c == drin.npi__c || c.pecos_group_pac_id__c==drin.pecos_group_pac_id__c)
                    {
                        drIn.lead__c=c.lead__c;
                    }
                }
            }
        }
    }
}

Please check my updated, it does not update field when we create record, but update it when we edit it.

  • why dont you move all code base in to before update/create.You dont need any additional dml operation since your trigger is on same object. – erkan çipil Mar 10 '17 at 11:50
0
  1. Use before insert, before update trigger and do not use DML operations.
  2. Use map to find appropriate record.
  • I want bulk trigger, so it will be bulk? – Amol Mar 10 '17 at 12:39
  • yes, but it is not clear how leads should be assigned to Dr_Invite__c . Is there any field on Lead for matching? Currently it would not work because if we have two Dr_Invite__c records with the same fpn id field and we update one record and this record doesn't have a lead then you are trying to remove leads from all records with the same fpn id field, but this doesn't make sense to me, this logic appears to be wrong – Patlatus Mar 10 '17 at 12:49
  • Please check my updated code. It does not update field when we create record, but it update field when we edit the record. Please guide – Amol Mar 10 '17 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.