Change Lead owner trigger before insert - triggers

I have a trigger change owner before insert a new lead record.
trigger trickOnLead on Lead (before insert) {
for (Lead ld : Trigger.new) {
if(ld.testId__c != null ){
ld.ownerid = ld.testId__c;
}
}
}
I tested it through web-to-lead, and its successful.(on Monday, July 8th),
but today its not work anymore ?!
note that it only work for manual create a new record.
Thanks,
Manh

trigger trickOnLead on Lead (before insert, before update) {
for (Lead ld : Trigger.new) {
if(ld.testId__c != null ){
ld.ownerid = ld.testId__c;
}
}
}

Related

Prevent the deletion of Account which have related contact records having primary_contact__c check box = yes . (Salesforce trigger)

how do I know how many related contact record having primary_contact__c == true?
trigger AccountTrigger_1 on Account (before delete)
{
list <account> acclist = [select id,(select id, primary_contact__c from contacts) from account where id in : trigger.old];
for (account acc1 : trigger.old){
for(account acc2 :acclist ){
if(acc1.contacts.size() > 0){
acc1.adderror('you can not delete account');
}
}
}
}
You were almost there but that double for loop will explode if you ever do mass delete, 200x200 records to loop is bit evil.
This should work bit better.
trigger AccountTrigger_1 on Account (before delete)
{
Map<Id, Account> checks = new Map<Id, Account>([SELECT Id,
(SELECT Id
FROM Contacts
WHERE Primary_Contact__c = true
LIMIT 1)
FROM Account
WHERE Id IN :trigger.old
]);
for (account acc1 : trigger.old){
Account check = checks.get(acc1.Id);
if(!check.Contacts.isEmpty()){
acc1.adderror('you can not delete account');
}
}
}

APEX Trigger update picklist on child object

I am trying to do something simple (I hope).
I have 2x objects: Contact and Career_Path__c
Contact has one field: Currency__c (picklist)
Career_Path__c has one field: Next_Currency__c (picklist)
hese two objects are related via a loopup relationship from Career_Path__c up to Contact
I am trying to update the Next_Currency__c field every time a new Career_Path__c record is created. This does not seem to work. Thanks for any suggestions.
trigger Trigger_dynPicklist_2 on Contact (after insert) {
Map<Id, String> contactIdToCareerPathMap = new Map<Id, String>();
for (Contact c : Trigger.new) {
if (String.isNotBlank(c.Id)) {
//System.debug('El contacto tiene id: '+c.Id);
contactIdToCareerPathMap.put(c.Id, c.Currency__c);
}
}
List<Career_Path__c> careerPathToUpdate = [SELECT Id, Next_Currency__c, Contact__c
FROM Career_Path__c
WHERE Contact__c IN :contactIdToCareerPathMap.keySet()
];
if(careerPathToUpdate.size() > 0){
//System.debug('Tiene hijos');
for (Career_Path__c career : careerPathToUpdate) {
//System.debug('hijos: '+ career.id);
career.Next_Currency__c = contactIdToCareerPathMap.get(career.Contact__c);
}
update careerPathToUpdate;
}
}
You're trying to set trigger on Contact when you actually need to set it on Career_Path__c SObject. Your code should look something like this to achieve Next_Currency__c auto-populated from related Contact Currency__c (also take a note that trigger will now be BEFORE insert, so you do not need to update record again):
trigger Trigger_dynPicklist_2 on Career_Path__c (before insert) {
Set<Id> relatedContactsIds = new Set<Id>();
for (Career_Path__c path : Trigger.new) {
if (path.Contact__c != null) {
relatedContactsIds.add(path.Contact__c);
}
}
Map<Id, Contact> relatedContactsMap = [SELECT Id, Currency__c FROM Contact WHERE Id IN :relatedContactsIds AND Currency__c != NULL];
for (Career_Path__c path : Trigger.new) {
if (path.Contact__c != null && relatedContactsMap.containsKey(path.Contact__c)) {
path.Next_Currency__c = relatedContactsMap.get(path.Contact__c).Currency__c;
}
}
}
Also note, that when Contact is just created (and you are in trigger on insert), it cannot have related Career_Path__c objects.

Connecting Unrelated Objects during Apex Trigger

I am trying to create a trigger that does the following:
Once an Account has been created, create an unrelated record (called a "Portal Content" record) that bears the same name, assuming the default RecordTypeId
Take the ID of the newly created "Portal Content" record, and insert it into a lookup field on the originally created Account
Add the ID of the original Account, and enter it into a field of the newly created "Portal Content" record
Steps 1 and 2 were addressed in post Populate Lookup Field with Record Created From Trigger. The new problem is that when attempting Item 3, in the Trigger.isAfter code block, a.Portal_Content_Record__r returns null rather than with the Id value populated after insert p in the Trigger.isBefore block.
trigger newAccountCreated on Account (before insert, after insert) {
List<Account> alist = Trigger.New;
if(Trigger.isBefore){
for(Account a : alist) {
if (a.RecordTypeId == '012i0000001Iy1H') {
Portal_Content__c p = new Portal_Content__c(
Name=a.Name,
RecordTypeId='012i0000001J1zZ'
);
insert p;
a.Portal_Content_Record__c = p.Id;
system.debug('Made it to insert p. P = ' + p.Id +'. a.Portal_Content_Record__c = ' + a.Portal_Content_Record__c);
}
}
}
if (Trigger.isAfter) {
for(Account a : alist){
system.debug('a.Id = ' + a.Id + ', p = ' +a.Portal_Content_Record__r);
String p = a.Portal_Content_Record__c;
for(Portal_Content__c port : [SELECT ID FROM Portal_Content__c WHERE Id = :p]){
port.School_SFDC_ID__c = a.Id;
update port;
}
}
}
}
My question has two parts:
How do I assign a field on the newly inserted Portal_Content__c record with the ID of the Account that started the trigger?
Can this be done within this trigger, or is a secondary "helper" trigger needed?
I was able to figure out a way to answer this issue within the same Trigger. The Trigger.isAfter && Trigger.isInsert code block is what solved my problem.
trigger newAccountCreated on Account (before insert, after insert, after delete) {
List<Account> alist = Trigger.New;
List<Account> oldlist = Trigger.old;
if(Trigger.isBefore){
for(Account a : alist) {
if (a.RecordTypeId == '012i0000001Iy1H') {
Portal_Content__c p = new Portal_Content__c(
Name=a.Name,
RecordTypeId='012i0000001J1zZ'
);
insert p;
a.Portal_Content_Record__c = p.Id;
}
}
}
else if (Trigger.isAfter && Trigger.isDelete){
for(Account a : oldlist){
for(Portal_Content__c p : [SELECT ID FROM Portal_Content__c WHERE ID = :a.Portal_Content_Record__c]){
delete p;
}
}
}
if (Trigger.isAfter && Trigger.isInsert){
for(Account a : alist){
List<Portal_Content__c> plist = [SELECT ID FROM Portal_Content__c WHERE Id = :a.Portal_Content_Record__c];
for(Portal_Content__c p : plist){
p.School_SFDC_ID__c = a.Id;
update p;
}
}
}
}
This code block does a query for Portal_Contact__c records that match the Account record's Portal_Content_Record__c field value, which is assigned in the first code block. It then takes the Portal_Content__c records found, and assigns the original Account's Id to the record's School_SFDC_ID__c field value.

Salesforce Trigger: Update field Trigger from Custom Object

New to apex and have a question about writing triggers. Essentially, I'm trying to create a trigger that updates a given field when another field is updated (after a record is created/inserted into Salesforce).
More specifically, when a custom Account lookup field (lookup value is a custom object record) is updated, it should update another field with a different value from the custom object record.
i.e. When I update the High School name to Elm High School, it will also update the yield associated with that high school.
Below is the code that I've created so far:
trigger UpdateYield on Account (before update) {
Set<String> HS = new Set<String>();
for (Account hsName : Trigger.new) {
if (hsName.High_School_LU__c != null) {
HS.add(hsName.High_School_LU__c);
}
List<High_School__c> y = [SELECT Name, Yield__c FROM High_School__c WHERE Name IN :HS];
Map<String, High_School__c> yLU = new Map<String, High_School__c>();
for (High_School__c h : y) {
yLU.put(h.Name, h);
}
for (Account YieldU : Trigger.new) {
if (YieldU.High_School_LU__c != null) {
High_School__c a = yLU.get(YieldU.Name);
if (a != null) {
YieldU.Yield__c = a.Yield__c;
}
}
}
}
}
It saves however, it still does not work when I update the field.
Any help would be greatly appreciated. Thanks in advance.
The problem here is that the value of a lookup field is not actually a string (as displayed in the UI) but an ID, so when you perform your SOQL query you are comparing an ID to the Name field and getting no results.
If you change your code to the following you should get the result you expect.
It should also be notified that this simple use case could also be accomplished using a simple Workflow Field Update rather than a trigger.
trigger UpdateYield on Account (before update)
{
Set<Id> HS = new Set<Id>();
for (Account hsName : Trigger.new)
{
if (hsName.High_School_LU__c != null)
{
HS.add(hsName.High_School_LU__c);
}
}
Map<Id, High_School__c> y = [SELECT Name, Yield__c FROM High_School__c WHERE Id IN :HS];
for (Account YieldU : Trigger.new)
{
High_School__c a = y.get(YieldU.High_School_LU__c);
if (a != null)
{
YieldU.Yield__c = a.Yield__c;
}
}
}

Cross Object Field(Picklist) Update

Can anyone help me with writing a trigger on cross object field update.
Object: Booking__c Field: Booking_Type__c (is a picklist)
Object: Booking_Item__C Field: Booking_type__c (is also a picklist)
The requirement is, when status (Booking_Type_c) of Booking_c is selected as cancelled the status (Booking_Type__c ) of Booking_item__c should also be updated to cancelled.
Any help will be much appreciated.
Thank you
I'd like to make an assumption that your objects have the following relationship
Booking_item__c.Booking__c (Booking_item_c record has a lookup to Booking_c)
Trigger Booking on Booking__c (after insert, after update) {
Set<Id> cancelledBookingIds = new Set<Id>();
for (Booking__c booking: Trigger.new) {
if (booking.Booking_Type__c == 'cancelled') {
cancelledBookingIds.add(booking.id);
}
}
List<Booking_Item__c> bookingItemsForCancelling = [SELECT Booking_Type__c
FROM Booking_Item__c
WHERE Booking__c IN: cancelledBookingIds];
for (Booking_Item__c item: bookingItemsForCancelling) {
item.Booking_Type__c = 'cancelled';
}
update bookingItemsForCancelling;
}