• alex_from_75015
  • NEWBIE
  • 80 Points
  • Member since 2010

  • Chatter
    Feed
  • 3
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 11
    Questions
  • 15
    Replies

Hi - I am relatively new to APEX and i have just created my first apex trigger. Now the problem is i do not know how to write an apex class trigger to test the new lead created. Can anyone please share their thoughts. 

 

Any help will be really really appreciated

 

Thanks 

 

Jeff 

 

Here is the trigger i wrote

 

 

trigger CreateRefLead on Lead (after insert) {


List<Lead> leads = new List<Lead> ();

for(Lead l:Trigger.new)
{

if(l.Referred_Email_1__c!=null && l.Referred_First_Name_1__c!=null && l.Referred_Last_Name_1__c!=null )
{
Lead l1 = new Lead();
l1.lastName = l.Referred_Last_Name_1__c;
l1.Referrer_Lead__c = l.id;
l1.FirstName = l.Referred_First_Name_1__c;
l1.Email = l.Referred_Email_1__c;
l1.Company = l.Company;
leads.add(l1);
}


if(l.Referred_Email_2__c!=null && l.Referred_First_Name_2__c!=null && l.Referred_Last_Name_2__c!=null )
{
Lead l2 = new Lead();
l2.lastName = l.Referred_Last_Name_2__c;
l2.Referrer_Lead__c = l.id;
l2.FirstName = l.Referred_First_Name_2__c;
l2.Email = l.Referred_Email_2__c;
l2.Company = l.Company;
leads.add(l2);
}

if(l.Referred_Email_3__c!=null && l.Referred_First_Name_3__c!=null && l.Referred_Last_Name_3__c!=null )
{
Lead l3 = new Lead();
l3.lastName = l.Referred_Last_Name_3__c;
l3.Referrer_Lead__c = l.id;
l3.FirstName = l.Referred_First_Name_3__c;
l3.Email = l.Referred_Email_3__c;
l3.Company = l.Company;
leads.add(l3);
}

if(l.Referred_Email_4__c!=null && l.Referred_First_Name_4__c!=null && l.Referred_Last_Name_4__c!=null )
{
Lead l4 = new Lead();
l4.lastName = l.Referred_Last_Name_4__c;
l4.Referrer_Lead__c = l.id;
l4.FirstName = l.Referred_First_Name_4__c;
l4.Email = l.Referred_Email_4__c;
l4.Company = l.Company;
leads.add(l4);
}

if(l.Referred_Email_5__c!=null && l.Referred_First_Name_5__c!=null && l.Referred_Last_Name_5__c!=null )
{
Lead l5 = new Lead();
l5.lastName = l.Referred_Last_Name_5__c;
l5.Referrer_Lead__c = l.id;
l5.FirstName = l.Referred_First_Name_5__c;
l5.Email = l.Referred_Email_5__c;
l5.Company = l.Company;
leads.add(l5);
}

if(l.Referred_Email_6__c!=null && l.Referred_First_Name_6__c!=null && l.Referred_Last_Name_6__c!=null )
{
Lead l6 = new Lead();
l6.lastName = l.Referred_Last_Name_6__c;
l6.Referrer_Lead__c = l.id;
l6.FirstName = l.Referred_First_Name_6__c;
l6.Email = l.Referred_Email_6__c;
l6.Company = l.Company;
leads.add(l6);
}




} insert leads;



}

Hi

I have a object "proposal" with 2 lookup fields (not master relationship) : one for product and one for insurrance

What would be the syntax in "proposal" email template to call field from product / insurrance

 

I have tried many syntax but still empty : exemple for company field

{!Proposal__c.Company__c.Description__c}

{!Proposal__c.Company__r.Description__c}

{!Company__c.Description__c}

 

{!Proposal__c.Product__c.Description__c}

 

{!Proposal__c.Product__r.Description__c}

 

{!Product__c.Description__c}

 

Moreover is is possible to write in email template something like :

if (!Proposal__c.Type="xxx")

{

  • ezazeza
  • eazeaz
  • eeezaez

}

else

{

  • lml
  • lmlm
  • lmlm

}

Thanks for your help since I could not any document on this

NB : If I have thought about visualforce email but it seems that you can only add 1 related Object and I need 2

Regards

Hi

Is there an api to get all layout description and not only the one attached to my profile (current function describeLayout) ?

With metadata api I can have list of layouts and Id but I could not find the api or function that gives me definition of a given layout (thanks to its id) or all layout (event the one not attached to my profile)

In Eclipse, I can get all layout definition

<?xml version="1.0" encoding="UTF-8"?>
<Layout xmlns="http://soap.sforce.com/2006/04/metadata">
    <emailDefault>false</emailDefault>
    <headers>PersonalTagging</headers>
    <headers>PublicTagging</headers>
    <layoutSections>
        <customLabel>false</customLabel>
        <detailHeading>false</detailHeading>
        <editHeading>true</editHeading>
        <label>Account Information</label>
        <layoutColumns>
            <layoutItems>
                <behavior>Edit</behavior>
                <field>OwnerId</field>
            </layoutItems>

....

 

Thanks for your help

Regards

Hello

I have a simple webservice that returns opportunities link to a field of a given account (id of account is sent in request)

I would like to return list of opportunities if account id is correct and an error message 'Incorrect account id' if account id sent in request in incorrect

How can I do that knowing that return of WS is specified in definition

Here would be my code

 

@RestResource(urlMapping='/xxxx/*')
global with sharing class MyWLResource {
  @HttpPost
    global static list <Opportunity> doPost(String accountid)
    {
        Account myaccount;
        list <Opportunity> myresult;
        try
        {
            myaccount = [select website from Account where id=:accountid];
            myresult=[select id from Opportunity where website__c=:myaccount.website limit 10];
            return myresult;
        }
        catch (Exception e)
        {
            return 'Incorrect account id';
        }
    }
}

 

Thanks for your help

Is there a simple solution without building a new class object ?

Regards

Hello

I am using a js code to get oauth 2.0 token

Everything is going well except that my final POST request does not get an answer (state 200 but no response !!!)

My redirect uri is "http://localhost/testoauthjs/oauth2/oauth2.html" since I am developing on my PCs

I have try also with an page hosted on force.com in https : no answer

 

Any idea of what might be the problem ?

Thanks for your help

Regards

Hello

Is there a way to retrieve definition of a custom report thanks to its url or its name ? : API or APEX

The idea is : my customer create a tabular report with column, filter... (he knows how to do it)

I get definition of report and perform same request in soql

Thanks for your help

Hi

I need to build a dynamic query with large number

ipt__c and ipf__c are number with 18 digit

 

This request works

list <ip__c> mycompany_list= Database.query('select id, ipt__c, ipf__c from IP__c where  (ipf__c<=1932520400 and ipt__c>=1932520400)');

 

This request does not work

list <ip__c> mycompany_list= Database.query('select id, ipt__c, ipf__c from IP__c where  (ipf__c<=19325204002 and ipt__c>=19325204002)');

System.QueryException: For input string: "193252040024"

 

Can you please help ?

I have tried : "ipf__c<=19325204002L" with no success

Regards

 

Hello

I need some help to develop a trigger (for mass insert)

Here is my need :

 

I have a reference table : lets call it TABLE1 with one field "start_row" (double) and one field "end_row" (double) and other information fields (TABLE1 has around 120000 records)

 

I am performing a mass insert on TABLE2 with one field called "position" (double)

For each insert (trigger before insert), I have to search in TABLE1 if there is a record where "start_row"<="position"<="end_row"

If one record is found, I need to transfer other information fields from TABLE1 to TABLE2 object inserted

 

If it was a one by one insert request will look like be

try {

myrecord=[select info1, info2, info3 from TABLE1 where start_row<=evt.position and end_row>=evt.position]; // limit 1 is not necessary since I am sure there is only one or none matching record in TABLE1

evt.info1=myrecord.info1;

evt.info2=myrecord.info2;

}

catch...

 

How can I perform this mass select ?

Thanks for your help

Regards

Hello

Is there a best practices to perform instead of apex:repeat ?

Class wrapper or something like that ?

Thanks for your advice

Regads

Hello

I am using <apex:repeat> to display an agregation of field from different object linked to a main one

I want to add a delete link and I am getting crazy

I have tried this :

<td><apex:outputLink title="" value="{!myurl}&delId={!CampaignMember[idx].Id}" target="_blank" style="font-weight:bold">Remove</apex:outputLink></td>

-> that cause DML currently not allowed by treating delId parameter in main controller

 

Some post said to treat it in apex:page action {!deletemyobject} : same result DML currently not allowed

 

Then I have tried ajax

<td><apex:commandLink onclick="deleteCM('{!CampaignMember[idx].Id}')" value="Delete"/></td>

Works well for deletion but refreshing does not work : either line is not deleted on refresh, either I have everyline in double

 

Can someone help me with such a simple need ?

By the way if some has code for checkbox for each line to perform an action or select all checkbox like SFDC view, I am interested instead of rebuilding everything

 

Thanks for your help


Hello

Let's sayt that I have this role (organization)

Manager - France - Paris

Manager - France - Lyon

Manager - US- NY

Manager - US- LA

 

How can I get through a simple select all roles attached to "manager" ie France, US (easy : ParentRoleId) but also Paris, Lyon, NY, LA -> which mean a kind of recursive loop : get all children of manager then get all children of children then get all children of children of children...

 

Thanks for your help

Hello

I have the following needed : whenever somebody attach a file to an opportunity "close won" I want to send an email with this attachment to somebody

So I have created a trigger but Issue I have is how to attach "attachment" to email I have tried :

1) mail.setDocumentAttachments : but this only works with document and not attachment !!!!

2) Messaging.EmailFileAttachment but I can't set the body : with a text file I receive a document badly encoded "sdsf" becomes "xœ+NI+NI", with a png file, trigger says "No body specified in the file attachment"

Here is my code :

 

trigger Send_Document on Attachment (after insert) {
for (Attachment evt : Trigger.new) {
        // Iterate over each sObject
        string parentid=evt.ParentId;
        string typeid=parentid.substring(0,3);
        if (typeid=='006')
        {
                    Opportunity myopp=[select id, StageName, Name, AccountId from Opportunity where id=:evt.ParentId limit 1];
                    if (myopp.StageName=='Close Gagnée')
                    {
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        String[] toAddresses = new String[]{};
                        toAddresses.add('alachmann@runmyprocess.com');
                                   
                       
                        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                        attach.setFileName(evt.Name);
                        attach.setBody(evt.Body);
                        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});
              
                        mail.setToAddresses(toAddresses);
                        mail.setSubject('Document');
                        mail.setHtmlBody('A document has been uploaded');
                       
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                    }
        }
    }
}


Can somebody help me ?

Regards

 



Hi

Is there an api to get all layout description and not only the one attached to my profile (current function describeLayout) ?

With metadata api I can have list of layouts and Id but I could not find the api or function that gives me definition of a given layout (thanks to its id) or all layout (event the one not attached to my profile)

In Eclipse, I can get all layout definition

<?xml version="1.0" encoding="UTF-8"?>
<Layout xmlns="http://soap.sforce.com/2006/04/metadata">
    <emailDefault>false</emailDefault>
    <headers>PersonalTagging</headers>
    <headers>PublicTagging</headers>
    <layoutSections>
        <customLabel>false</customLabel>
        <detailHeading>false</detailHeading>
        <editHeading>true</editHeading>
        <label>Account Information</label>
        <layoutColumns>
            <layoutItems>
                <behavior>Edit</behavior>
                <field>OwnerId</field>
            </layoutItems>

....

 

Thanks for your help

Regards

Hello

I have a simple webservice that returns opportunities link to a field of a given account (id of account is sent in request)

I would like to return list of opportunities if account id is correct and an error message 'Incorrect account id' if account id sent in request in incorrect

How can I do that knowing that return of WS is specified in definition

Here would be my code

 

@RestResource(urlMapping='/xxxx/*')
global with sharing class MyWLResource {
  @HttpPost
    global static list <Opportunity> doPost(String accountid)
    {
        Account myaccount;
        list <Opportunity> myresult;
        try
        {
            myaccount = [select website from Account where id=:accountid];
            myresult=[select id from Opportunity where website__c=:myaccount.website limit 10];
            return myresult;
        }
        catch (Exception e)
        {
            return 'Incorrect account id';
        }
    }
}

 

Thanks for your help

Is there a simple solution without building a new class object ?

Regards

Hello

I am using a js code to get oauth 2.0 token

Everything is going well except that my final POST request does not get an answer (state 200 but no response !!!)

My redirect uri is "http://localhost/testoauthjs/oauth2/oauth2.html" since I am developing on my PCs

I have try also with an page hosted on force.com in https : no answer

 

Any idea of what might be the problem ?

Thanks for your help

Regards

Hello

Is there a way to retrieve definition of a custom report thanks to its url or its name ? : API or APEX

The idea is : my customer create a tabular report with column, filter... (he knows how to do it)

I get definition of report and perform same request in soql

Thanks for your help

Hi - I am relatively new to APEX and i have just created my first apex trigger. Now the problem is i do not know how to write an apex class trigger to test the new lead created. Can anyone please share their thoughts. 

 

Any help will be really really appreciated

 

Thanks 

 

Jeff 

 

Here is the trigger i wrote

 

 

trigger CreateRefLead on Lead (after insert) {


List<Lead> leads = new List<Lead> ();

for(Lead l:Trigger.new)
{

if(l.Referred_Email_1__c!=null && l.Referred_First_Name_1__c!=null && l.Referred_Last_Name_1__c!=null )
{
Lead l1 = new Lead();
l1.lastName = l.Referred_Last_Name_1__c;
l1.Referrer_Lead__c = l.id;
l1.FirstName = l.Referred_First_Name_1__c;
l1.Email = l.Referred_Email_1__c;
l1.Company = l.Company;
leads.add(l1);
}


if(l.Referred_Email_2__c!=null && l.Referred_First_Name_2__c!=null && l.Referred_Last_Name_2__c!=null )
{
Lead l2 = new Lead();
l2.lastName = l.Referred_Last_Name_2__c;
l2.Referrer_Lead__c = l.id;
l2.FirstName = l.Referred_First_Name_2__c;
l2.Email = l.Referred_Email_2__c;
l2.Company = l.Company;
leads.add(l2);
}

if(l.Referred_Email_3__c!=null && l.Referred_First_Name_3__c!=null && l.Referred_Last_Name_3__c!=null )
{
Lead l3 = new Lead();
l3.lastName = l.Referred_Last_Name_3__c;
l3.Referrer_Lead__c = l.id;
l3.FirstName = l.Referred_First_Name_3__c;
l3.Email = l.Referred_Email_3__c;
l3.Company = l.Company;
leads.add(l3);
}

if(l.Referred_Email_4__c!=null && l.Referred_First_Name_4__c!=null && l.Referred_Last_Name_4__c!=null )
{
Lead l4 = new Lead();
l4.lastName = l.Referred_Last_Name_4__c;
l4.Referrer_Lead__c = l.id;
l4.FirstName = l.Referred_First_Name_4__c;
l4.Email = l.Referred_Email_4__c;
l4.Company = l.Company;
leads.add(l4);
}

if(l.Referred_Email_5__c!=null && l.Referred_First_Name_5__c!=null && l.Referred_Last_Name_5__c!=null )
{
Lead l5 = new Lead();
l5.lastName = l.Referred_Last_Name_5__c;
l5.Referrer_Lead__c = l.id;
l5.FirstName = l.Referred_First_Name_5__c;
l5.Email = l.Referred_Email_5__c;
l5.Company = l.Company;
leads.add(l5);
}

if(l.Referred_Email_6__c!=null && l.Referred_First_Name_6__c!=null && l.Referred_Last_Name_6__c!=null )
{
Lead l6 = new Lead();
l6.lastName = l.Referred_Last_Name_6__c;
l6.Referrer_Lead__c = l.id;
l6.FirstName = l.Referred_First_Name_6__c;
l6.Email = l.Referred_Email_6__c;
l6.Company = l.Company;
leads.add(l6);
}




} insert leads;



}

Hi all.


I am trying to fire a trigger when a new opportunity is created to select the account's default currency for this opportunity. 

 

trigger currencyTransfer on Opportunity (before insert) {
  for(Opportunity opp : Trigger.new){
   opp.CurrencyIsoCode = opp.Account.CurrencyIsoCode;
   }
}

 I guess it doesn't work to lookup the currency through the account this way - what else can I do? Any help would be greatly appreciated

Hi

I need to build a dynamic query with large number

ipt__c and ipf__c are number with 18 digit

 

This request works

list <ip__c> mycompany_list= Database.query('select id, ipt__c, ipf__c from IP__c where  (ipf__c<=1932520400 and ipt__c>=1932520400)');

 

This request does not work

list <ip__c> mycompany_list= Database.query('select id, ipt__c, ipf__c from IP__c where  (ipf__c<=19325204002 and ipt__c>=19325204002)');

System.QueryException: For input string: "193252040024"

 

Can you please help ?

I have tried : "ipf__c<=19325204002L" with no success

Regards

 

Hello

I need some help to develop a trigger (for mass insert)

Here is my need :

 

I have a reference table : lets call it TABLE1 with one field "start_row" (double) and one field "end_row" (double) and other information fields (TABLE1 has around 120000 records)

 

I am performing a mass insert on TABLE2 with one field called "position" (double)

For each insert (trigger before insert), I have to search in TABLE1 if there is a record where "start_row"<="position"<="end_row"

If one record is found, I need to transfer other information fields from TABLE1 to TABLE2 object inserted

 

If it was a one by one insert request will look like be

try {

myrecord=[select info1, info2, info3 from TABLE1 where start_row<=evt.position and end_row>=evt.position]; // limit 1 is not necessary since I am sure there is only one or none matching record in TABLE1

evt.info1=myrecord.info1;

evt.info2=myrecord.info2;

}

catch...

 

How can I perform this mass select ?

Thanks for your help

Regards

Hello

Is there a best practices to perform instead of apex:repeat ?

Class wrapper or something like that ?

Thanks for your advice

Regads

Hello

I am using <apex:repeat> to display an agregation of field from different object linked to a main one

I want to add a delete link and I am getting crazy

I have tried this :

<td><apex:outputLink title="" value="{!myurl}&delId={!CampaignMember[idx].Id}" target="_blank" style="font-weight:bold">Remove</apex:outputLink></td>

-> that cause DML currently not allowed by treating delId parameter in main controller

 

Some post said to treat it in apex:page action {!deletemyobject} : same result DML currently not allowed

 

Then I have tried ajax

<td><apex:commandLink onclick="deleteCM('{!CampaignMember[idx].Id}')" value="Delete"/></td>

Works well for deletion but refreshing does not work : either line is not deleted on refresh, either I have everyline in double

 

Can someone help me with such a simple need ?

By the way if some has code for checkbox for each line to perform an action or select all checkbox like SFDC view, I am interested instead of rebuilding everything

 

Thanks for your help


Hello

I have the following needed : whenever somebody attach a file to an opportunity "close won" I want to send an email with this attachment to somebody

So I have created a trigger but Issue I have is how to attach "attachment" to email I have tried :

1) mail.setDocumentAttachments : but this only works with document and not attachment !!!!

2) Messaging.EmailFileAttachment but I can't set the body : with a text file I receive a document badly encoded "sdsf" becomes "xœ+NI+NI", with a png file, trigger says "No body specified in the file attachment"

Here is my code :

 

trigger Send_Document on Attachment (after insert) {
for (Attachment evt : Trigger.new) {
        // Iterate over each sObject
        string parentid=evt.ParentId;
        string typeid=parentid.substring(0,3);
        if (typeid=='006')
        {
                    Opportunity myopp=[select id, StageName, Name, AccountId from Opportunity where id=:evt.ParentId limit 1];
                    if (myopp.StageName=='Close Gagnée')
                    {
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        String[] toAddresses = new String[]{};
                        toAddresses.add('alachmann@runmyprocess.com');
                                   
                       
                        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                        attach.setFileName(evt.Name);
                        attach.setBody(evt.Body);
                        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});
              
                        mail.setToAddresses(toAddresses);
                        mail.setSubject('Document');
                        mail.setHtmlBody('A document has been uploaded');
                       
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                    }
        }
    }
}


Can somebody help me ?

Regards

 



Hello
I have developed a trigger on account "before insert, before update".
It seems that this trigger is not called when I convert a Lead to an Account
How come : it is an insertion ?
How can I activated this trigger ? after insert ?
Thanks for your help
Regards