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
Michael WebbMichael Webb 

Trigger not working.. (new to dev)

I have a trigger all I want to do is make field a equal to field b when the document is saved.  The first feild (name) which is the standard feild is a required field.  When I try to save this it says needs = in line 2 column 30.  I dont know what I am doing wrong. Thank you for your help!

Trigger updateFields on  McLabs2__Property__c (before update) {
     for (McLabs2__Property__c : trigger.new){
     Name = McLabs2__Property_Address__c;
    }
}
Best Answer chosen by Michael Webb
Vatsal KothariVatsal Kothari
Hi Michael,

Please Refer below code:

Trigger updateFields on  McLabs2__Property__c (before insert,before update) {
	
	if(Trigger.isInsert){
		for (McLabs2__Property__c a: trigger.new){
			a.Name = a.Property_Name__c;
		}
	}
	
	if(Trigger.isUpdate){
		for (McLabs2__Property__c a: trigger.new){
			McLabs2__Property__c property = Trigger.olMap.get(a.Id);
			if(propert.Property_Name__c != a.Property_Name__c){
				a.Name = a.Property_Name__c;
			}
		}
	}		
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Michael WebbMichael Webb
Made a change, now I am getting the following error.   Compile Error: Invalid field McLabs2_Property_Address__c for SObject McLabs2__Property__c at line 3 column 15, however, I copied the api name directly from the fields on the Property Object.

Trigger updateFields on  McLabs2__Property__c (before update) {
     for (McLabs2__Property__c a: trigger.new){
     a.Name = a.McLabs2_Property_Address__c;
    }
}
Michael WebbMichael Webb
I guess I should tell you what I am trying to accomplish.  I am trying to on save or anytime there is a change in the Property_Name__c field to copy the contents of that field into the Name field.  As I said this is a required field.  I thought I had it so many different ways but it just isnt' working.  Thanks
Vatsal KothariVatsal Kothari
Hi Michael,

Please Refer below code:

Trigger updateFields on  McLabs2__Property__c (before insert,before update) {
	
	if(Trigger.isInsert){
		for (McLabs2__Property__c a: trigger.new){
			a.Name = a.Property_Name__c;
		}
	}
	
	if(Trigger.isUpdate){
		for (McLabs2__Property__c a: trigger.new){
			McLabs2__Property__c property = Trigger.olMap.get(a.Id);
			if(propert.Property_Name__c != a.Property_Name__c){
				a.Name = a.Property_Name__c;
			}
		}
	}		
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

This was selected as the best answer
Michael WebbMichael Webb
Hmm I keep geting the following Error: Compile Error: Variable does not exist: Trigger.olMap at line 9 column 45, which is this right here "= T" on your line 10.  Thanks!!


Vatsal KothariVatsal Kothari
Sorry Michael, Its typo.
I missed 'd' in oldMap.
Replace it with below line:

McLabs2__Property__c property = Trigger.oldMap.get(a.Id);
Michael WebbMichael Webb
Vastal,

That worked if I broke it up into 2 triggers, kept telling me I had a duplicate variable, I will continue to work on that error and try to get them both in the dame trigger.  One Question though, Name is a required field, is there any way to populate Name when I hit save?  It makes me enter something into the Name field then when I save the trigger changes it to what was in the address field (which is correct) I just want that field to be populated so I don''t get the error that it is a requried feild.

Thank you, I will mark yours as the best answer.