Wednesday, July 5, 2017

Trigger scenario2 in salesforce :

Account - Account Number -india
countrty__c - name(Text) and Checkbox__c  --
insert contact. 

While inserting Account field account number value is 'India' then compare to without related object Country__c ,and have field name value 'India' and  
Checkbox__c  ='true'
Then insert the contact. If doesn't have 
field name value 'India' and  Checkbox__c  ='true' doesn't insert the contact.

Trigger triggernameForPorton on account(after insert){
   list<contact> lstcon = new list<contact>();
   set<string> sbc = new set<string>();
   Map<string,string> mapc = new map<string,string>();
    if(trigger.isinsert && trigger.isAfter){
     for(account acc : trigger.new){
       sbc.add(acc.accountnumber);
     }
    }
    for(country__c c: [select id,name,Checkbox__c from Country__c where name IN:sbc and Checkbox__c =true ]){
        mapc.put(c.name,c.name);
    }   
   for(account acc : trigger.new){
    if(mapc.containsKey(acc.accountnumber)) {
        contact con = new contact();
        con.lastname =  acc.name;
        con.phone = '8867414079';
        con.email ='test@gmail.com';
        con.AccountId = acc.id;
        lstcon.add(con);
}
  }
    if(lstcon !=null){
        insert lstcon;
    }
}

Auto populate a  field Email "Email " on contact  with an  custom Email field "EMAIL__c" from the Account object or possibliy from Account Object itself, needs to happen when a user selects the account and saves the record.


trigger childtoparent on Contact (before update) {
    set<id> accids = new set<id>();
    for(contact con : trigger.new){
        if(con.Status__c == 'Active'){
        accids.add(con.AccountId);
        }
    }
    map<id,account> mapacc = new map<id,account>([select id,name,industry,email__c from account where id IN: accids  ]);
    for(contact con : trigger.new){
        if(mapacc.get(con.accountid).industry == 'Banking'){
        if( mapacc.containsKey(con.accountid)){
        con.email = mapacc.get(con.AccountId).email__c;
        }
    }
    }
}

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger childtoparent caused an unexpected exception, contact your administrator: childtoparent: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.childtoparent: line 10, column 1



trigger childtoparent on Contact (before update) {
    set<id> accids = new set<id>();
    for(contact con : trigger.new){
        if(con.Status__c == 'Active'){
        accids.add(con.AccountId);
        }
    }
    map<id,account> mapacc = new map<id,account>([select id,name,industry,email__c from account where id IN: accids and industry = 'Banking'  ]);
    for(contact con : trigger.new){
       // if(mapacc.get(con.accountid).industry == 'Banking'){
        if( mapacc.containsKey(con.accountid)){
        con.email = mapacc.get(con.AccountId).email__c;
        }
  //  }
    }
}

No comments:

Post a Comment