• Depton
  • NEWBIE
  • 480 Points
  • Member since 2011

  • Chatter
    Feed
  • 19
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 156
    Questions
  • 268
    Replies

I am trying to populate Primary_Contact__c ( contact look up field in opportunities) when converting a lead.

 

The following code populates the standard opportunity contact role with the lead  name and makes the lead as the primary contact.

 

I am looking for a similar behavior but instead of populating the contact role,  automatically populate the Primary_Contact__c field with the Lead/Contact name

 

What should I change to get the opportunity - Primary_Contact__c ?

Thanks

 

 

trigger trigMapFields on Lead (before update) {

    
    List<Id> conAcctId = new List<Id>(); // List of the converted Account IDs
    List<Id> conOppId = new List<Id>(); // List of the converted Opportunity IDs
    Map<Id,String> leadStatus = new Map<Id,String>(); // Map of the converted Contact ID and the Lead Status
    Map<Id,Id> leadContactMap = new Map<Id,Id>(); // Map of the Lead ID and the converted contact Id (for expense)
    Map<Id,Id> leadAccountMap = new Map<Id,Id>(); // Map of the Lead ID and the converted account Id (for expense)


    for(Lead lead : Trigger.new) {
        if (lead.IsConverted) {
            leadStatus.put(lead.ConvertedContactId,lead.Status);
            conAcctId.add(lead.ConvertedAccountId);
            if (lead.ConvertedOpportunityId != null) {
                conOppId.add(lead.ConvertedOpportunityId);
            }
        leadContactMap.put(lead.ID,lead.ConvertedContactId);
        leadAccountMap.put(lead.ID,lead.ConvertedAccountId);
        }
    }

    // If an opportunity was created, need to assign the contact role as primary
    List<OpportunityContactRole> oppConRole = [select ContactID from OpportunityContactRole where OpportunityId IN :conOppId];
    for ( OpportunitycontactRole cr :oppConRole) {
        cr.IsPrimary = true;
    }
    update oppConRole;

}

 

Hi,

 

I have a trigger that counts the number of close cases for each contact, now I want to send an email alert each times when the close case is , 1,3,6,9,12,15.....etc....

 

 

Any ideas on setting this rule without having to write all numbers until 500!!!

Any tricks??

 

Thank you

/* Provide summary of Number of Cases on Contacts record */ 

trigger CaseSumTrigger on Case(after delete, after insert, after undelete, 
after update) {

    Case [] cas;
    if (Trigger.isDelete) 
        cas= Trigger.old;
    else
        cas= Trigger.new;

    // get list of Contacts
    Set<ID> contIds = new Set<ID>();
    for (Case cs : cas) {
            contIds.add(cs.ContactId);
    }
    
    Map<ID, Case> CasesForContacts = new Map<ID, Case>([select Id
                                                            ,ContactId
                                                            from Case
                                                            where ContactId in :contIds]);

    Map<ID, Contact> contToUpdate = new Map<ID, Contact>([select Id
                                                                 ,items__c
                                                                  from Contact
                                                                  where Id in :contIds]);
                                                                 
    for (Contact  cont: contToUpdate.values()) {
        Set<ID> casIds = new Set<ID>();
        for (Case cs : CasesForContacts.values()) {
            if (cs.ContactId == cont.Id)
                casIds.add(cs.Id);
        }
        if (cont.items__c != casIds.size())
            cont.items__c = casIds.size();
    }

    update contToUpdate.values();

}

 

How/Where do i add the loop to get only CLOSED cases?

Thank you!

 

Hi all,

 

 

I cannot create a Roll-Up Summaryfield on Contats to see the total of close cases in the contact detail page.

 

Any sample code for this?

 

Tank you

 


Hi,

 

I am trying to set up a workflow email alert to be send to the owner of the record 7 days before the Due Date (date/time field ) if the picklist value Attending__c is not equals  to "Yes"

 

Thank you

 

 

Hi .

I have created a visualforce email template and using it in the workflow email  alert. When i test the template it works fine. But when I test it with a different user wh o is not admin, i am seeing blank emails. Any ideas of why this is causing an issue?

Hi,

 

I am running crazy trying to calculate age since a custom date/time field gets populated. Avoiding weekends.

I thought I founded the formula but is giving me some errors.

 

Anyone has a formula that counts the hours bases on a date/time field?. Avoiding weekends.

 

So it will count 24 hours from Monday to Friday.

Zero hours Saturday and Sunday!

 

Thanks.

Hi everyone,

 

I am trying to set up a field update to determine the correct  links to be sent using ISPICKVAL.

 

I have three links (survey links) that has to be sent based on which one you select from the drop down box.

 

For example if you select Germany, it should send the survey link in german language, if you select England, English and so on.

 

Many thanks,

 

Hi,

 

Anyone has an idea to avouid the error: Thanks!!:)

 

IF( date1__c > 2, 
IMAGE("/img/samples/color_red.gif", "red", 30, 30),
IF( date1__c > 1,
IMAGE("/img/samples/color_yellow.gif", "yellow", 30, 30),
IMAGE("/img/samples/color_green.gif", "green", 30, 30)))

 Error: Compiled formula is too big to execute (5,509 characters). Maximum size is 5,000 characters

 

 

I am trying to create a workflow to notify the contact when the case owner changes from the queue to an specific user.

 

I am trying something like

 

 

AND( 
ISCHANGED(OwnerId),
NOT(OwnerId  = "00G30000002CJIA"))

 but getting this error:

 

Function ISCHANGED may not be used in this type of formula

 

Any idea on how can I create this workflow? Thank you

1.Create a field for business days using a number formula field. Use the following formula to calculate minus weekends.

 

CASE( 
MOD(DATEVALUE(CreatedDate) - DATE(1900, 1, 7), 7), 
0, (TODAY() - DATEVALUE(CreatedDate)) - 1 - FLOOR((TODAY() - DATEVALUE(CreatedDate))/7)*2, 
1, (TODAY() - DATEVALUE(CreatedDate)) - FLOOR((TODAY() - DATEVALUE(CreatedDate))/7)*2, 
2, (TODAY() - DATEVALUE(CreatedDate)) - FLOOR((TODAY() - DATEVALUE(CreatedDate))/7)*2, 
3, (TODAY() - DATEVALUE(CreatedDate)) - FLOOR((TODAY() - DATEVALUE(CreatedDate))/7)*2, 
4, (TODAY() - DATEVALUE(CreatedDate)) - FLOOR((TODAY() - DATEVALUE(CreatedDate))/7)*2, 
5, (TODAY() - DATEVALUE(CreatedDate)) - 2 - FLOOR((TODAY() - DATEVALUE(CreatedDate))/7)*2, 
6, (TODAY() - DATEVALUE(CreatedDate)) - 2 - FLOOR((TODAY() - DATEVALUE(CreatedDate))/7)*2, 
null)

 

 

2.Create a date/time field for the current date/time 

 

3.Create workflow rule to update this field every time it is edited with formula below

 

Now()

 

 

4.Create a number formula field to calculate the age difference of the case today. I used the formula

 

VALUE( MID( TEXT( NOW__c ), 12, 2 ) ) - VALUE( MID( TEXT( CreatedDate ), 12, 2 ) )

 

 

5.Create a number formula field for the business hours of the company ie. 8 hrs

 

 

 

6.Create a number formula field to calculate the total age of the case in business hours

 

(business_days__c * business_hours ) + age_difference_of_case

 

I think I might be a little bit blind here, because I cannot see why a validation rule should be throwing up an exception.

 

The only thing that I can fathom that would create the error would be if the associated test class was setting an illegal date - i.e. one that is not on the first of the month.

 

However, the only Start Dates set are:

Start_Date__c=date.newinstance(2011, 1, 1)

Start_Date__c=date.newinstance(2011, 7, 1)

which are both on the first of the month!



 

So, first, the error:

Error:Apex trigger ItemandIPISubscriptionLinks caused an unexpected exception, contact your administrator: ItemandIPISubscriptionLinks: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a092000000KcY3sAAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Start Date must be the first of the month.: [Start_Date__c]: Trigger.ItemandIPISubscriptionLinks: line 223, column 1

 Now the validation rule:

AND (
NOT ( ISBLANK ( Start_Date__c )),
DAY ( Start_Date__c ) > 1
)

 Now the Trigger:

trigger ItemandIPISubscriptionLinks on Item__c (after update, after insert) {

Set<Id> itemIds = new Set<Id>();
Set<Id> eventIds = new Set<Id>();
Set<Id> itemCountIds = new Set<Id>();


//Put the list value ids in the set
if (Trigger.isUpdate){
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].RecordTypeId == '01220000000AGp1' &&
            ((Trigger.new[i].Probability__c == 100 && Trigger.new[i].Cancelled__c == FALSE 
            && 
            ((Trigger.old[i].Probability__c <> 100 || Trigger.old[i].Cancelled__c <> FALSE)
            || 
            (Trigger.new[i].Start_Date__c <> Trigger.old[i].Start_Date__c ||
            Trigger.new[i].Term__c <> Trigger.old[i].Term__c ||
            Trigger.new[i].Full_Amount2__c <> Trigger.old[i].Full_Amount2__c)))
            
            ||
            
            (Trigger.new[i].Probability__c <> 100 || Trigger.new[i].Cancelled__c <> FALSE) 
            && 
            Trigger.old[i].Probability__c == 100 && Trigger.old[i].Cancelled__c == FALSE)
           )
        {
            itemIds.add(Trigger.new[i].id);
            eventIds.add(Trigger.new[i].Event_Issue__c);
        }
    }
}
if (Trigger.isInsert){
    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].RecordTypeId == '01220000000AGp1' && Trigger.new[i].Probability__c == 100 && Trigger.new[i].Cancelled__c == FALSE) {
            itemIds.add(Trigger.new[i].id);
            eventIds.add(Trigger.new[i].Event_Issue__c);
        }
    }
}

IPI_Subscription__c[] sub =
[SELECT Id, Event_Issue__c, Month__c FROM IPI_Subscription__c WHERE Event_Issue__c IN :eventIds];

Item__c[] ite =
[SELECT Id, Event_Issue__c, Probability__c, Cancelled__c, Start_Date__c, End_Date__c, Term__c, Amount_Formula__c, Monthly_Amount__c, Jan_Amount__c, Feb_Amount__c, Mar_Amount__c,
Apr_Amount__c, May_Amount__c, Jun_Amount__c, Jul_Amount__c, Aug_Amount__c, Sep_Amount__c, Oct_Amount__c, 
Nov_Amount__c, Dec_Amount__c FROM Item__c WHERE Event_Issue__c IN :eventIds];

for (IPI_Subscription__c subs :sub){
    Decimal amou = 0;
    Integer n = 0;

for (Item__c items :ite){
    
    if (items.Probability__c == 100 && items.Cancelled__c == FALSE){
    
    //January 
        if (subs.Month__c == 'Jan'){
            if (items.Jan_Amount__c>0){
                items.Jan_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                n += 1;
                subs.Amount_Sold2__c = amou;
            }
            else
                items.Jan_Subscription__c = NULL;
        }

    //February
        else if (subs.Month__c == 'Feb'){
            if (items.Feb_Amount__c>0){
                items.Feb_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Feb_Subscription__c = NULL;
        }
       
    //March
        else if (subs.Month__c == 'Mar'){
            if (items.Mar_Amount__c>0){
                items.Mar_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Mar_Subscription__c = NULL;
        }
    
    //April
        else if (subs.Month__c == 'Apr'){
            if (items.Apr_Amount__c>0){
                items.Apr_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Apr_Subscription__c = NULL;
        }
    
    //May
        else if (subs.Month__c == 'May'){
            if (items.May_Amount__c>0){
                items.May_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.May_Subscription__c = NULL;
        }
    
    //Jun
        else if (subs.Month__c == 'Jun'){
            if (items.Jun_Amount__c>0){
                items.Jun_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Jun_Subscription__c = NULL;
        }
    
    //July
        else if (subs.Month__c == 'Jul'){
            if (items.Jul_Amount__c>0){
                items.Jul_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Jul_Subscription__c = NULL;
        }
    
    //August
        else if (subs.Month__c == 'Aug'){
            if (items.Aug_Amount__c>0){
                items.Aug_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Aug_Subscription__c = NULL;
        }
    
    //September
        else if (subs.Month__c == 'Sep'){
            if (items.Sep_Amount__c>0){
                items.Sep_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Sep_Subscription__c = NULL;
        }
    
    //October
        else if (subs.Month__c == 'Oct'){
            if (items.Oct_Amount__c>0){
                items.Oct_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Oct_Subscription__c = NULL;
        }
    
    //November
        else if (subs.Month__c == 'Nov'){
            if (items.Nov_Amount__c>0){
                items.Nov_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Nov_Subscription__c = NULL;
        }
    
    //December
        else if (subs.Month__c == 'Dec'){
            if (items.Dec_Amount__c>0){
                items.Dec_Subscription__c = subs.Id;
                amou += items.Monthly_Amount__c;
                subs.Amount_Sold2__c = amou;
                n += 1;
            }
            else
                items.Dec_Subscription__c = NULL;
        }
    }
    
    else {
    items.Jan_Subscription__c = NULL;
    items.Feb_Subscription__c = NULL;
    items.Mar_Subscription__c = NULL;
    items.Apr_Subscription__c = NULL;
    items.May_Subscription__c = NULL;
    items.Jun_Subscription__c = NULL;
    items.Jul_Subscription__c = NULL;
    items.Aug_Subscription__c = NULL;
    items.Sep_Subscription__c = NULL;
    items.Oct_Subscription__c = NULL;
    items.Nov_Subscription__c = NULL;
    items.Dec_Subscription__c = NULL;
    }
}
    
    subs.Number_Sold__c = n;
    
}

update sub;
update ite;





}

 

And finally the Test class:

@isTest
private class TestIPITriggers {

static TestMethod void TestTotalItemAmount(){

Account company = new Account(Name='test company', BillingStreet='123 red', BillingCity='London', BillingPostalCode='123fgh', BillingCountry='United Kingdom');
insert company;

Contact contact = new Contact(LastName = 'test', AccountId = company.Id, Email = 'test@test.com', Phone='1234');
insert contact;

Opportunity deal = new Opportunity(Name='autofill', AccountId = company.Id, RecordTypeId ='01220000000AGqi', Contact_Delegate__c=contact.Id, CloseDate=date.today(), 
                    StageName='Closed - Awaiting Approval', Invoice_Address__c='Company', Address_Invoice_to_contact_above__c='Yes', 
                    Voucher_Copy_Address__c='Company', Address_Voucher_Copy_to_contact_above__c='Yes');
insert deal;

Issue_Year__c event = new Issue_Year__c(Name='LC11a', Date__c=date.newinstance(2011, 1, 1), Description__c='Leaders Council 2011', SUN_T3_Code__c = 'LC2011');
insert event;

IPI_Subscription__c subscription1 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jan', Name='LCJan11a');
insert subscription1;

IPI_Subscription__c subscription2 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Feb', Name='LCFeb11a');
insert subscription2;

IPI_Subscription__c subscription3 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Mar', Name='LCMar11a');
insert subscription3;

IPI_Subscription__c subscription4 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Apr', Name='LCApr11a');
insert subscription4;

IPI_Subscription__c subscription5 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='May', Name='LCMay11a');
insert subscription5;

IPI_Subscription__c subscription6 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jun', Name='LCJun11a');
insert subscription6;

IPI_Subscription__c subscription7 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Jul', Name='LCJul11a');
insert subscription7;

IPI_Subscription__c subscription8 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Aug', Name='LCAug11a');
insert subscription8;

IPI_Subscription__c subscription9 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Sep', Name='LCSep11a');
insert subscription9;

IPI_Subscription__c subscription10 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Oct', Name='LCOct11a');
insert subscription10;

IPI_Subscription__c subscription11 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Nov', Name='LCNov11a');
insert subscription11;

IPI_Subscription__c subscription12 = new IPI_Subscription__c(Event_Issue__c=event.Id, Month__c='Dec', Name='LCDec11a');
insert subscription12;

Item__c item1 = new Item__c(Name='.', Opportunity__c = deal.Id, Full_Amount2__c=6000, RecordTypeId='01220000000AGp1', Probability__c=100, 
                Event_Issue__c = event.Id, Start_Date__c=date.newinstance(2011, 1, 1), Term__c=6, Payment_Terms__c='28 days');
insert item1;

Item__c[] items = [SELECT Id, Amount__c, Amount_Formula__c, Jan_Subscription__r.Name, Jan_Amount__c, Event_Issue__r.Name FROM Item__c WHERE Event_Issue__r.Name = 'LC11a'];

for (Item__c it :items){
system.assertEquals(it.Jan_Amount__c, 1000);
system.assertEquals(it.Jan_Subscription__r.Name, 'LCJan11a');
}

item1.Start_Date__c=date.newinstance(2011, 7, 1);
update item1;

item1.Probability__c = 50;
update item1;

delete item1;


}

}

 

 

Hope you can help!