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
Jairo Straga 6Jairo Straga 6 

Help with a basic Trigger

Hi people, I'm a Salesforce admin without code knowledge but I have to make a Trigger that I think is really basic, but I still can't do it.

I need to have a mirror Record Type text field that has to be filled before the matching rules are executed. With Process Builder I can't do that so I need a Trigger.

I wrote this with some videos that I watched, please don't laugh at me haha:

trigger RecordType on Account (before insert) {
    for(Account a: Trigger.New) {
    if(a.RecordTypeId = "012150000013uJx") {a.RecordType__c = 'Advertiser';}
    if(a.RecordTypeId = "012150000013uJs") {a.RecordType__c = 'Agencia';}
    if(a.RecordTypeId != "012150000013uJs" && a.RecordTypeId != "012150000013uJx" ) {a.RecordType__c = ' ';}
}

I'm recieving the following error message: Error de compilación: Unrecognized symbol '"', which is not a valid Apex identifier. en la línea 3 columna 25



 

Maharajan CMaharajan C
Hi Jairo,

Please use the == in if conditions like belowbelow will solve your error.
trigger RecordType on Account (before insert) {
    for(Account a: Trigger.New) {
          if(a.RecordTypeId == "012150000013uJx") {
a.RecordType__c = 'Advertiser';
             }

 if(a.RecordTypeId == "012150000013uJs")
{a.RecordType__c = 'Agencia';
}
    if(a.RecordTypeId != "012150000013uJs" && a.RecordTypeId != "012150000013uJx" ) {a.RecordType__c = ' ';} }
}

Thanks,
Maharajan.C
 
Maharajan CMaharajan C
Hey suggestion in your code:

Don't use the ID's directly in code. Simply don't hard code the ids

You can use schema to get the record type I'd by using the record type developer name.

Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();

use the above value in code to comparsion like below.
 if(a.RecordTypeId == recordTypeId)
{
}

 http://smukov.github.io/blog/2018/06/09/Record-Type-Id-By-Developer-Name/
Raj VakatiRaj Vakati
Use this code and dnt hardcode the record type Id .. inseted use the record type name in using dynamic apex
 
trigger RecordType on Account (before insert) {
	Id AdvertiserRTID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Advertiser').getRecordTypeId();
Id AgenciaRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Agencia').getRecordTypeId();

	
    for(Account a: Trigger.New) {
    if(a.RecordTypeId ==AdvertiserRTID)
		{a.RecordType__c = 'Advertiser';
	}
    if(a.RecordTypeId ==AgenciaRT) 
	{a.RecordType__c = 'Agencia';
	}
    if(a.RecordTypeId != AdvertiserRTID && a.RecordTypeId != AgenciaRT ) {
		a.RecordType__c = ' ';
	}
}
}

 
Jairo Straga 6Jairo Straga 6
I get the same error with == 

Unrecognized symbol '"', which is not a valid Apex identifier. en la línea 3 columna 32
Raj VakatiRaj Vakati
I guess you are trying to write the apex trigger in apex class .. Can u share the screenshot 

 
Jairo Straga 6Jairo Straga 6
User-added image
Raj VakatiRaj Vakati
you forget end ;

use this code as it is
 
trigger RecordType on Account (before insert) {
	Id AdvertiserRTID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Advertiser').getRecordTypeId();
Id AgenciaRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Agencia').getRecordTypeId();

	
    for(Account a: Trigger.New) {
    if(a.RecordTypeId ==AdvertiserRTID)
		{a.RecordType__c = 'Advertiser';
	}
    if(a.RecordTypeId ==AgenciaRT) 
	{a.RecordType__c = 'Agencia';
	}
    if(a.RecordTypeId != AdvertiserRTID && a.RecordTypeId != AgenciaRT ) {
		a.RecordType__c = ' ';
	}
}
}