0

Account has child Change_Request__c. Changes are made to account by Change_Request__c. changes are made depending on picklist field. when new email is to be inserted in account first it should check all accounts if present then show error else insert the email. this functionality is not working.

  trigger UpdateAccountSubscriptionEmail on Change_Request__c (after insert,after update) {

    map<Id, Account> mapAccount = new map<Id, Account>();
    List<Id> listIds = new List<Id>();
    public boolean error = false;


     for(Change_Request__c crchild : Trigger.new)
     {
        listIds.add(crchild.Lead_Merchant_Name__c); 

     }

     mapAccount = new Map<Id, Account>([SELECT id,Subscription_Email__c,RecordTypeId,(SELECT Id,Updated__c,Lead_Merchant_Name__c,New_Value__c,Request_Type__c 
                                            FROM Change_Requests__r) FROM Account]);

      Map<String,Schema.RecordTypeInfo> rtMap = Schema.SObjectType.Account.getRecordTypeInfosByName();
        Id rtLeadID =  rtMap.get('Lead').getRecordTypeId();                                     

     system.debug('mapAccount.values() -->'+mapAccount.values());                                       
     for(Change_Request__c CR : Trigger.new)
     {
        for(Account acc : mapAccount.values())
        {
          if(acc.Subscription_Email__c != null)
          { 

            if(trigger.isUpdate && trigger.newMap.get(CR.Id).New_Value__c != trigger.oldMap.get(CR.Id).New_Value__c && CR.New_Value__c.contains(acc.Subscription_Email__c) && CR.Request_Type__c == 'Contact Email')
            {
                if(CR.Updated__c == false){ 

                     system.debug('CR.New_Value__cc -->'+CR.New_Value__c);
                     system.debug('acc.Subscription_Email__c -->'+acc.Subscription_Email__c);   
                     error = true;
                     CR.New_Value__c.addError('<span><b>Duplicate Email found with the same Email</b></br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kindly update this email as alternate Email!</b></span>',false);
                //return null;
                }

            }
          } 
        }
     }
     //try{
     if(!error){
     for(Change_Request__c CR : Trigger.new)
     {  
         Account  myParentAcc = mapAccount.get(CR.Lead_Merchant_Name__c);
     if(myparentAcc.RecordTypeId == rtLeadID)
     {   
         if(CR.Request_Type__c == 'Contact Email'){
            system.debug('CR.Request_Type__c -->'+CR.Request_Type__c);
            myParentAcc.Subscription_Email__c = CR.New_Value__c;    
         }  
     }  
    }
     update mapAccount.values();    
     }    
    /* }catch(Exception e){
       system.debug('e-->' + e);
     }  */                       
   }

enter image description here

0

in your scenario there is same record in mapAccount.values();. Because for multiple child there can be same parent. You need to keep record of account object and remove duplicate account by assign them in set and then reassign them in list and then update the record.

  • I dint get exactly can you please customize the code? – SFDC shaikh Oct 27 '15 at 11:10

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.