• jaredbaker
  • NEWBIE
  • 75 Points
  • Member since 2011

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

I have the following code for pulling up driving directions from SalesForce. Works amazingly well as long as the address is on one line. When it's a two-line address, none of the script will load. I've tried putting the variable into a try/catch rule, but that doesn't solve the problem. Advice?

 

 

 

<apex:page standardController="Account" showHeader="false" sidebar="false" standardstylesheets="false">
<script>

// SOFTWARE BUG!!! MULTI-LINE ADDRESS AND PAGE LOADS W/O JAVASCRIPT!

var currentLocation;

function getLocation()
  {
    navigator.geolocation.getCurrentPosition(showPosition);
  }

function showPosition(position)
  {

try {

    var address = '{!Account.BillingStreet}' + '{!Account.BillingCity},' + '{!Account.BillingState}' + '{!Account.BillingPostalCode}';
    var destination = address.split(' ').join('+');
}
catch(err)
{

    alert('Street address must be on one line to use this function.');
    window.close();
    
}

  currentLocation = position.coords.latitude + ',' + position.coords.longitude;
  
  if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i)))
    {
        var URL = 'http://maps.apple.com/?saddr=' + currentLocation + '&daddr=' + destination;
        window.open(URL,'_parent');
    }
  else
    {
        var URL = 'https://maps.google.com/maps?saddr=' + currentLocation + '&daddr=' + destination;
        window.open(URL,'_parent');
    }
  }
function init()
  {
    document.write('<div style="position:absolute;top:50%;left:50%;">loading...</div>');
    getLocation();
  }

window.onload = new function() { init(); };


</script>

</apex:page>

I'm trying to write code to have the system send an email to a user when an administrator has added a comment to their activities.  I want the trigger to only fire when an update has been made to this field.  I am consistently getting the above error, but I assume there will be others when I resolve it.  Please help!

 

trigger adminCommentEmail on Task (after update) {
    List<Task> t = [SELECT Id, owner.Id, owner.Email, Subject,
                    ActivityDate, Administrative_Comments_Questions__c
                    FROM Task t];
    if(t.Administrative_Comments_Questions__c != PriorValue(t.Administrative_Comments_Questions__c))
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {t.subject};  //error here
        mail.setToAddresses(toAddresses);
        mail.setSubject('Administrator Comment on Your Activity');
        mail.setPlainTextBody('A comment has been added to your activity.  To view it, please click the following link: https://cs10.salesforce.com/'
                          + t.Id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

I'm currently working to create a test class and am running into a brick wall.  The test is returning an error after the first run and not iterating through.  My error is:

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]

 

Class.saleIO.ioJJInit: line 336, column 9 Class.saleIOTest.IOTest: line 38, column 15 External entry point

 

The relevant code is below.  What am I doing wrong?

 

                         

 Opportunity a = [SELECT Id, Account.Id, Account.Owner.Name, Contact__r.Name, Account.BillingStreet, Account.Name,
                         Account.BillingCity, Contact__r.Email, Contact__r.Fax, Contact__r.Phone,
                         Account.BillingState, Account.BillingPostalCode, Category__c,
                         Notes__c, Display_Ad_Size_JJ__c, Display_Ad_Size_TR__c, Display_Ad_Size__c,
                         Art_Date__c, Art_Info__c, Bill_To__c, Insertion_Dates__c, Insertion_Dates_TR__c,
                         Amount, Color__c, New_Advertiser__c
                         FROM Opportunity
 WHERE id = :ApexPages.currentPage().getParameters().get('id')];

 



public PageReference ioJJInit()
{
    String xmlContent = getJJXmlString(a);
    
    Attachment attachment = new Attachment();
        attachment.Name = 'JJ_' + a.Account.Name + '_' + formatted + '.xfdf';
        attachment.ParentId = a.Account.Id;
        attachment.Body = Blob.valueof(xmlContent);
        insert attachment; /* line 336, location of the error */
    
    PageReference orgPage = new PageReference('/' + a.Account.id);
    orgPage.setRedirect(true);
    return orgPage;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@isTest
private class saleIOTest {

    static testMethod void IOTest() {

    String [] jjSizes = new String[] {'1/16', '1/2 Horizontal', '1/2 Vertical', '1/4 Horizontal', '1/4 Vertical', '1/8 Horizontal', '1/8 Vertical', '3/4', '3/8', 'Full', 'Jr.', 'TOC', null};
    
    String [] trSizes = new String[] {'1/6', '1/3 Vertical', '1/2 Horizontal', '1/2 Vertical', '1/4 Vertical', '2/3', 'Full', 'Double Truck', null};
    
    String [] mpSizes = new String[] {'1/2 Horizontal', '1/2 Vertical', '1/3 Vertical', '1/4 Vertical', '1/8 Horizontal', 'Full', '1x1', '1x2', '1x3', '2x1', '2x2', '2x3', '3x1', '3x2', '3x3', null};
    
    Date testDay = Date.parse('11/10/2011');
    
    User u = [SELECT Id FROM User WHERE name = 'Jared Baker'];
    
    Account testOrg = new Account (Name = 'Test', BillingStreet = '3580 Wilshire',
        BillingState = 'CA', BillingPostalCode = '90010');
    insert testOrg;
        
    Contact testCon = new Contact (FirstName = 'Joe', LastName = 'Tester', Email = 'jotes@jewishjournal.com', Fax = '(213) 555-0789', Phone = '(213) 555-0790', Account = testOrg);
    insert testCon;
    
    Opportunity testOpp = new Opportunity (Name = 'Test Op', Owner = u, Account = testOrg, Contact__c = testCon.ID, Category__c = 'Rockin Test', Art_Date__c = testDay,
        Notes__c = 'notes test', New_Advertiser__c = true, Bill_to__c = 'Agency', StageName = 'Won', CloseDate = testDay, Color__c = 'One');
    insert testOpp;
    System.debug('Organization ID=' + testOpp.Account.ID);

    for(Integer i = 0; i<13; i++)
    {
        testOpp.Display_Ad_Size_JJ__c = jjSizes[i];
        update testOpp;
        if(testOpp.Display_Ad_Size_JJ__c != null)
        {
            System.debug('Organization ID=' + testOpp.Account.ID);
            PageReference jjRef = new PageReference('/apex/saleIO?id=' + testOpp.id);
            Test.setCurrentPage(jjRef);
            saleIO testJJIO = new  saleIO();
              testJJIO.ioJJInit();
        }
    }
    
    testOpp.Notes__c = null;
    testOpp.New_Advertiser__c = false;
    testOpp.Bill_to__c = 'Advertiser';
    testOpp.Insertion_Dates__c = '11/10';
    update testOpp;    

  }  
}

So, I have the code running to create the .xfdf and attach it to the proper account.  Everything looked perfect, until I went to open my file.  Instead of the .xfdf opening and overlaying the online form, it instead downloads a new copy of itself.  Is there an issue in my xlm?  What am I doing wrong here.  Below is the code, and I have verified the .xfdf's are coming out correctly.

 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public with sharing class CreateIO {

    DateTime nowDT=System.now();
    String formatted=nowDT.format('MM-dd-yyyy');

private String getXmlString(JJSale__c a)
{

    
    String s = '<?xml version="1.0" encoding="UTF-8"?>' +
        '<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">' +
        '<f href="https://na11.salesforce.com/resource/1300398748000/IO_JJ"/>' +
        '<fields>' +
        '<field name="Acct Rep"><value>' + a.CreatedBy.Name + '</value></field>' +
        '<field name="Ad Cost"><value>' + a.Ad_Price__c + '</value></field>' +
        '<field name="Address"><value>' + a.Organization__r.BillingStreet + '</value></field>' +
        '<field name="Advertiser"><value>' + a.Organization__r.Name + '</value></field>' +
        '<field name="Bill to"><value>x</value></field>' +
        '<field name="City"><value>' + a.Organization__r.BillingCity + '</value></field>' +
        '<field name="Contact"><value>' + a.Contact__r.Name + '</value></field>' +
        '<field name="Date"><value>' + formatted + '</value></field>' +
        '<field name="Email"><value>' + a.Contact__r.Email + '</value></field>' +
        '<field name="Fax"><value>' + a.Contact__r.Fax + '</value></field>' +
        '<field name="INSERTION DATES AND NOTES 1"><value>' + a.Journal_Issue__r.Name + '</value></field>' +
        '<field name="INSERTION DATES AND NOTES 2"><value>' +
            a.Page_Requested__c + ', ' + a.Insertion_Notes__c + '</value></field>' +
        '<field name="Phone"><value>' + a.Contact__r.Phone + '</value></field>' +
        '<field name="State"><value>' + a.Organization__r.BillingState + '</value></field>' +
        '<field name="Todays Date"><value>' + formatted + '</value></field>' +
        '<field name="Zip"><value>' + a.Organization__r.BillingPostalCode + '</value></field>' +
        '</fields><ids original="CB86FA72BFC7A2744D5052A67D1686EE" modified="C0A949601A5BB74AB94C1DAA08D65D2F"/>' +
        '</xfdf>';
        
    return s;
}

public PageReference XFDFInit()
{
    JJSale__c a = [SELECT Id, CreatedBy.Name, Ad_Price__c, Organization__r.BillingStreet, Organization__r.Name,
                         Organization__r.BillingCity, Contact__r.Name, Contact__r.Email, Contact__r.Fax,
                         Journal_Issue__r.Name, Page_Requested__c, Insertion_Notes__c, Contact__r.Phone,
                         Organization__r.BillingState, Organization__r.BillingPostalCode
                         FROM JJSale__c
                         WHERE id = :ApexPages.currentPage().getParameters().get('id')];
    String xmlContent = getXmlString(a);
    
    Attachment attachment = new Attachment();
        attachment.Body = Blob.valueof(xmlContent);
        attachment.Name = a.Organization__r.Name + '_' + formatted + '.XFDF';
        attachment.ParentId = a.Organization__r.Id;
        insert attachment;
    
    PageReference orgPage = new PageReference('/' + a.Organization__r.id);
    orgPage.setRedirect(true);
    return orgPage;
}

}

UPDATE:

 

I've gotten it working, made a button, it is attaching the file properly and everything.  But now, when I go to open the .xfdf file, it just downloads another copy, or get stuck in a loop without ever opening the form.  Now what!?

 

 

I am trying to create a button that will call a Visualforce page which calls the below code to make a .xfdf file.  For whatever reason, it won't let me make a button.  What am I doing wrong?  Any help is HUGELY appreciated!  Thanks in advance!

 





 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public with sharing class CreateIO {

    DateTime nowDT=System.now();
    String formatted=nowDT.format('dd_MM_yyyy');

private String getXmlString(JJSale__c a)
{

    
    String s = '<?xml version="1.0" encoding="UTF-8"?>' +
        '<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">' +
        '<f href="https://cs9.salesforce.com/resource/1304098375000/IO_JJ"/>' +
        '<fields>' +
        '<field name="Acct Rep"><value>' + a.CreatedBy.Name + '</value></field>' +
        '<field name="Ad Cost"><value>' + a.Ad_Price__c + '</value></field>' +
        '<field name="Address"><value>' + a.Organization__r.BillingStreet + '</value></field>' +
        '<field name="Advertiser"><value>' + a.Organization__c + '</value></field>' +
        '<field name="Bill to"><value>x</value></field>' +
        '<field name="City"><value>' + a.Organization__r.BillingCity + '</value></field>' +
        '<field name="Contact"><value>' + a.Contact__c + '</value></field>' +
        '<field name="Date"><value>' + formatted + '</value></field>' +
        '<field name="Email"><value>' + a.Contact__r.Email + '</value></field>' +
        '<field name="Fax"><value>' + a.Contact__r.Fax + '</value></field>' +
        '<field name="INSERTION DATES AND NOTES 1"><value>' + a.Journal_Issue__c + '</value></field>' +
        '<field name="INSERTION DATES AND NOTES 2"><value>' +
            a.Page_Requested__c + ',' + a.Insertion_Notes__c + '</value></field>' +
        '<field name="Phone"><value>' + a.Contact__r.Phone + '</value></field>' +
        '<field name="State"><value>' + a.Organization__r.BillingState + '</value></field>' +
        '<field name="Todays Date"><value>' + formatted + '</value></field>' +
        '<field name="Zip"><value>' + a.Organization__r.BillingPostalCode + '</value></field>' +
        '</fields><ids original="CB86FA72BFC7A2744D5052A67D1686EE" modified="C0A949601A5BB74AB94C1DAA08D65D2F"/>' +
        '</xfdf>';
        
    return s;
}

public PageReference XFDFInit()
{
    JJSale__c a = [SELECT Id, CreatedBy.Name, Ad_Price__c, Organization__r.BillingStreet, Organization__c,
                         Organization__r.BillingCity, Contact__c, Contact__r.Email, Contact__r.Fax,
                         Journal_Issue__c, Page_Requested__c, Insertion_Notes__c, Contact__r.Phone,
                         Organization__r.BillingState, Organization__r.BillingPostalCode
                         FROM JJSale__c
                         WHERE id = :ApexPages.currentPage().getParameters().get('id')];
    String xmlContent = getXmlString(a);
    
    Attachment attachment = new Attachment();
        attachment.Body = Blob.valueof(xmlContent);
        attachment.Name = a.Organization__c + formatted + '.XFDF';
        attachment.ParentId = a.Id;
        insert attachment;
    
    PageReference salePage = new PageReference('/' + a.id);
    salePage.setRedirect(true);
    return salePage;
}

}
<apex:page showHeader="true"
    controller="CreateIO"
    action="{!XFDFInit}">
</apex:page>

Our users are constantly forgetting to link tasks to both accounts and contacts.  Those that remember hate doubling their work to do it.  I've tried to write a trigger that would do the work for them, but I'm VERY new to the language (and object-oriented programming in general) and am having some troubles referencing the proper fields.  I've coppied my current failed attempt below...maybe its not all a waste.

 

 

trigger AddOrgName on Task (before insert) {
 List<Task> newTask = 
 [SELECT t.Who.name, t.Who.Account.Name, t.whatid
 FROM Task t
 WHERE t.WhatId = ''
 FOR UPDATE];
 for (Task nt: newTask) {
 if(nt.WhatId == '') {
 nt.What = nt.Who.Account.Name;
 }
 }
update newTask;
}

trigger AddOrgName on Task (before insert) { List<Task> newTask =  [SELECT t.Who.name, t.Who.Account.Name, t.whatid FROM Task t WHERE t.WhatId = '' FOR UPDATE]; for (Task nt: newTask) { if(nt.WhatId == '') { nt.What = nt.Who.Account.Name; } } update newTask;}