0

I am working on a project in "Developer Edition" of salesforce ... I want to create such mechanism so that whenever I create a record in a standard object , it would check a certain criteria ( custom field )in the custom object as well as the standard object and accordingly insert values in some of the fields of the standard object from the custom object. The standard object I wanna use is the "Account" one , and I came to know that I have to use tiggers for this and this is the code I could come up with until now:

trigger Info_from_Cust_to_Acc_main on Account (before insert) 
{
        List<Account> memberList = new List<Account>();
        List<Customer__c> customList = new List<Customer__c>();
         //Customer__c c = new Customer__c();

    for (Account member : Trigger.new)
    { Customer__c c = new Customer__c();
     system.debug(c); 
       if(c.Cust_Phone__c == member.Name)      
       {
           //list<Customer__c> d = [select Email__c from Customer__c] ;
           //List<Customer__c> customList = new List<Customer__c>();
           member.Cust_Email__c =  c.Email__c ;

system.debug(c);    
       }
        memberList.add(member);
    }

  // system.debug(memberList);
  system.debug(customList);

}

But the problem here is that , the Customer (i.e. custom ) object isn't fetching any record from the database. Please help me out!

  • 1
    try using before insert trigger. in the trigger you can check the criteria and decide whether to insert or not. – Anu Jul 26 '16 at 10:57
  • You need to loop through your Accounts and collect the Account.Name field in a Set or List. Then use that set to query your Customer__c object. Put those in a map with Cust_Phone__c as the key and the Customer__c object as the value. Then loop through your Accounts again, user the Name field to check the Map that holds the Customer__c object. If it exists, then you can populate the email. – dBeltowski Jul 26 '16 at 15:10
1

Customer__c c reference is assigned to New SObject, which will have null values. If condition [if(c.Cust_Phone__c == member.Name)] will not be evaluated to true. We should not be writing SOQL queries in for loop, it may exceed Governor Limits

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.