• kevinjia1984
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 16
    Replies

Hi all,

Could anyone help me out to turn the following code into bulk version. I'm learning bulk trigger and still feel confused. Thanks in advance.

 

trigger CampaignMemberRelationshipTrigger on CampaignMember (after insert, after update) {

 

for(CampaignMember CM : Trigger.New){

 

if((CM.ContactId != NULL)&&(CM.Opt_Out__c == TRUE)){

 

 

Contact con = [SELECT Id, Relationship__c FROM Contact WHERE Id =: CM.ContactId];

 

 

Campaign cam = [SELECT Id, Account__c FROM Campaign WHERE Id =: CM.CampaignId];

 

 

Relationships__c rs = [SELECT Id, Status__c, Contact__c,List_Owner__c FROM Relationships__c WHERE Id =: con.Relationship__c];

 

 

if(rs.List_Owner__c == cam.Account__c)

{

rs.Status__c = 'Opt-Out';

 

Update rs;

}

}

}

}

Hi all,

 

My customer wants me to create a trigger fired on contact object, this trigger will create a relationship object related to this new created contact record and get some field values from contact. After inserting the contact object, I need to blank out three fields of this contact object and the relationship should get this contactId

 

My problem is, when I set this trigger before insert. The blank out code part works fine, but I cannot get the contactId in the relationship object for this trigger fired before insert.

 

And then I try to set the trigger after insert, it gives me some error message said that fields is read only, so I cannot get the fields to be blank out successfully.

 

Is there any way to meet both requirements ie. 1. Realtionship object should obtain the contactId 2. In the contact, field should be blank out. 

 

I have highlight the parts which will cause some problems. Thanks in advance for any answer

 

trigger ContactRelationshipTrigger on Contact (before insert) {

 

for(Contact CON : Trigger.New){

 

//A new Relationships__c record will be created

 Relationships__c relation = new Relationships__c();

 

 //Make it related to the Account which name is in the List Owner

Account ac = [SELECT Id, Name FROM Account WHERE Name =: CON.List_Owner__c];

relation.List_Owner__c = ac.Id;

relation.Contact__c = CON.Id;

 

 //Get the values in both List Id and opt-in of the contact record

relation.List_ID__c = CON.List_ID__C;

relation.List_Opt_In__c = CON.List_Opt_In__c;

 

insert relation;

 

 

CON.Relationship__c = relation.Id;

 //Blank out the three string field in the Contact

CON.List_Owner__c = '';

CON.List_ID__c = '';

CON.List_Opt_In__c = '';

 

}

}

Hi all,

 

Just want to ask if I add a for loop just inside the trigger bracket will the trigger has the ability to handle bulk operation of Data? ie.

 

trigger ContactRelationshipTrigger on Contact (before insert, before update) {

 

for(Contact CON : Trigger.New){

...

}

}

 

If not, how to realize it? Thanks. 

Hi All, I have met a problem and have no idea how to handle it. Here is the situation: I need to write a SOQL sentence like "SELECT field1 FROM object1" but in my case I don't know the value of fields1 in the first place. There is a string to hold the value. How can write the SOQL sentence to use this string value. Thanks

Hi all,

 

I have a trigger to send  emails to the email addresses listed in the "toAddresses" .And all the body message goes within the variable named 'body'. Is there any possible to format the content in the body to make it looks like a table? The following is the email sending part of my code. Thanks in advance for any help

 

 

//Static email subject information
 String subject = Client_Name + '- Project Status Report - ' + Report_Date;
 
 //Construct email body
 String body = 'Report Generated by:   ' + UserName + '<BR />'
 + 'Reporting For:  '+ Report_Date + '<BR />' + '<BR />' +'Client Name: ' +Client_Name + '<BR />'
 +  'Overall Status: '+ Overall_Status + '<BR />' + 'Phase: ' + Phase + '<BR />'
 + 'Actions Today: '+ Actions + '<BR />'+ 'Yellow Flags: '+ Yellow_Flags+'<BR />'
 + 'Action Plan: ' + Action_Plan + '<BR />' 
 
 //Send Email
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 String[] toAddresses = statusReportEmails.get(sReport.Id).split(';');
 mail.setToAddresses(toAddresses);
 mail.setSubject(subject);
mail.setSaveAsActivity(true);
mail.setHtmlBody(body);

 

Hi all,

Can I make an text data type equals to a string value in the syntax like object1.textField =(text)object1.stringField? If not, how can i get it. Many thanks for any help.

 

Hi all,
I have met a problem. I'd appreciate it very much if anyone can offer help. The situation is when the Publication_Mentions__c object created with the Referral__c check box selected,  An opportunity object will be created in this Publication_Mentions__c object's related list. In the opportunity record's Name field, I need to put contact's firstname and lastname. Contact object is lookup related to Publication_Mention__c object and the lookup field in Publication_Mention__c named "Organisation_Contact_del__r". After I finished the code below and test it. I found the value in Opportunity.Name is null null, other fields' values are fine. Don't know what happened, I have selected a contact firstname and lastname when I created the Publication_Mention__c. Please give me some help, thanks.
public with sharing class NewPublicationMention {
 public static void createNewOpportunity(List<Publication_Mention__c> PublicationMentions)
 {
 for(Publication_Mention__c PM: PublicationMentions)
 {
 //when the Referral__c checkbox ticked
 if(PM.Referral__c==True)
 {
 
 //a new Opportunity object will be created
 Opportunity newOpportunity = new Opportunity();
 newOpportunity.Publication_Mention__c = PM.Id;
 
 
 //fields of the new Opportunity object
 newOpportunity.Name = PM.Organisation_Contact_del__r.FirstName + PM.Organisation_Contact_del__r.LastName;
newOpportunity.StageName = 'New';
newOpportunity.CloseDate = Date.today();
newOpportunity.Type = 'Free';
newOpportunity.Order_Source__c = 'Helpline';
newOpportunity.Order_Date__c = Date.today();
newOpportunity.Amount = 0;
insert newOpportunity;

}

}

}

}

 

Hi All,

 

I have three object Publication_Mention__c ,  Opportunity, Product. They all have lookup relationship between each other. The requirement is when a new Publication_Mention__c object is inserted into database with its tickbox field referall__c being ticked. A new Opportunity and the related Product record will be both created. I have tested the code I wrote. The Opportunity object inserting works fine(If I Comment out "insert newProduct;" in my code) . But when I try to insert a Product, error occurred.  Here is my code

 

Ps. 1.The "Product" renamed "Product2" in our data Schema

        2. I am using a trigger to call createNewOpportunity on Publicatiopn_Mention__c object

 

public with sharing class NewPublicationMention {
 public static void createNewOpportunity(List<Publication_Mention__c> PublicationMentions)
 {
 for(Publication_Mention__c PM: PublicationMentions)
 {
 //when the Referral__c checkbox ticked
 if(PM.Referral__c==True)
 {
 //a new Opportunity object will be created
 Opportunity newOpportunity = new Opportunity(Publication_Mention__c = PM.Id);
 
 //fields of the new Opportunity object
 newOpportunity.Name = newOpportunity.Contact_Name__r.FirstName + newOpportunity.Contact_Name__r.LastName + '-' + newOpportunity.Product__r.Name;
 newOpportunity.StageName = 'New';
 newOpportunity.CloseDate = Date.today();
 newOpportunity.Type = 'Free';
 newOpportunity.Order_Source__c = 'Helpline';
 newOpportunity.Order_Date__c = Date.today();
 newOpportunity.Amount = 0;
 
 insert newOpportunity;
 
 
 //a new Product object will be created
 Product2 newProduct = new Product2(Publication_Mention__c = PM.Id);
 
 //fields of the new Product object
 newProduct.Quantity_in_Stock__c = 1;
 newProduct.Price__c = 0.00;
 newProduct.First_Publication_Date__c = Date.today();
 
 insert newProduct;
 }
 }
}
}

 

 

Flowing is the error message: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Product Name]: [Product Name]: 

 

I'd appreciate it very  much for any help. Thanks

 

Hi all,

 

I am a newbie in this area. I want to realize that When a new Publication_Mention__c record is inserted into the database with the Referral__c checkbox ticked  a new Opportunity  with a Product line item associated against the Opportunity will be created.

 

Below is the trigger:

 

 

trigger NewPublicationMentionTrigger on Publication_Mention__c(before insert, before update) {

 NewPublicationMention.createNewOpportunity(Trigger.New);

}

 

 

Here is the class:

 

 

public with sharing class NewPublicationMention {
 public static void createNewOpportunity(List<Publication_Mention__c> PublicationMentions)
 {
 for(Publication_Mention__c PM: PublicationMentions)
 {
 //when the Referral__c checkbox ticked
 if(PM.Referral__c==True)
 {
 //a new Opportunity object will be created
 Opportunity newOpportunity = new Opportunity();
 
 //a new Product object will be created
 Product2 newProduct = new Product2();
 
 //fields of the new Opportunity object
 newOpportunity.Name = newOpportunity.Contact_Name__r.FirstName + newOpportunity.Contact_Name__r.LastName + '-' + newOpportunity.Product__r.Name;
 newOpportunity.StageName = 'New';
 newOpportunity.CloseDate = Date.today();
 newOpportunity.Type = 'Free';
 newOpportunity.Order_Source__c = 'Helpline';
 newOpportunity.Order_Date__c = Date.today();
 newOpportunity.Amount = 0;
 
 //fields of the new Product object
 newProduct.Quantity_in_Stock__c = 1;
 newProduct.Price__c = 0.00;
 newProduct.First_Publication_Date__c = Date.today();
 }
 }
 }
}

 

 

I have tested it with creating a publication mention object. But no Opportunity and product record created. Can some one help me out with the wrong part. Thanks a lot

Hi Guys,

 

I am a newbie. Just want to know how to make a date type field equals to today's date. I have tried objectName.fieldName= today(). but it seems wrong. 

Hi all,

Could anyone help me out to turn the following code into bulk version. I'm learning bulk trigger and still feel confused. Thanks in advance.

 

trigger CampaignMemberRelationshipTrigger on CampaignMember (after insert, after update) {

 

for(CampaignMember CM : Trigger.New){

 

if((CM.ContactId != NULL)&&(CM.Opt_Out__c == TRUE)){

 

 

Contact con = [SELECT Id, Relationship__c FROM Contact WHERE Id =: CM.ContactId];

 

 

Campaign cam = [SELECT Id, Account__c FROM Campaign WHERE Id =: CM.CampaignId];

 

 

Relationships__c rs = [SELECT Id, Status__c, Contact__c,List_Owner__c FROM Relationships__c WHERE Id =: con.Relationship__c];

 

 

if(rs.List_Owner__c == cam.Account__c)

{

rs.Status__c = 'Opt-Out';

 

Update rs;

}

}

}

}

Hi all,

 

My customer wants me to create a trigger fired on contact object, this trigger will create a relationship object related to this new created contact record and get some field values from contact. After inserting the contact object, I need to blank out three fields of this contact object and the relationship should get this contactId

 

My problem is, when I set this trigger before insert. The blank out code part works fine, but I cannot get the contactId in the relationship object for this trigger fired before insert.

 

And then I try to set the trigger after insert, it gives me some error message said that fields is read only, so I cannot get the fields to be blank out successfully.

 

Is there any way to meet both requirements ie. 1. Realtionship object should obtain the contactId 2. In the contact, field should be blank out. 

 

I have highlight the parts which will cause some problems. Thanks in advance for any answer

 

trigger ContactRelationshipTrigger on Contact (before insert) {

 

for(Contact CON : Trigger.New){

 

//A new Relationships__c record will be created

 Relationships__c relation = new Relationships__c();

 

 //Make it related to the Account which name is in the List Owner

Account ac = [SELECT Id, Name FROM Account WHERE Name =: CON.List_Owner__c];

relation.List_Owner__c = ac.Id;

relation.Contact__c = CON.Id;

 

 //Get the values in both List Id and opt-in of the contact record

relation.List_ID__c = CON.List_ID__C;

relation.List_Opt_In__c = CON.List_Opt_In__c;

 

insert relation;

 

 

CON.Relationship__c = relation.Id;

 //Blank out the three string field in the Contact

CON.List_Owner__c = '';

CON.List_ID__c = '';

CON.List_Opt_In__c = '';

 

}

}

Hi All, I have met a problem and have no idea how to handle it. Here is the situation: I need to write a SOQL sentence like "SELECT field1 FROM object1" but in my case I don't know the value of fields1 in the first place. There is a string to hold the value. How can write the SOQL sentence to use this string value. Thanks

Hi all,

 

I have a trigger to send  emails to the email addresses listed in the "toAddresses" .And all the body message goes within the variable named 'body'. Is there any possible to format the content in the body to make it looks like a table? The following is the email sending part of my code. Thanks in advance for any help

 

 

//Static email subject information
 String subject = Client_Name + '- Project Status Report - ' + Report_Date;
 
 //Construct email body
 String body = 'Report Generated by:   ' + UserName + '<BR />'
 + 'Reporting For:  '+ Report_Date + '<BR />' + '<BR />' +'Client Name: ' +Client_Name + '<BR />'
 +  'Overall Status: '+ Overall_Status + '<BR />' + 'Phase: ' + Phase + '<BR />'
 + 'Actions Today: '+ Actions + '<BR />'+ 'Yellow Flags: '+ Yellow_Flags+'<BR />'
 + 'Action Plan: ' + Action_Plan + '<BR />' 
 
 //Send Email
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 String[] toAddresses = statusReportEmails.get(sReport.Id).split(';');
 mail.setToAddresses(toAddresses);
 mail.setSubject(subject);
mail.setSaveAsActivity(true);
mail.setHtmlBody(body);

 

Hi all,

Can I make an text data type equals to a string value in the syntax like object1.textField =(text)object1.stringField? If not, how can i get it. Many thanks for any help.

Hi All,

 

I have three object Publication_Mention__c ,  Opportunity, Product. They all have lookup relationship between each other. The requirement is when a new Publication_Mention__c object is inserted into database with its tickbox field referall__c being ticked. A new Opportunity and the related Product record will be both created. I have tested the code I wrote. The Opportunity object inserting works fine(If I Comment out "insert newProduct;" in my code) . But when I try to insert a Product, error occurred.  Here is my code

 

Ps. 1.The "Product" renamed "Product2" in our data Schema

        2. I am using a trigger to call createNewOpportunity on Publicatiopn_Mention__c object

 

public with sharing class NewPublicationMention {
 public static void createNewOpportunity(List<Publication_Mention__c> PublicationMentions)
 {
 for(Publication_Mention__c PM: PublicationMentions)
 {
 //when the Referral__c checkbox ticked
 if(PM.Referral__c==True)
 {
 //a new Opportunity object will be created
 Opportunity newOpportunity = new Opportunity(Publication_Mention__c = PM.Id);
 
 //fields of the new Opportunity object
 newOpportunity.Name = newOpportunity.Contact_Name__r.FirstName + newOpportunity.Contact_Name__r.LastName + '-' + newOpportunity.Product__r.Name;
 newOpportunity.StageName = 'New';
 newOpportunity.CloseDate = Date.today();
 newOpportunity.Type = 'Free';
 newOpportunity.Order_Source__c = 'Helpline';
 newOpportunity.Order_Date__c = Date.today();
 newOpportunity.Amount = 0;
 
 insert newOpportunity;
 
 
 //a new Product object will be created
 Product2 newProduct = new Product2(Publication_Mention__c = PM.Id);
 
 //fields of the new Product object
 newProduct.Quantity_in_Stock__c = 1;
 newProduct.Price__c = 0.00;
 newProduct.First_Publication_Date__c = Date.today();
 
 insert newProduct;
 }
 }
}
}

 

 

Flowing is the error message: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Product Name]: [Product Name]: 

 

I'd appreciate it very  much for any help. Thanks

 

Hi Guys,

 

I am a newbie. Just want to know how to make a date type field equals to today's date. I have tried objectName.fieldName= today(). but it seems wrong.