+ Start a Discussion
Rahul H 4Rahul H 4 

create a trigger to populate contact name with customer name

Hello,
I am looking to create a trigger to populate contact name (system field) with customer name in case entity.

trigger Images2_case on Case (before insert, before update) {
    for(Case c : Trigger.New) {
    string img = c.ContactId;

       if(ContactId=null){
 
               c.ContactId = Customer_Name__c;
        
        System.debug(img);
    }

    
    
}
}

however, its not working and giving error as Error: Compile Error: Condition expression must be of type Boolean: String at line 5 column 11

Can you please help. Thanks 
Best Answer chosen by Rahul H 4
Raj VakatiRaj Vakati
Try this
 
trigger Images2_case on Case (before insert, before update) {
List<Contact> cons =new List<Contact>();
Map<Id,String> maps = new Map<Id,String>();
    for(Case c : Trigger.New) {
        if(c.ContactId!=null){
       maps.put(c.ContactId ,c.Customer_Name__c);
     }
   } 

List<Contact> conList = [Select Id , LastName from Contact where Id in :maps.keySet()];
for(contact c : conList ){
c.LastName = maps.get(c.Id);
}

update conList  ; 

}

 

All Answers

Raj VakatiRaj Vakati
Try this code
 
trigger Images2_case on Case (before insert, before update) {
List<Contact> cons =new List<Contact>();
Map<Id,String> maps = new Map<Id,String>();
    for(Case c : Trigger.New) {
        if(c.ContactId=null){
       maps.put(c.ContactId , Customer_Name__c);
     }
   } 

List<Contact> conList = [Select Id , LastName from Contact where Id in :maps.keySet()];
for(contact c : conList ){
c.LastName = maps.get(c.Id);
}

update conList  ; 

}

 
Raj VakatiRaj Vakati
Try this pls
 
trigger Images2_case on Case (before insert, before update) {
List<Contact> cons =new List<Contact>();
Map<Id,String> maps = new Map<Id,String>();
    for(Case c : Trigger.New) {
        if(c.ContactId=null){
       maps.put(c.ContactId ,c.Customer_Name__c);
     }
   } 

List<Contact> conList = [Select Id , LastName from Contact where Id in :maps.keySet()];
for(contact c : conList ){
c.LastName = maps.get(c.Id);
}

update conList  ; 

}

 
Rahul H 4Rahul H 4
Thanks, But, same error ☹
Raj VakatiRaj Vakati
Try this
 
trigger Images2_case on Case (before insert, before update) {
List<Contact> cons =new List<Contact>();
Map<Id,String> maps = new Map<Id,String>();
    for(Case c : Trigger.New) {
        if(c.ContactId!=null){
       maps.put(c.ContactId ,c.Customer_Name__c);
     }
   } 

List<Contact> conList = [Select Id , LastName from Contact where Id in :maps.keySet()];
for(contact c : conList ){
c.LastName = maps.get(c.Id);
}

update conList  ; 

}

 
This was selected as the best answer
DyraSan tDyraSan t

hello, 

we tried these all. Its just not happening.
Again and again coming same error.

can someone please give a proper solution of it?

DyraSan tDyraSan t
Sujeet PatelSujeet Patel
Hello dyra San t

Raj's trigger is working perfectely as your requirement.
if You need more solution then you should add
below code at 13 number line
i hope this will help full for you 

c.FirstName=' ';
 
Rahul H 4Rahul H 4
thanks everyone, pleasure 
Rahul H 4Rahul H 4
Hello,

I modified the trigger 


trigger Images34_case on Case (before insert, before update) {
Case caseToAdd = new Case();
caseToAdd.subject = 'dean'; 
// caseToAdd.contact.name = 'Something'; 
insert caseToAdd; 
system.debug(caseToAdd); 
List<case> clist = new List<case>(); 
clist.add(caseToAdd); 
// finding the contact with which the contact name needs to be populated 
contact c= [select id from contact where name ='Customer_Name__c']; 
for( case ca : clist) 

//Associating contact Id 
ca.contactid=c.id; 

system.debug('kaka'+clist); 
database.DMLOptions dmo = new database.DMLOptions(); 
update (caseToAdd); 
}

There is no syntax error however, when I am trying to save the Case getting error as 


case: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Images34_case: maximum trigger depth exceeded Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert Case trigger event BeforeInsert: [] Trigger.Images34_case: line 5, column 1

can u pls help