function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sajidai SyedSajidai Syed 

how to write a trigger for this situtation

I have two objects one is a standard object i.e Account and the other one is a custom object and their have a look up relations .I have  created 3 text fields as A,B,C in  custom object.Now when I concatenate b/w A and B , the result should get displayed in C.when u type the value manually in field C ,it should throw the error.So which type of trigger is used and how wil u write the code
pankul guptapankul gupta
Instead of creating a trigger, you can create the C field as the Formula Field, with return type as "Text" and in the formula of C, you can write the concatenate logic of A & B like as below:
 
A + " " + B
Hope this resolves your query. Please revert if not.
Sajidai SyedSajidai Syed
if we use formula field and the output type as text then it will display in detail page right, but i want the field in edit page also when i'm trying to insert the value manually it has to through an error
 
Rahul.MishraRahul.Mishra
Hi Sajidai,

Here is the code for you, just update your custom object API name whereever I have used Teacher__c, then it will work for you:
 
trigger poPulateValue on Teacher__c (before insert, before update) {
    
    for (Teacher__c tc : trigger.new) {
        if(trigger.isInsert) {
                if(string.isNotBlank(tc.C__c)) {
                  tc.addError('You Can Not Insert Value in Field C Manually');
          }   
        } else {
            if(trigger.oldMap.get(tc.Id).C__c != tc.C__c)
             tc.addError('You Can Not Update the value Manually');
        }
    }
  
  
  
     for(Teacher__c tc : trigger.new) {
        if(string.isBlank(tc.A__c)){
            if(string.isNotBlank(tc.B__c))
              tc.C__c = tc.B__c;
             else 
              tc.C__c = '';
              
        } else if(string.isBlank(tc.B__c)) {
            if(string.isNotBlank(tc.A__c))
              tc.C__c = tc.A__c;
             else 
              tc.C__c = '';
        } else if(string.isNotBlank(tc.A__c) && string.isNotBlank(tc.B__c)) {
            tc.C__c = tc.A__c +' ' +tc.B__c;
        }
        
    }       
}

Mark Solved if it does help you.
Akshay_DhimanAkshay_Dhiman
Hi Syed
 
trigger triggerName on CustomObj__c (before insert, before update) {
    if(Trigger.isInsert){
  for(CustomObj__c obj : trigger.new){
   if(obj.fieldC__c == null){
    obj.fieldC__c=obj.fieldC__c+' '+fieldC__c;
   }else{
    obj.fieldC__c.addError('You can not enter manually in this Field.');
   }
   
  }
 }
 if(Trigger.isUpdate ){
  for(CustomObj__c obj : trigger.new){
   if(obj.fieldC__c == null){
    obj.fieldC__c=obj.fieldA__c+' '+fieldB__c;
   }else{
    if(trigger.oldMap.get(obj.Id).fieldC__c != obj.fieldC__c)
    obj.fieldC__c.addError('You can not update manually this Field.');
   }
   
  }
 }
}


if you found this answer helpful then please mark it as best answer so it can help others.   

Thanks 
Akshay