• Dorel4
  • NEWBIE
  • 30 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 17
    Replies
I need help creating an Apex trigger to get the lead assignment rule to trigger on our imported leads. I need it to run on all leads and I am not sure how to get it to work.  thank you

trigger LeadAssign on Lead (after update) 
{
 List<Id> lIds=new List<id>();
For (lead l:trigger.new){
    if (l.IsConverted==False){
        lIds.add(l.Id);   }
       }
if (AssignLeads.assignAlreadyCalled()==FALSE){
    system.debug('Assign already called? '+AssignLeads.assignAlreadyCalled());
    AssignLeads.Assign(lIds);
}
}
public global class AssignLeads{

public static Boolean assignAlreadyCalled=FALSE;

public static boolean assignAlreadyCalled(){
return assignAlreadyCalled;
}

@future
public static void assign(List<Id> lIds){
assignAlreadyCalled=TRUE;
List<Lead> leads=[SELECT Id FROM Lead WHERE Id IN: lIds];
For (lead l:leads){
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
l.setOptions(dmo);

}
update(leads);
}

}
  
  • January 27, 2016
  • Like
  • 0
I have created a trigger on a custom object called Trident Contract. It pulled the agent name from the custom object Agent using the Agent Id in the field Buyer Agent Lookup (it is a lookup field). Now I need to add the Buyer Agent Office with a look up from the Agent object mapping with   the Agent Id to the trigger. The Buyer Agent Lookup Field trigger was working until I tried to add the Buyer Agent Office.
Here is my originial trigger. Please advise how to add another field lookup for Buyer Agent Office. Thank you

trigger BuyAgent on Trident_Contract__c (before insert, before update) {
Set <String> AgentIDs = new Set <String>();
    for(Trident_Contract__c tc:trigger.new){
        if(tc.Buyer_Agent_ID__c !=NULL){
            AgentIDs.add(tc.Buyer_Agent_ID__c);
        } 
    }
    //Match Agent ID to Buyer Agent ID
    //Map ID 

   Map<String, Agent__c> Agents = new map <String, Agent__C>();
    
    for (Agent__c obj: [SELECT ID, Agent_ID__c
                        from Agent__c
                        Where Agent_ID__c 
                        IN: AgentIDs]){                            Agents.put(obj.Agent_ID__c,obj);}
for(Trident_Contract__c tc : trigger.new)
{if (Agents.containsKey(tc.Buyer_Agent_ID__c))
                tc.Buyer_Agent_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;
 }
}
  • February 13, 2015
  • Like
  • 0
I am trying to create an apex email trigger on an Opportunity but have the recipient email come from a custom field called Agent email. I would like the email to be sent when the Stage is in 'Agent Accepted' to the email address in the field Agent Email. I have a template created for the body of the email but I can not pass the test. Almost the whole code is an error when I run the test.  Please help.

trigger AgentEmailfee on Opportunity  (after Insert,after update) {
                  EmailTemplate template = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate 
                                            WHERE Name = 'Agent_Referral_Fee'];
//Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  //           mail.setTemplateId(template.id);
               
                              String subject = template.Subject;
subject = subject.replace('{!Opportunity.Name}','has been accepted'); 

 // Pick ca dummy Contact
 Opportunity o =[select id, Agent_Email__c from opportunity where Agent_Email__c <> null limit 1];

 // Construct the list of emails we want to send
 List <Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();

 Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
 msg.setTemplateId( [select id from EmailTemplate where DeveloperName='Agent_Referral_Fee'].id );
 msg.setWhatId( [select id from Opportunity limit 1].id );
 msg.setTargetObjectId(o.agent_email__c);
 msg.setToAddresses(new List<String>{''});

 lstMsgs.add(msg);

 // Send the emails in a transaction, then roll it back
 Savepoint sp = Database.setSavepoint();
 Messaging.sendEmail(lstMsgs);
 Database.rollback(sp);

     
 // For each SingleEmailMessage that was just populated by the sendEmail() method, copy its
 // contents to a new SingleEmailMessage. Then send those new messages.
 List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
 for (Messaging.SingleEmailMessage email : lstMsgs) {
 Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
 emailToSend.setToAddresses(email.getToAddresses());
 emailToSend.setPlainTextBody(email.getPlainTextBody());
 emailToSend.setHTMLBody(email.getHTMLBody());
 emailToSend.setSubject(email.getSubject());
 lstMsgsToSend.add(emailToSend);
 }
 Messaging.sendEmail(lstMsgsToSend);       
 for (opportunity op :Trigger.new)
    {
        if(op.Agent_Assigned__c != NULL)
            (op.Referral_Fee__c = TRUE);
            (op.stageName ='Agent Assigned');  }
}
  • February 09, 2015
  • Like
  • 0
I have written a trigger (ListingAgent) trying to match fields from different objects, Opportunity and Agent. On the Opportunity page if the Listing Agent ID from LR (Listing_Agent_ID_From_LR__c) has a ID that matches an Agent ID from the Agent Object , I want the Agent Name (Name) to be populated in the Listing Agent Field. I get 87% coverage but an error for the last line. Does someone see a problem with the code? I can not see it. Please help. Thank you.
trigger Listingagent on Opportunity (before insert) {
    Set<String> lagid=new set<string>();
    for(opportunity o :Trigger.new){
        //if(o.listing_Agent_ID_From_LR__c != NULL)      
            lagid.add(o.Listing_Agent_ID_From_LR__c);
    }

    Map<String, String>matchid=new Map<String, String>();

    For(Agent__c ag: [select Agent_ID__c,
               Name
               from Agent__c 
               where Agent_ID__c 
                IN: lagid]){
        matchid.put(ag.Agent_ID__c, ag.Name);
    }
    For(Opportunity o : Trigger.new){
        if(matchid.containskey(o.Listing_Agent_ID_From_LR__c))
            o.Listing_Agent__c = matchid.get(o.Listing_Agent_ID_From_LR__c);
    }
}


 
  • February 02, 2015
  • Like
  • 0
Please help.  I am creating my first trigger called "List Agent".  I would like to take the"Lead" field 'Listing Agent ID LR Source' to look up in the Custom Object "Agent" to see if there is a match to the "Agent" field 'Agent ID' If it does match, I would like to take the information from the "Agent" field 'Agent Name' and insert it in the "Lead" Field 'Listing Name'.  I can not get the trigger to save and I am not sure what I am doing wrong.

My fields 
Lead Fields
Listing Agent = Listing_Agent__c
Listing Agent ID LR Source = Listing_Agent_ID_LR_Source__c 

Agent Fields
Agent Name = Name
Agent ID = Agent_ID__c

I wrote the trigger two different way but either will work. Please let me know what I am doing wrong.  Thank you.

Trigger One
trigger ListingAgent on Lead (before insert) {
 List<String> listingagentid=new list<string>();
    Map<string,Leads>mapvalues= new Map<String,Agent>()
        ;Listing_Agent_ID_LR_Source__c sobjTrigger.new{
            Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //compare Listing Agent LR ID Source and Agent ID 
        }
    List<Agent__c> agentvalues = [select id,AgentName from Agent__c where AgentID IN: Listing_Agent_ID_LR_Source__c];
    for(Lead sobjs : Trigger.new){
        for(Agent__c agc: agent Values){
            if(agc.Agent_ID__c == sobjs.Listing_Agent_ID_LR_Source__c){
                fieldListing_Agent= agc.Agent_Name__c
            }
        }
    }
}



This is my second trigger I wrote.

trigger ListingAgent on Lead (before insert) {
  List<string> listingAgent = new List<String>();
Map<String,Lead> mapValues = new Map<String,Lead>()
         for(Listing_Agent_ID_LR_Source__c sobj : Trigger.new){
               Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //Field name you want to compare
 
        }
List<agent__c> agentValues = [select id,Name from Agent__c where AgentId IN: Listing_Agent_ID_LR_Source__c];
 
   for(Lead : Trigger.new){
    (agent__c agc: agentValues){
       if(agc.Agent_Id__C == Listing_Agent_ID_LR_Source__c){
           Listing_Agent_c = agc.Agent_Name__c
        }
 
    }
}
}
 

Thank you 
  • January 29, 2015
  • Like
  • 0
I am trying to create a custom button on the Opportunity Page to update the stage.  After you click the button you select from the picklist and hit save. I have used similar code for a button on a list view but I am getting an error message: Field Save does not exist check spelling

Here is my code, I am not sure what I am doing wrong.  Why isn't Save an action not a field?  Thank you.
<apex:page standardController="Opportunity" showHeader="true">
        <apex:form>
        <apex:pageBlock title="Opportunity Stage-Update" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!opportunity.stagename}"> id="stagename">
                                    </apex:inputField>
                                               </apex:pageBlockSection>
           <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Select Stage" id="muselectedlist">
            <apex:pageBlockTable value="{!selected}" var="opp" id="mutab">
                                <apex:column value="{!opp.stagename}" id="oppstage"/>
            </apex:pageBlockTable>
           </apex:pageBlock> </apex:form>
        </apex:page>
 
  • January 28, 2015
  • Like
  • 0
Hello,  I am trying to create a custom button on a lead and opportunity page.  When the button is clicked, the checkbox on the field Lead Counts Against User will be checked.  I get an error message Unexpected Number when I try to click the button.  Here is my code;

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
var leadrcd = new sforce.SObject("Lead"); 
leadrcd.id = "{!Lead.Id}"; 
leadrcd.{!Lead.Lead_Counts_Against_User__c}=="TRUE"; 
sforce.connection.update([leadrcd]);

Please let me know what I am doing wrong.  I have tried it with one = also.

Thank you
  • January 28, 2015
  • Like
  • 0
I am created a formula field called "List Agent" (see below).  I wpuld like to take the Listing Agent ID LR Source field's information and look up in the Custom Object "Agent" to see if there is an Agent ID that matches,  If it does, I would like the Agent Name to be returned.  I can not get the formula to use the Listing Agent ID LR Source to search in the Agent Object.  Please help.  If this is not possible with a formula please advise my options.  Thank you 

IF( Listing_Agent_ID_LR_Source__c =  Agent__r.Agent_ID__c ,  Agent__r.Name , NULL)  
  • January 26, 2015
  • Like
  • 0
I am trying to create a custom button on a lead and opportunity that when clicked it will check a checkbox field.  I have tried a formula : (Lead.Lead_Counts_Against_User__c=True) and I get an error message Invalid Left-hand bside in Assignment. I have also tried code and get the same error message. Please help.  
  • January 26, 2015
  • Like
  • 0
I am trying to create a custom button on the Opportunity Page to update the stage.  After you click the button you select from the picklist and hit save. I have used similar code for a button on a list view but I am getting an error message:  Error: Unknown property 'OpportunityStandardController.selected'
 
Please help I am new at Visual Force. Thank you Here is my code;

<apex:page standardController="Opportunity" showHeader="true">
        <apex:form>
        <apex:pageBlock title="Opportunity Stage-Update" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!opportunity.stagename}"> id="stagename">
                                    </apex:inputField>
                                               </apex:pageBlockSection>
           <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!save}" id="butsav"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="butcan"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Select Stage" id="muselectedlist">
            <apex:pageBlockTable value="{!selected}" var="opp" id="mutab">
                                <apex:column value="{!opp.stagename}" id="oppstage"/>
            </apex:pageBlockTable>
           </apex:pageBlock> </apex:form>
        </apex:page> 
 
 
  • January 26, 2015
  • Like
  • 0
I need help creating an Apex trigger to get the lead assignment rule to trigger on our imported leads. I need it to run on all leads and I am not sure how to get it to work.  thank you

trigger LeadAssign on Lead (after update) 
{
 List<Id> lIds=new List<id>();
For (lead l:trigger.new){
    if (l.IsConverted==False){
        lIds.add(l.Id);   }
       }
if (AssignLeads.assignAlreadyCalled()==FALSE){
    system.debug('Assign already called? '+AssignLeads.assignAlreadyCalled());
    AssignLeads.Assign(lIds);
}
}
public global class AssignLeads{

public static Boolean assignAlreadyCalled=FALSE;

public static boolean assignAlreadyCalled(){
return assignAlreadyCalled;
}

@future
public static void assign(List<Id> lIds){
assignAlreadyCalled=TRUE;
List<Lead> leads=[SELECT Id FROM Lead WHERE Id IN: lIds];
For (lead l:leads){
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
l.setOptions(dmo);

}
update(leads);
}

}
  
  • January 27, 2016
  • Like
  • 0
I have created a trigger on a custom object called Trident Contract. It pulled the agent name from the custom object Agent using the Agent Id in the field Buyer Agent Lookup (it is a lookup field). Now I need to add the Buyer Agent Office with a look up from the Agent object mapping with   the Agent Id to the trigger. The Buyer Agent Lookup Field trigger was working until I tried to add the Buyer Agent Office.
Here is my originial trigger. Please advise how to add another field lookup for Buyer Agent Office. Thank you

trigger BuyAgent on Trident_Contract__c (before insert, before update) {
Set <String> AgentIDs = new Set <String>();
    for(Trident_Contract__c tc:trigger.new){
        if(tc.Buyer_Agent_ID__c !=NULL){
            AgentIDs.add(tc.Buyer_Agent_ID__c);
        } 
    }
    //Match Agent ID to Buyer Agent ID
    //Map ID 

   Map<String, Agent__c> Agents = new map <String, Agent__C>();
    
    for (Agent__c obj: [SELECT ID, Agent_ID__c
                        from Agent__c
                        Where Agent_ID__c 
                        IN: AgentIDs]){                            Agents.put(obj.Agent_ID__c,obj);}
for(Trident_Contract__c tc : trigger.new)
{if (Agents.containsKey(tc.Buyer_Agent_ID__c))
                tc.Buyer_Agent_Lookup__c = Agents.get(tc.Buyer_Agent_ID__c).ID;
 }
}
  • February 13, 2015
  • Like
  • 0
I have written a trigger (ListingAgent) trying to match fields from different objects, Opportunity and Agent. On the Opportunity page if the Listing Agent ID from LR (Listing_Agent_ID_From_LR__c) has a ID that matches an Agent ID from the Agent Object , I want the Agent Name (Name) to be populated in the Listing Agent Field. I get 87% coverage but an error for the last line. Does someone see a problem with the code? I can not see it. Please help. Thank you.
trigger Listingagent on Opportunity (before insert) {
    Set<String> lagid=new set<string>();
    for(opportunity o :Trigger.new){
        //if(o.listing_Agent_ID_From_LR__c != NULL)      
            lagid.add(o.Listing_Agent_ID_From_LR__c);
    }

    Map<String, String>matchid=new Map<String, String>();

    For(Agent__c ag: [select Agent_ID__c,
               Name
               from Agent__c 
               where Agent_ID__c 
                IN: lagid]){
        matchid.put(ag.Agent_ID__c, ag.Name);
    }
    For(Opportunity o : Trigger.new){
        if(matchid.containskey(o.Listing_Agent_ID_From_LR__c))
            o.Listing_Agent__c = matchid.get(o.Listing_Agent_ID_From_LR__c);
    }
}


 
  • February 02, 2015
  • Like
  • 0
Please help.  I am creating my first trigger called "List Agent".  I would like to take the"Lead" field 'Listing Agent ID LR Source' to look up in the Custom Object "Agent" to see if there is a match to the "Agent" field 'Agent ID' If it does match, I would like to take the information from the "Agent" field 'Agent Name' and insert it in the "Lead" Field 'Listing Name'.  I can not get the trigger to save and I am not sure what I am doing wrong.

My fields 
Lead Fields
Listing Agent = Listing_Agent__c
Listing Agent ID LR Source = Listing_Agent_ID_LR_Source__c 

Agent Fields
Agent Name = Name
Agent ID = Agent_ID__c

I wrote the trigger two different way but either will work. Please let me know what I am doing wrong.  Thank you.

Trigger One
trigger ListingAgent on Lead (before insert) {
 List<String> listingagentid=new list<string>();
    Map<string,Leads>mapvalues= new Map<String,Agent>()
        ;Listing_Agent_ID_LR_Source__c sobjTrigger.new{
            Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //compare Listing Agent LR ID Source and Agent ID 
        }
    List<Agent__c> agentvalues = [select id,AgentName from Agent__c where AgentID IN: Listing_Agent_ID_LR_Source__c];
    for(Lead sobjs : Trigger.new){
        for(Agent__c agc: agent Values){
            if(agc.Agent_ID__c == sobjs.Listing_Agent_ID_LR_Source__c){
                fieldListing_Agent= agc.Agent_Name__c
            }
        }
    }
}



This is my second trigger I wrote.

trigger ListingAgent on Lead (before insert) {
  List<string> listingAgent = new List<String>();
Map<String,Lead> mapValues = new Map<String,Lead>()
         for(Listing_Agent_ID_LR_Source__c sobj : Trigger.new){
               Listing_Agent_ID_LR_Source__c.add(sobj.AgentID); //Field name you want to compare
 
        }
List<agent__c> agentValues = [select id,Name from Agent__c where AgentId IN: Listing_Agent_ID_LR_Source__c];
 
   for(Lead : Trigger.new){
    (agent__c agc: agentValues){
       if(agc.Agent_Id__C == Listing_Agent_ID_LR_Source__c){
           Listing_Agent_c = agc.Agent_Name__c
        }
 
    }
}
}
 

Thank you 
  • January 29, 2015
  • Like
  • 0
I am trying to create a custom button on the Opportunity Page to update the stage.  After you click the button you select from the picklist and hit save. I have used similar code for a button on a list view but I am getting an error message: Field Save does not exist check spelling

Here is my code, I am not sure what I am doing wrong.  Why isn't Save an action not a field?  Thank you.
<apex:page standardController="Opportunity" showHeader="true">
        <apex:form>
        <apex:pageBlock title="Opportunity Stage-Update" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!opportunity.stagename}"> id="stagename">
                                    </apex:inputField>
                                               </apex:pageBlockSection>
           <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Select Stage" id="muselectedlist">
            <apex:pageBlockTable value="{!selected}" var="opp" id="mutab">
                                <apex:column value="{!opp.stagename}" id="oppstage"/>
            </apex:pageBlockTable>
           </apex:pageBlock> </apex:form>
        </apex:page>
 
  • January 28, 2015
  • Like
  • 0
Hello,  I am trying to create a custom button on a lead and opportunity page.  When the button is clicked, the checkbox on the field Lead Counts Against User will be checked.  I get an error message Unexpected Number when I try to click the button.  Here is my code;

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 
var leadrcd = new sforce.SObject("Lead"); 
leadrcd.id = "{!Lead.Id}"; 
leadrcd.{!Lead.Lead_Counts_Against_User__c}=="TRUE"; 
sforce.connection.update([leadrcd]);

Please let me know what I am doing wrong.  I have tried it with one = also.

Thank you
  • January 28, 2015
  • Like
  • 0
I am trying to create a custom button on a lead and opportunity that when clicked it will check a checkbox field.  I have tried a formula : (Lead.Lead_Counts_Against_User__c=True) and I get an error message Invalid Left-hand bside in Assignment. I have also tried code and get the same error message. Please help.  
  • January 26, 2015
  • Like
  • 0
I am trying to create a custom button on the Opportunity Page to update the stage.  After you click the button you select from the picklist and hit save. I have used similar code for a button on a list view but I am getting an error message:  Error: Unknown property 'OpportunityStandardController.selected'
 
Please help I am new at Visual Force. Thank you Here is my code;

<apex:page standardController="Opportunity" showHeader="true">
        <apex:form>
        <apex:pageBlock title="Opportunity Stage-Update" mode="edit" id="mub1">
            <apex:pageMessages />
            <apex:pageBlockSection id="mus1">
                <apex:inputField value="{!opportunity.stagename}"> id="stagename">
                                    </apex:inputField>
                                               </apex:pageBlockSection>
           <apex:pageBlockButtons location="bottom" id="mubut">
                <apex:commandButton value="Save" action="{!save}" id="butsav"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="butcan"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pageBlock title="Select Stage" id="muselectedlist">
            <apex:pageBlockTable value="{!selected}" var="opp" id="mutab">
                                <apex:column value="{!opp.stagename}" id="oppstage"/>
            </apex:pageBlockTable>
           </apex:pageBlock> </apex:form>
        </apex:page> 
 
 
  • January 26, 2015
  • Like
  • 0