• AntonyWarc
  • NEWBIE
  • 10 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 9
    Questions
  • 19
    Replies
Hi all,

The below trigger was written for us to auto-create a renewal opportunity when a contract is saved. All is fine except we are now in the position of needing more than one product line item on an opp. When testing this, I get an error saying more than one product line item found (error is triggered by line 53). What do I need to change to get this code to select all product line items from the original opp and insert into the new opp?

Thanks in advance,

Antony
 
trigger ContractRenewalOpportunityCreate on Contract (after insert) {

    Contract contract = Trigger.new[0];
    
    //if contract was created with'Create Contract' button on opportunity then create renewal opportunity
    if (contract.Create_Renewal_Opportunity__c == true) {
        
        Opportunity currentOpp = [SELECT Name, 
                                          AccountId,
                                          Account.Name,
                                          Account.Warc_SubRef__c,
                                          Contact__r.Name,
                                          Contact__c,
                                          COF_Description__c,
                                          Description,
                                          Large_Agency__c,
                                          Budget_Assumption__c,
                                          Budgeted_Value_Local__c,
                                          Budgeted_Value_Assumption__c,
                                          Original_Budgeted_amount__c,
                                          Renewal_Forecast_Value__c,
                                          Original_Budgeted_local_amount__c,
                                          Last_Year_Local_value__c,
                                          Last_Year_GBP_value__c,
                                          Subscription_Type__c,
                                          Local_Client_Service_Manager__r.Name,
                                          Local_Client_Service_Manager__c,
                                          Sales_Manager__r.Name,
                                          Sales_Manager__c,
                                          Currency__c,
                                          CurrencyISOCode,
                                          CloseDate,
                                          LeadSource,
                                          Master_Password__c,
                                          Master_Username__c,
                                          Year_1__c,
                                          VAT_Code__c,
                                          Type,
                                          (SELECT OpportunityId, 
                                                  PricebookEntryId, 
                                                  Quantity,  
                                                  ServiceDate, 
                                                  Description, 
                                                  UnitPrice 
                                                  FROM OpportunityLineItems)
                                          FROM Opportunity WHERE Id=:contract.Opportunity__c];         
                
        OpportunityLineItem currentOppLineItem = currentOpp.OpportunityLineItems;
        
        //calc renewal opportunity close date
        Date renewalOppCloseDate;        
        String RenewalOppYr1;
        //If Ren opp is to be created from New business opp
        if(currentOpp.Type == 'New Business'){
            RenewalOppYr1 = 'Yes'; 
            renewalOppCloseDate = currentOpp.CloseDate.addYears(1).addMonths(1);   
            renewalOppCloseDate = Date.newInstance(renewalOppCloseDate.Year(), renewalOppCloseDate.Month(), 1);
        }
        else if(currentOpp.Type == 'Existing Business'){
            RenewalOppYr1 = 'No';
            renewalOppCloseDate = currentOpp.CloseDate.addYears(1);  
        }
        
        //Build renewal opportunity name
        if(renewalOppCloseDate != null){
            String renewalOppName = contract.id + '-Ren' + renewalOppCloseDate.year();
            if (renewalOppCloseDate.month() < 10) {
                renewalOppName += '0' + renewalOppCloseDate.month();
            } else {
            renewalOppName += renewalOppCloseDate.month();
            }

            //Fathom edit (tmh): get record type ID for Renewal record type 
            Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Renewal').getRecordTypeId(); 
            
            
            //create renewal opportunity
            Opportunity renewalOpp = new Opportunity(Name=renewalOppName,
                                                 Type='Existing Business',
                                                 Subscription_Type__c=currentOpp.Subscription_Type__c,
                                                 Description=currentOpp.Description,
                                                 COF_Description__c=currentOpp.COF_Description__c,
                                                 Large_Agency__c=currentOpp.Large_Agency__c,
                                                 Budget_Assumption__c=currentOpp.Budget_Assumption__c,
                                                 Budgeted_Value_Local__c=currentOpp.Budgeted_Value_Local__c,
                                                 Budgeted_Value_Assumption__c=currentOpp.Budgeted_Value_Assumption__c,
                                                 Original_Budgeted_amount__c=currentOpp.Original_Budgeted_amount__c,
                                                 //Renewal_Forecast_Value__c=currentOpp.Renewal_Forecast_Value__c,
                                                 Original_Budgeted_local_amount__c=currentOpp.Original_Budgeted_local_amount__c,
                                                 Last_Year_Local_value__c=currentOpp.Last_Year_Local_value__c,
                                                 Last_Year_GBP_value__c=currentOpp.Last_Year_GBP_value__c,
                                                 Local_Client_Service_Manager__c=currentOpp.Local_Client_Service_Manager__c,
                                                 Contact__c=currentOpp.Contact__c,
                                                 Sales_Manager__c=currentOpp.Sales_Manager__c,
                                                 Currency__c=currentOpp.Currency__c,
                                                 CurrencyISOCode=currentOpp.CurrencyISOCode,
                                                 AccountId=currentOpp.AccountId,
                                                 CloseDate=renewalOppCloseDate,
                                                 StageName='D - Interested',  // Fathom Edit (tmh): Change from 'Renewal M/Y' 
                                                 LeadSource='Renewal',
                                                 Year_1__c = RenewalOppYr1, //AR Additoon 300117
                                                 Master_Password__c=currentOpp.Master_Password__c,
                                                 Master_Username__c=currentOpp.Master_Username__c,
                                                 VAT_Code__c=currentOpp.VAT_Code__c,
                                                 renewal_category__c = 'Anticipated Yes',   // Fathom Edit (tmh): add the renewal category to the opp.
                                                 recordTypeId = devRecordTypeId         // Fathom Edit (tmh): add the record type 
                                                 );
            insert renewalOpp;

            //create add product to renewal opportunity
            OpportunityLineItem renewalOppLineItem = new OpportunityLineItem(OpportunityId=renewalOpp.Id,
                                                                          PricebookEntryId=currentOppLineItem.PricebookEntryId,
                                                                          Quantity=currentOppLineItem.Quantity,
                                                                          ServiceDate=currentOppLineItem.ServiceDate,
                                                                          Description=currentOppLineItem.Description,
                                                                          UnitPrice=currentOppLineItem.UnitPrice);
            insert renewalOppLineItem;
        }    
    }    
}

 
Hi all,

I have built this cutom button javascript which works when a new record is created, but if number of offices = number of allowed offices, I get this error "cannot read property [0] of undefined after my bespoke error message has displayed and you click OK.

Basically I want to click on the button, if the number of offices is already equal to the number of allowed ofices, display the message, then refresh the page. As I said if my built in validation rule = false, then the record is created and I'm taken to the page defined as expected.

Any help appreciated.  

Antony
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var alow = new sforce.SObject("Allowed_Offices__c");
alow.contract__c="{!Contract.Id}"
if('{!Contract.Allowed_office_count__c}' == '{!Contract.Number_of_offices__c}')
{alert('No more offices can be added to this contract');}
else{
var result = sforce.connection.create([alow]);};
if(result[0].getBoolean("success")){window.location = "/" + result[0].id;}
else{window.location.reload()
}

 

Hi,

 

We have a 3rd party webserivce that send leads from our site to SF with Lead Owner already assigned based on some coded rules. We now want to add a level of lead assignment above what this can handle, so I wrote a lead assignment rule and have activated it.

 

The rules seem to be completley ignored as leads are coming in, but work if a lead is edited and saved ticking the "use active assignmnet rules" checkbox. (i.e the lead assignment crieria works, but its not being triggered)

 

So I thought a trigger to re-run lead assignment directly after a lead has been created would be the only solution, issue is I have no coding experience (aside from one easy update trigger some guys on here helped with!)

 

Can anyone help? Or if another solution exists that woudl be great too!

 

Antony

Hi,

 

We have a 3rd party webserivce that send leads from our site to SF with Lead Owner already assigned based on some coded rules. We now want to add a level of lead assignment above what this can handle, so I wrote a lead assignment rule and have activated it.

 

The rules seem to be completley ignored as leads are coming in, but work if a lead is edited and saved ticking the "use active assignmnet rules" checkbox. (i.e the lead assignment crieria works, but its not being triggered)

 

So I thought a trigger to re-run lead assignment directly after a lead has been created would be the only solution, issue is I have no coding experience (aside from one easy update trigger some guys on here helped with!)

 

Can anyone help? Or if another solution exists that woudl be great too!

 

Antony

Hi

 

I have no Apex training but have managed to piece this trigger together with help from these boards! Now i have my trigger in place I need to update all my leads to use it. However I get the  System.LimitException: Too many SOQL queries: 101 error when mass-updating leads.

 

I understand the concept that I'm calling too many SOQL queiers. Anyone willing to help re-code this trigger to bring teh SOQL out of the for loop?

 

trigger updateCCLookupField on Lead (before insert, before update) {

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

  for (Lead l : Trigger.new)
  { 
  Try
  {
     Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
    l.CC_Lookup__c = AssociatedCC.Id;
  }
  Catch(Exception e)
  {
  l.CC_Lookup__c = null;
  } 
  }
}

Hi

 

I have no Apex training but have managed to piece this trigger together with help from these boards! Now i have my trigger in place I need to update all my leads to use it. However I get the  System.LimitException: Too many SOQL queries: 101 error when mass-updating leads.

 

I understand the concept that I'm calling too many SOQL queiers. Anyone willing to help re-code this trigger to bring teh SOQL out of the for loop?

 

trigger updateCCLookupField on Lead (before insert, before update) {

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

  for (Lead l : Trigger.new)
  { 
  Try
  {
     Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
    l.CC_Lookup__c = AssociatedCC.Id;
  }
  Catch(Exception e)
  {
  l.CC_Lookup__c = null;
  } 
  }
}

I have no Apex coding experience, can someone help me write a test class for the following Apex Trigger?

 

trigger updateCCLookupField on Lead (before insert, before update) {

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

for (Lead l : Trigger.new)
{
Try
{
Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
l.CC_Lookup__c = AssociatedCC.Id;
}
Catch(Exception e)
{
l.CC_Lookup__c = 'a0zD00000028X59';
}
}
}

Hi,

 

First of all I have no Apex experience at all, and up until now havent needed any, so be kind! I have my sandbox built and have done some reading up on Apex language (last time I programmed  was PASCAL at college!)

 

I need to populate a custom lookup field on our Lead object based on it referencing a field on a custom object. As Leads can't be a child of a master-Child relationship, I've now realised the only way to do this will be an Apex trigger.

 

I've created a relationship between the Lead and custom object and now need the lookup field (CCLookup) on the Lead to use the value from Campaign Code (also on Lead) look up the value of the Field 'Campaign Code_CC' on my custom object and return the value of the custom object Name:

 

Campaign code on Lead: 1234

Campaign Code on custom Object: 1234

Name ID of Custom object: Campaign 1234

Lookup up field returns value of: Campaign1234

 

If no campaign code that matches exists on the custom object, the lookup field can remain blank.

 

Anyone willing to help!?

 

Antony

Hi,

 

First of all I have no Apex experience at all, and up until now havent needed any, so be kind! I have my sandbox built and have done some reading up on Apex language (last time I programmed  was PASCAL at college!)

 

I need to populate a custom lookup field on our Lead object based on it referencing a field on a custom object. As Leads can't be a child of a master-Child relationship, I've now realised the only way to do this will be an Apex trigger.

 

I've created a relationship between the Lead and custom object and now need the lookup field (CCLookup) on the Lead to use the value from Campaign Code (also on Lead) look up the value of the Field 'Campaign Code_CC' on my custom object and return the value of the custom object Name:

 

Campaign code on Lead: 1234

Campaign Code on custom Object: 1234

Name ID of Custom object: Campaign 1234

Lookup up field returns value of: Campaign1234

 

If no campaign code that matches exists on the custom object, the lookup field can remain blank.

 

Anyone willing to help!?

 

Antony

 

 

Hi,

 

We have a 3rd party webserivce that send leads from our site to SF with Lead Owner already assigned based on some coded rules. We now want to add a level of lead assignment above what this can handle, so I wrote a lead assignment rule and have activated it.

 

The rules seem to be completley ignored as leads are coming in, but work if a lead is edited and saved ticking the "use active assignmnet rules" checkbox. (i.e the lead assignment crieria works, but its not being triggered)

 

So I thought a trigger to re-run lead assignment directly after a lead has been created would be the only solution, issue is I have no coding experience (aside from one easy update trigger some guys on here helped with!)

 

Can anyone help? Or if another solution exists that woudl be great too!

 

Antony

Hi all,

The below trigger was written for us to auto-create a renewal opportunity when a contract is saved. All is fine except we are now in the position of needing more than one product line item on an opp. When testing this, I get an error saying more than one product line item found (error is triggered by line 53). What do I need to change to get this code to select all product line items from the original opp and insert into the new opp?

Thanks in advance,

Antony
 
trigger ContractRenewalOpportunityCreate on Contract (after insert) {

    Contract contract = Trigger.new[0];
    
    //if contract was created with'Create Contract' button on opportunity then create renewal opportunity
    if (contract.Create_Renewal_Opportunity__c == true) {
        
        Opportunity currentOpp = [SELECT Name, 
                                          AccountId,
                                          Account.Name,
                                          Account.Warc_SubRef__c,
                                          Contact__r.Name,
                                          Contact__c,
                                          COF_Description__c,
                                          Description,
                                          Large_Agency__c,
                                          Budget_Assumption__c,
                                          Budgeted_Value_Local__c,
                                          Budgeted_Value_Assumption__c,
                                          Original_Budgeted_amount__c,
                                          Renewal_Forecast_Value__c,
                                          Original_Budgeted_local_amount__c,
                                          Last_Year_Local_value__c,
                                          Last_Year_GBP_value__c,
                                          Subscription_Type__c,
                                          Local_Client_Service_Manager__r.Name,
                                          Local_Client_Service_Manager__c,
                                          Sales_Manager__r.Name,
                                          Sales_Manager__c,
                                          Currency__c,
                                          CurrencyISOCode,
                                          CloseDate,
                                          LeadSource,
                                          Master_Password__c,
                                          Master_Username__c,
                                          Year_1__c,
                                          VAT_Code__c,
                                          Type,
                                          (SELECT OpportunityId, 
                                                  PricebookEntryId, 
                                                  Quantity,  
                                                  ServiceDate, 
                                                  Description, 
                                                  UnitPrice 
                                                  FROM OpportunityLineItems)
                                          FROM Opportunity WHERE Id=:contract.Opportunity__c];         
                
        OpportunityLineItem currentOppLineItem = currentOpp.OpportunityLineItems;
        
        //calc renewal opportunity close date
        Date renewalOppCloseDate;        
        String RenewalOppYr1;
        //If Ren opp is to be created from New business opp
        if(currentOpp.Type == 'New Business'){
            RenewalOppYr1 = 'Yes'; 
            renewalOppCloseDate = currentOpp.CloseDate.addYears(1).addMonths(1);   
            renewalOppCloseDate = Date.newInstance(renewalOppCloseDate.Year(), renewalOppCloseDate.Month(), 1);
        }
        else if(currentOpp.Type == 'Existing Business'){
            RenewalOppYr1 = 'No';
            renewalOppCloseDate = currentOpp.CloseDate.addYears(1);  
        }
        
        //Build renewal opportunity name
        if(renewalOppCloseDate != null){
            String renewalOppName = contract.id + '-Ren' + renewalOppCloseDate.year();
            if (renewalOppCloseDate.month() < 10) {
                renewalOppName += '0' + renewalOppCloseDate.month();
            } else {
            renewalOppName += renewalOppCloseDate.month();
            }

            //Fathom edit (tmh): get record type ID for Renewal record type 
            Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Renewal').getRecordTypeId(); 
            
            
            //create renewal opportunity
            Opportunity renewalOpp = new Opportunity(Name=renewalOppName,
                                                 Type='Existing Business',
                                                 Subscription_Type__c=currentOpp.Subscription_Type__c,
                                                 Description=currentOpp.Description,
                                                 COF_Description__c=currentOpp.COF_Description__c,
                                                 Large_Agency__c=currentOpp.Large_Agency__c,
                                                 Budget_Assumption__c=currentOpp.Budget_Assumption__c,
                                                 Budgeted_Value_Local__c=currentOpp.Budgeted_Value_Local__c,
                                                 Budgeted_Value_Assumption__c=currentOpp.Budgeted_Value_Assumption__c,
                                                 Original_Budgeted_amount__c=currentOpp.Original_Budgeted_amount__c,
                                                 //Renewal_Forecast_Value__c=currentOpp.Renewal_Forecast_Value__c,
                                                 Original_Budgeted_local_amount__c=currentOpp.Original_Budgeted_local_amount__c,
                                                 Last_Year_Local_value__c=currentOpp.Last_Year_Local_value__c,
                                                 Last_Year_GBP_value__c=currentOpp.Last_Year_GBP_value__c,
                                                 Local_Client_Service_Manager__c=currentOpp.Local_Client_Service_Manager__c,
                                                 Contact__c=currentOpp.Contact__c,
                                                 Sales_Manager__c=currentOpp.Sales_Manager__c,
                                                 Currency__c=currentOpp.Currency__c,
                                                 CurrencyISOCode=currentOpp.CurrencyISOCode,
                                                 AccountId=currentOpp.AccountId,
                                                 CloseDate=renewalOppCloseDate,
                                                 StageName='D - Interested',  // Fathom Edit (tmh): Change from 'Renewal M/Y' 
                                                 LeadSource='Renewal',
                                                 Year_1__c = RenewalOppYr1, //AR Additoon 300117
                                                 Master_Password__c=currentOpp.Master_Password__c,
                                                 Master_Username__c=currentOpp.Master_Username__c,
                                                 VAT_Code__c=currentOpp.VAT_Code__c,
                                                 renewal_category__c = 'Anticipated Yes',   // Fathom Edit (tmh): add the renewal category to the opp.
                                                 recordTypeId = devRecordTypeId         // Fathom Edit (tmh): add the record type 
                                                 );
            insert renewalOpp;

            //create add product to renewal opportunity
            OpportunityLineItem renewalOppLineItem = new OpportunityLineItem(OpportunityId=renewalOpp.Id,
                                                                          PricebookEntryId=currentOppLineItem.PricebookEntryId,
                                                                          Quantity=currentOppLineItem.Quantity,
                                                                          ServiceDate=currentOppLineItem.ServiceDate,
                                                                          Description=currentOppLineItem.Description,
                                                                          UnitPrice=currentOppLineItem.UnitPrice);
            insert renewalOppLineItem;
        }    
    }    
}

 
Hi all,

I have built this cutom button javascript which works when a new record is created, but if number of offices = number of allowed offices, I get this error "cannot read property [0] of undefined after my bespoke error message has displayed and you click OK.

Basically I want to click on the button, if the number of offices is already equal to the number of allowed ofices, display the message, then refresh the page. As I said if my built in validation rule = false, then the record is created and I'm taken to the page defined as expected.

Any help appreciated.  

Antony
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var alow = new sforce.SObject("Allowed_Offices__c");
alow.contract__c="{!Contract.Id}"
if('{!Contract.Allowed_office_count__c}' == '{!Contract.Number_of_offices__c}')
{alert('No more offices can be added to this contract');}
else{
var result = sforce.connection.create([alow]);};
if(result[0].getBoolean("success")){window.location = "/" + result[0].id;}
else{window.location.reload()
}

 

Hi,

 

We have a 3rd party webserivce that send leads from our site to SF with Lead Owner already assigned based on some coded rules. We now want to add a level of lead assignment above what this can handle, so I wrote a lead assignment rule and have activated it.

 

The rules seem to be completley ignored as leads are coming in, but work if a lead is edited and saved ticking the "use active assignmnet rules" checkbox. (i.e the lead assignment crieria works, but its not being triggered)

 

So I thought a trigger to re-run lead assignment directly after a lead has been created would be the only solution, issue is I have no coding experience (aside from one easy update trigger some guys on here helped with!)

 

Can anyone help? Or if another solution exists that woudl be great too!

 

Antony

Hi,

 

We have a 3rd party webserivce that send leads from our site to SF with Lead Owner already assigned based on some coded rules. We now want to add a level of lead assignment above what this can handle, so I wrote a lead assignment rule and have activated it.

 

The rules seem to be completley ignored as leads are coming in, but work if a lead is edited and saved ticking the "use active assignmnet rules" checkbox. (i.e the lead assignment crieria works, but its not being triggered)

 

So I thought a trigger to re-run lead assignment directly after a lead has been created would be the only solution, issue is I have no coding experience (aside from one easy update trigger some guys on here helped with!)

 

Can anyone help? Or if another solution exists that woudl be great too!

 

Antony

Hi

 

I have no Apex training but have managed to piece this trigger together with help from these boards! Now i have my trigger in place I need to update all my leads to use it. However I get the  System.LimitException: Too many SOQL queries: 101 error when mass-updating leads.

 

I understand the concept that I'm calling too many SOQL queiers. Anyone willing to help re-code this trigger to bring teh SOQL out of the for loop?

 

trigger updateCCLookupField on Lead (before insert, before update) {

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

  for (Lead l : Trigger.new)
  { 
  Try
  {
     Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
    l.CC_Lookup__c = AssociatedCC.Id;
  }
  Catch(Exception e)
  {
  l.CC_Lookup__c = null;
  } 
  }
}

Hi

 

I have no Apex training but have managed to piece this trigger together with help from these boards! Now i have my trigger in place I need to update all my leads to use it. However I get the  System.LimitException: Too many SOQL queries: 101 error when mass-updating leads.

 

I understand the concept that I'm calling too many SOQL queiers. Anyone willing to help re-code this trigger to bring teh SOQL out of the for loop?

 

trigger updateCCLookupField on Lead (before insert, before update) {

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

  for (Lead l : Trigger.new)
  { 
  Try
  {
     Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
    l.CC_Lookup__c = AssociatedCC.Id;
  }
  Catch(Exception e)
  {
  l.CC_Lookup__c = null;
  } 
  }
}

I have no Apex coding experience, can someone help me write a test class for the following Apex Trigger?

 

trigger updateCCLookupField on Lead (before insert, before update) {

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

for (Lead l : Trigger.new)
{
Try
{
Campaign_Codes__c AssociatedCC = [SELECT Id FROM Campaign_Codes__c WHERE CodeOnCC__c= :l.Campaign_Code__c];
l.CC_Lookup__c = AssociatedCC.Id;
}
Catch(Exception e)
{
l.CC_Lookup__c = 'a0zD00000028X59';
}
}
}

Hi,

 

First of all I have no Apex experience at all, and up until now havent needed any, so be kind! I have my sandbox built and have done some reading up on Apex language (last time I programmed  was PASCAL at college!)

 

I need to populate a custom lookup field on our Lead object based on it referencing a field on a custom object. As Leads can't be a child of a master-Child relationship, I've now realised the only way to do this will be an Apex trigger.

 

I've created a relationship between the Lead and custom object and now need the lookup field (CCLookup) on the Lead to use the value from Campaign Code (also on Lead) look up the value of the Field 'Campaign Code_CC' on my custom object and return the value of the custom object Name:

 

Campaign code on Lead: 1234

Campaign Code on custom Object: 1234

Name ID of Custom object: Campaign 1234

Lookup up field returns value of: Campaign1234

 

If no campaign code that matches exists on the custom object, the lookup field can remain blank.

 

Anyone willing to help!?

 

Antony