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
Zach BratcherZach Bratcher 

Need Help Writing my first trigger

I currently have a custom field on the Contacts Object called Field Lable: Colleague ID API Name: TargetX_SRMb__Colleague_ID__c that I need when it gets filled in to trigger and copy the data from Colleague ID field and add it to the Field Lable: SRM ETL ID API Name: TargetX_SRMb__SRM_ETL_ID__c on the Application Object
Best Answer chosen by Zach Bratcher
SHRAVAN K 6SHRAVAN K 6
Zach Bratcher- Kindly close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

All Answers

SHRAVAN K 6SHRAVAN K 6
How is contact and application object related? can you share schema?
Zach BratcherZach Bratcher
Looks like Application is connected by Student to the Mater-Detail(contact). Let me know if this is what you were asking for?User-added image
Raj VakatiRaj Vakati
You can able to do it using the process builder  and i dnt think so you need the trigger .. can you check once 
Zach BratcherZach Bratcher
I was looking at the Process builder but the process builder I couldn't figure out how get it to use contacts. It seems to only want to use Objects like the Application Object. 
SHRAVAN K 6SHRAVAN K 6
Can try this but please use standard features. 
trigger contactTrigger on Contact (before update) {

    set<id> d= new set<id>();
    list<application__c> lst= new list<application__c>();
    map<id,contact> l= new map<id,contact>();
    for(contact c:trigger.new){    
        if(c.CleanStatus != trigger.oldmap.get(c.id).CleanStatus && c.CleanStatus=='test'){
            d.add(c.id); 
            l.put(c.id,c);           
       } 
      if(d.size()>0){
         for(application__C c:[select id,TargetX_SRMb__SRM_ETL_ID__c from application__C where relationshipnameField=:d]){
             c.TargetX_SRMb__SRM_ETL_ID__c = l.get(c.relationshipid(contactrelated)).TargetX_SRMb__Colleague_ID__c;
             lst.add(c);
         }
         if(lst.size()>0){
             update lst;
         }
      }  
    }

}
Zach BratcherZach Bratcher
Using that code I get the error message Error: Compile Error: Duplicate variable: c at line 12 column 29  which is for(application__C c:
Ramesh DRamesh D
@Zach Rename the varable 'c' to unique which doesnt already exists 
 for(application__C app:[select id,TargetX_SRMb__SRM_ETL_ID__c from application__C where relationshipnameField=:d]){
             app.TargetX_SRMb__SRM_ETL_ID__c = l.get(app.relationshipid(contactrelated)).TargetX_SRMb__Colleague_ID__c;
             lst.add(app);
         }
SHRAVAN K 6SHRAVAN K 6
@Zach- Any other issues?
 
Zach BratcherZach Bratcher
I already had a trigger called contactTrigger so I updated that to contactUpdateAppTrigger and updated the code to be.

trigger contactUpdateAppTrigger on Contact (before update) {

    set<id> d= new set<id>();
    list<application__c> lst= new list<application__c>();
    map<id,contact> l= new map<id,contact>();
    for(contact c:trigger.new){    
        if(c.CleanStatus != trigger.oldmap.get(c.id).CleanStatus && c.CleanStatus=='test'){
            d.add(c.id); 
            l.put(c.id,c);           
       } 
      if(d.size()>0){
         for(application__C app:[select id,TargetX_SRMb__SRM_ETL_ID__c from application__C where relationshipnameField=:d]){
             app.TargetX_SRMb__SRM_ETL_ID__c = l.get(app.relationshipid(contactrelated)).TargetX_SRMb__Colleague_ID__c;
             lst.add(app);
         }
         if(lst.size()>0){
             update lst;
         }
      }  
    }
    }

But now getting the error Error: Compile Error: Invalid type: application__c at line 4 column 5 which line 4 is list<application__c> lst= new list<application__c>();
Zach BratcherZach Bratcher
would application__C actually need to be changed to TargetX_SRMb__Application__c that is the API Name
SHRAVAN K 6SHRAVAN K 6
yes you may have to use tha object api name..
Zach BratcherZach Bratcher
after i mad that changed it gives me Error: Compile Error: Variable does not exist: CleanStatus at line 7 column 14
Ramesh DRamesh D
Make sure the field exists on conatct and you use API name for "CleanStatus" 
Here is the link how you can find it https://help.salesforce.com/articleView?id=000007594&type=1

Thanks
Zach BratcherZach Bratcher
I don't have a field called CleanStatus on my contacts.  Here is a list of all the C fields I have on contacts.

Contact OwnerOwnerLookup(User) 

Contact Record TypeRecordTypeRecord Type 

Created ByCreatedByLookup(User)  

ReplaceCampus       TargetX_SRMb__Campus__c    TX Recruitment Manager     Picklist 

CEEB Code          TargetX_SRMb__ceeb_code__c    TX Recruitment Manager        Text(6)  

Citizenship                  hed__Citizenship__c                                HEDA                              Picklist  

Citizenship                 TargetX_SRMb__Citizenship__c     TX Recruitment Manager      Picklist  

Colleague ID                   TargetX_SRMb__Colleague_ID__c      TX Recruitment Manager Text(255) (External ID) (Unique Case Insensitive) 

College              TargetX_SRMb__College__c             TX Recruitment Manager                    Picklist  

Community User            TargetX_Base__Community_User__c            TargetX Base             Text(25)  

Concentration         TargetX_SRMb__Concentration__c           TX Recruitment Manager          Picklist  

Contact Owner Home Campus                  Contact_Owner_Home_Campus__c                      Formula (Text) 

Country of Origin              hed__Country_of_Origin__c          HEDA                       Picklist

Country of Permanent Residence     TargetX_SRMb__Country_of_Permanent_Residence__c       TX Recruitment Manager  Text(255)  

Current Address        hed__Current_Address__c        HEDA                          Lookup(Address) Account 

Current Campus       Current_Campus__c              Picklist 

Current County or Parish      TargetX_SRMb__Current_County_or_Parish__c     TX Recruitment Manager      Text(50)  

Current Major       Current_Major__c               Text(250)  
Ramesh DRamesh D
Where is that field coming from then? 
 if(c.CleanStatus != trigger.oldmap.get(c.id).CleanStatus && c.CleanStatus=='test'){

Must be from different object make sure you use it from right object

Thanks
Zach BratcherZach Bratcher
Ramesh,
Thanks for the Help but I was able to find a way through the Process builder to get it to work like I needed.
SHRAVAN K 6SHRAVAN K 6
Zach Bratcher- Kindly close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
This was selected as the best answer
Zach BratcherZach Bratcher
Shravan,
How do I go through and mark it as solved. I am not seeing anything that gives me the ability to do so.